blob: 4c8bddc1e9a37db94ffc48d089a961db23f41953 [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
Konrad Grochowski9be4e682013-06-22 22:03:31 +020023#include <thrift/thrift-config.h>
Mark Slee4f261c52007-04-13 00:33:24 +000024
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000025/**
26 * Contains utility macros for debugging and logging.
27 *
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000028 */
29
Mark Slee63608e82006-10-26 05:06:26 +000030#include <time.h>
Mark Slee4f261c52007-04-13 00:33:24 +000031
32#ifdef HAVE_STDINT_H
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000033#include <stdint.h>
Mark Slee4f261c52007-04-13 00:33:24 +000034#endif
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000035
36/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000037 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000038 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000039 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000040#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000041
42
43/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000044 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000045 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000046 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000047#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000048
49
David Reiss0c90f6f2008-02-06 22:18:40 +000050/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000051 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000052 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000053 * turned on or off.
54 *
David Reiss0c90f6f2008-02-06 22:18:40 +000055 * @param format_string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000056 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000057#if T_GLOBAL_DEBUGGING_LEVEL > 0
58 #define T_DEBUG(format_string,...) \
59 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
David Reiss46e4f252010-10-06 17:10:53 +000060 fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
David Reiss0c90f6f2008-02-06 22:18:40 +000061 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000062#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000063 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000064#endif
65
66
David Reiss0c90f6f2008-02-06 22:18:40 +000067/**
68 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000069 *
70 * @param string format_string input: printf style format string
71 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000072#if T_GLOBAL_DEBUGGING_LEVEL > 0
73 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000074 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000075 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000076 time_t now; \
77 char dbgtime[26] ; \
78 time(&now); \
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040079 THRIFT_CTIME_R(&now, dbgtime); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000080 dbgtime[24] = '\0'; \
David Reiss46e4f252010-10-06 17:10:53 +000081 fprintf(stderr,"[%s,%d] [%s] " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000082 } \
83 }
84#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000085 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000086#endif
87
88
David Reiss0c90f6f2008-02-06 22:18:40 +000089/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000090 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000091 * should be logged.
92 *
93 * @param int level: specified debug level
94 * @param string format_string input: format string
95 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000096#define T_DEBUG_L(level, format_string,...) \
97 if ((level) > 0) { \
David Reiss46e4f252010-10-06 17:10:53 +000098 fprintf(stderr,"[%s,%d] " format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000099 }
100
101
David Reiss0c90f6f2008-02-06 22:18:40 +0000102/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000103 * Explicit error logging. Prints time, file name and line number
104 *
105 * @param string format_string input: printf style format string
106 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000107#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000108 { \
109 time_t now; \
110 char dbgtime[26] ; \
111 time(&now); \
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400112 THRIFT_CTIME_R(&now, dbgtime); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000113 dbgtime[24] = '\0'; \
David Reiss46e4f252010-10-06 17:10:53 +0000114 fprintf(stderr,"[%s,%d] [%s] ERROR: " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000115 }
116
117
David Reiss0c90f6f2008-02-06 22:18:40 +0000118/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000119 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000120 * WARNING: macro calls abort(), ending program execution
121 *
122 * @param string format_string input: printf style format string
123 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000124#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000125 { \
126 time_t now; \
127 char dbgtime[26] ; \
128 time(&now); \
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400129 THRIFT_CTIME_R(&now, dbgtime); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000130 dbgtime[24] = '\0'; \
David Reiss46e4f252010-10-06 17:10:53 +0000131 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000132 exit(1); \
133 }
134
135
David Reiss0c90f6f2008-02-06 22:18:40 +0000136/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000137 * Log input message
138 *
139 * @param string format_string input: printf style format string
140 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000141#if T_GLOBAL_LOGGING_LEVEL > 0
142 #define T_LOG_OPER(format_string,...) \
143 { \
144 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
145 time_t now; \
146 char dbgtime[26] ; \
147 time(&now); \
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400148 THRIFT_CTIME_R(&now, dbgtime); \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000149 dbgtime[24] = '\0'; \
David Reiss46e4f252010-10-06 17:10:53 +0000150 fprintf(stderr,"[%s] " format_string " \n", dbgtime,##__VA_ARGS__); \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000151 } \
David Reiss0c90f6f2008-02-06 22:18:40 +0000152 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000153#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000154 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000155#endif
156
David Reisse879c2f2010-10-06 17:09:50 +0000157
158/**
David Reissc3b36222010-10-06 17:10:10 +0000159 * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation,
160 * virtual call debug messages disabled
161 * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an
162 * avoidable virtual call is made
163 * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be
164 * printed by calling
165 * apache::thrift::profile_print_info()
David Reisse879c2f2010-10-06 17:09:50 +0000166 */
David Reissc3b36222010-10-06 17:10:10 +0000167#if T_GLOBAL_DEBUG_VIRTUAL > 1
168 #define T_VIRTUAL_CALL() \
169 ::apache::thrift::profile_virtual_call(typeid(*this))
170 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
171 do { \
172 if (!(specific_prot)) { \
173 ::apache::thrift::profile_generic_protocol( \
174 typeid(*template_class), typeid(*generic_prot)); \
175 } \
176 } while (0)
177#elif T_GLOBAL_DEBUG_VIRTUAL == 1
178 #define T_VIRTUAL_CALL() \
David Reisse879c2f2010-10-06 17:09:50 +0000179 fprintf(stderr,"[%s,%d] virtual call\n", __FILE__, __LINE__)
David Reissc3b36222010-10-06 17:10:10 +0000180 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
181 do { \
182 if (!(specific_prot)) { \
183 fprintf(stderr, \
184 "[%s,%d] failed to cast to specific protocol type\n", \
185 __FILE__, __LINE__); \
186 } \
187 } while (0)
David Reisse879c2f2010-10-06 17:09:50 +0000188#else
189 #define T_VIRTUAL_CALL()
David Reissc3b36222010-10-06 17:10:10 +0000190 #define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot)
David Reisse879c2f2010-10-06 17:09:50 +0000191#endif
192
David Reiss23753122007-08-27 19:57:34 +0000193#endif // #ifndef _THRIFT_TLOGGING_H_