blob: 54cccbec1d8751e1421544126b8e6b226a33d20f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
David Reiss23753122007-08-27 19:57:34 +000020#ifndef _THRIFT_TLOGGING_H_
21#define _THRIFT_TLOGGING_H_ 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000022
Mark Slee4f261c52007-04-13 00:33:24 +000023#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000027/**
28 * Contains utility macros for debugging and logging.
29 *
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000030 */
31
Mark Slee4f261c52007-04-13 00:33:24 +000032#ifndef HAVE_CLOCK_GETTIME
Mark Slee63608e82006-10-26 05:06:26 +000033#include <time.h>
Mark Slee4f261c52007-04-13 00:33:24 +000034#else
35#include <sys/time.h>
36#endif
37
38#ifdef HAVE_STDINT_H
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000039#include <stdint.h>
Mark Slee4f261c52007-04-13 00:33:24 +000040#endif
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000041
42/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000043 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000044 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000045 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000046#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000047
48
49/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000050 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000051 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000052 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000053#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000054
55
David Reiss0c90f6f2008-02-06 22:18:40 +000056/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000057 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000058 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000059 * turned on or off.
60 *
David Reiss0c90f6f2008-02-06 22:18:40 +000061 * @param format_string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000062 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000063#if T_GLOBAL_DEBUGGING_LEVEL > 0
64 #define T_DEBUG(format_string,...) \
65 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000066 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
David Reiss0c90f6f2008-02-06 22:18:40 +000067 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000068#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000069 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000070#endif
71
72
David Reiss0c90f6f2008-02-06 22:18:40 +000073/**
74 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000075 *
76 * @param string format_string input: printf style format string
77 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000078#if T_GLOBAL_DEBUGGING_LEVEL > 0
79 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000080 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000081 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000082 time_t now; \
83 char dbgtime[26] ; \
84 time(&now); \
85 ctime_r(&now, dbgtime); \
86 dbgtime[24] = '\0'; \
87 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
88 } \
89 }
90#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000091 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000092#endif
93
94
David Reiss0c90f6f2008-02-06 22:18:40 +000095/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000096 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000097 * should be logged.
98 *
99 * @param int level: specified debug level
100 * @param string format_string input: format string
101 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000102#define T_DEBUG_L(level, format_string,...) \
103 if ((level) > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000104 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
105 }
106
107
David Reiss0c90f6f2008-02-06 22:18:40 +0000108/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000109 * Explicit error logging. Prints time, file name and line number
110 *
111 * @param string format_string input: printf style format string
112 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000113#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000114 { \
115 time_t now; \
116 char dbgtime[26] ; \
117 time(&now); \
118 ctime_r(&now, dbgtime); \
119 dbgtime[24] = '\0'; \
120 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
121 }
122
123
David Reiss0c90f6f2008-02-06 22:18:40 +0000124/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000125 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000126 * WARNING: macro calls abort(), ending program execution
127 *
128 * @param string format_string input: printf style format string
129 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000130#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000131 { \
132 time_t now; \
133 char dbgtime[26] ; \
134 time(&now); \
135 ctime_r(&now, dbgtime); \
136 dbgtime[24] = '\0'; \
137 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
138 exit(1); \
139 }
140
141
David Reiss0c90f6f2008-02-06 22:18:40 +0000142/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000143 * Log input message
144 *
145 * @param string format_string input: printf style format string
146 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000147#if T_GLOBAL_LOGGING_LEVEL > 0
148 #define T_LOG_OPER(format_string,...) \
149 { \
150 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
151 time_t now; \
152 char dbgtime[26] ; \
153 time(&now); \
154 ctime_r(&now, dbgtime); \
155 dbgtime[24] = '\0'; \
156 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
157 } \
David Reiss0c90f6f2008-02-06 22:18:40 +0000158 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000159#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000160 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000161#endif
162
David Reisse879c2f2010-10-06 17:09:50 +0000163
164/**
165 * T_GLOBAL_DEBUG_VIRTUAL = 0: normal operation,
166 * virtual call debug messages disabled
167 * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an
168 * avoidable virtual call is made
169 */
170#define T_GLOBAL_DEBUG_VIRTUAL 0
171
172/**
173 * Log a message indicating that a virtual function call is being made.
174 *
175 * This should be disabled during normal use. It is intended to be used
176 * only to help debug serialization performance.
177 */
178#if T_GLOBAL_DEBUG_VIRTUAL > 0
179 #define T_VIRTUAL_CALL() \
180 fprintf(stderr,"[%s,%d] virtual call\n", __FILE__, __LINE__)
181#else
182 #define T_VIRTUAL_CALL()
183#endif
184
David Reiss23753122007-08-27 19:57:34 +0000185#endif // #ifndef _THRIFT_TLOGGING_H_