blob: 07ff030f7da74734af7fc26cd0c81d7f461c1b66 [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
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000042/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000043 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000044 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000045 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010046#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000047
David Reiss0c90f6f2008-02-06 22:18:40 +000048/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000049 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000050 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000051 * turned on or off.
52 *
David Reiss0c90f6f2008-02-06 22:18:40 +000053 * @param format_string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000054 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000055#if T_GLOBAL_DEBUGGING_LEVEL > 0
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056#define T_DEBUG(format_string, ...) \
57 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
58 fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
David Reiss0c90f6f2008-02-06 22:18:40 +000059 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000060#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +010061#define T_DEBUG(format_string, ...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000062#endif
63
David Reiss0c90f6f2008-02-06 22:18:40 +000064/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +010065 * analogous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000066 *
67 * @param string format_string input: printf style format string
68 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000069#if T_GLOBAL_DEBUGGING_LEVEL > 0
Konrad Grochowski16a23a62014-11-13 15:33:38 +010070#define T_DEBUG_T(format_string, ...) \
71 { \
72 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
73 time_t now; \
74 char dbgtime[26]; \
75 time(&now); \
76 THRIFT_CTIME_R(&now, dbgtime); \
77 dbgtime[24] = '\0'; \
78 fprintf(stderr, \
79 "[%s,%d] [%s] " format_string " \n", \
80 __FILE__, \
81 __LINE__, \
82 dbgtime, \
83 ##__VA_ARGS__); \
84 } \
85 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000086#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087#define T_DEBUG_T(format_string, ...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000088#endif
89
David Reiss0c90f6f2008-02-06 22:18:40 +000090/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +010091 * analogous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000092 * should be logged.
93 *
94 * @param int level: specified debug level
95 * @param string format_string input: format string
96 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097#define T_DEBUG_L(level, format_string, ...) \
98 if ((level) > 0) { \
99 fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000100 }
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 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100107#define T_ERROR(format_string, ...) \
108 { \
109 time_t now; \
110 char dbgtime[26]; \
111 time(&now); \
112 THRIFT_CTIME_R(&now, dbgtime); \
113 dbgtime[24] = '\0'; \
114 fprintf(stderr, \
115 "[%s,%d] [%s] ERROR: " format_string " \n", \
116 __FILE__, \
117 __LINE__, \
118 dbgtime, \
119 ##__VA_ARGS__); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000120 }
121
David Reiss0c90f6f2008-02-06 22:18:40 +0000122/**
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100123 * Analogous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000124 * WARNING: macro calls abort(), ending program execution
125 *
126 * @param string format_string input: printf style format string
127 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100128#define T_ERROR_ABORT(format_string, ...) \
129 { \
130 time_t now; \
131 char dbgtime[26]; \
132 time(&now); \
133 THRIFT_CTIME_R(&now, dbgtime); \
134 dbgtime[24] = '\0'; \
135 fprintf(stderr, \
136 "[%s,%d] [%s] ERROR: Going to abort " format_string " \n", \
137 __FILE__, \
138 __LINE__, \
139 dbgtime, \
140 ##__VA_ARGS__); \
141 exit(1); \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000142 }
143
David Reiss0c90f6f2008-02-06 22:18:40 +0000144/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000145 * Log input message
146 *
147 * @param string format_string input: printf style format string
148 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000149#if T_GLOBAL_LOGGING_LEVEL > 0
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100150#define T_LOG_OPER(format_string, ...) \
151 { \
152 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
153 time_t now; \
154 char dbgtime[26]; \
155 time(&now); \
156 THRIFT_CTIME_R(&now, dbgtime); \
157 dbgtime[24] = '\0'; \
158 fprintf(stderr, "[%s] " format_string " \n", dbgtime, ##__VA_ARGS__); \
159 } \
160 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000161#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100162#define T_LOG_OPER(format_string, ...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000163#endif
164
David Reisse879c2f2010-10-06 17:09:50 +0000165/**
David Reissc3b36222010-10-06 17:10:10 +0000166 * T_GLOBAL_DEBUG_VIRTUAL = 0 or unset: normal operation,
167 * virtual call debug messages disabled
168 * T_GLOBAL_DEBUG_VIRTUAL = 1: log a debug messages whenever an
169 * avoidable virtual call is made
170 * T_GLOBAL_DEBUG_VIRTUAL = 2: record detailed info that can be
171 * printed by calling
172 * apache::thrift::profile_print_info()
David Reisse879c2f2010-10-06 17:09:50 +0000173 */
David Reissc3b36222010-10-06 17:10:10 +0000174#if T_GLOBAL_DEBUG_VIRTUAL > 1
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100175#define T_VIRTUAL_CALL() ::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(typeid(*template_class), typeid(*generic_prot)); \
180 } \
181 } while (0)
David Reissc3b36222010-10-06 17:10:10 +0000182#elif T_GLOBAL_DEBUG_VIRTUAL == 1
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100183#define T_VIRTUAL_CALL() fprintf(stderr, "[%s,%d] virtual call\n", __FILE__, __LINE__)
184#define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot) \
185 do { \
186 if (!(specific_prot)) { \
187 fprintf(stderr, "[%s,%d] failed to cast to specific protocol type\n", __FILE__, __LINE__); \
188 } \
189 } while (0)
David Reisse879c2f2010-10-06 17:09:50 +0000190#else
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100191#define T_VIRTUAL_CALL()
192#define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot)
David Reisse879c2f2010-10-06 17:09:50 +0000193#endif
194
David Reiss23753122007-08-27 19:57:34 +0000195#endif // #ifndef _THRIFT_TLOGGING_H_