blob: 934e8fc11139fba790d254761cc995a634fecc0c [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) { \
David Reiss46e4f252010-10-06 17:10:53 +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'; \
David Reiss46e4f252010-10-06 17:10:53 +000087 fprintf(stderr,"[%s,%d] [%s] " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000088 } \
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) { \
David Reiss46e4f252010-10-06 17:10:53 +0000104 fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000105 }
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'; \
David Reiss46e4f252010-10-06 17:10:53 +0000120 fprintf(stderr,"[%s,%d] [%s] ERROR: " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000121 }
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'; \
David Reiss46e4f252010-10-06 17:10:53 +0000137 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000138 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'; \
David Reiss46e4f252010-10-06 17:10:53 +0000156 fprintf(stderr,"[%s] " format_string " \n", dbgtime,##__VA_ARGS__); \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000157 } \
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/**
David Reissc3b36222010-10-06 17:10:10 +0000165 * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: 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 * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be
170 * printed by calling
171 * apache::thrift::profile_print_info()
David Reisse879c2f2010-10-06 17:09:50 +0000172 */
David Reissc3b36222010-10-06 17:10:10 +0000173#if T_GLOBAL_DEBUG_VIRTUAL > 1
174 #define T_VIRTUAL_CALL() \
175 ::apache::thrift::profile_virtual_call(typeid(*this))
176 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
177 do { \
178 if (!(specific_prot)) { \
179 ::apache::thrift::profile_generic_protocol( \
180 typeid(*template_class), typeid(*generic_prot)); \
181 } \
182 } while (0)
183#elif T_GLOBAL_DEBUG_VIRTUAL == 1
184 #define T_VIRTUAL_CALL() \
David Reisse879c2f2010-10-06 17:09:50 +0000185 fprintf(stderr,"[%s,%d] virtual call\n", __FILE__, __LINE__)
David Reissc3b36222010-10-06 17:10:10 +0000186 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
187 do { \
188 if (!(specific_prot)) { \
189 fprintf(stderr, \
190 "[%s,%d] failed to cast to specific protocol type\n", \
191 __FILE__, __LINE__); \
192 } \
193 } while (0)
David Reisse879c2f2010-10-06 17:09:50 +0000194#else
195 #define T_VIRTUAL_CALL()
David Reissc3b36222010-10-06 17:10:10 +0000196 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot)
David Reisse879c2f2010-10-06 17:09:50 +0000197#endif
198
David Reiss23753122007-08-27 19:57:34 +0000199#endif // #ifndef _THRIFT_TLOGGING_H_