1/*
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 */
19
20#ifndef _THRIFT_TLOGGING_H_
21#define _THRIFT_TLOGGING_H_ 1
22
23#include <thrift/thrift-config.h>
24
25/**
26 * Contains utility macros for debugging and logging.
27 *
28 */
29
30#include <time.h>
31
32#ifdef HAVE_STDINT_H
33#include <stdint.h>
34#endif
35
36/**
37 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
38 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
39 */
40#define T_GLOBAL_DEBUGGING_LEVEL 0
41
42/**
43 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
44 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
45 */
46#define T_GLOBAL_LOGGING_LEVEL 1
47
48/**
49 * Standard wrapper around fprintf what will prefix the file name and line
50 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
51 * turned on or off.
52 *
53 * @param format_string
54 */
55#if T_GLOBAL_DEBUGGING_LEVEL > 0
56#define T_DEBUG(format_string, ...) \
57 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
58 fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
59 }
60#else
61#define T_DEBUG(format_string, ...)
62#endif
63
64/**
65 * analogous to T_DEBUG but also prints the time
66 *
67 * @param string format_string input: printf style format string
68 */
69#if T_GLOBAL_DEBUGGING_LEVEL > 0
70#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 }
86#else
87#define T_DEBUG_T(format_string, ...)
88#endif
89
90/**
91 * analogous to T_DEBUG but uses input level to determine whether or not the string
92 * should be logged.
93 *
94 * @param int level: specified debug level
95 * @param string format_string input: format string
96 */
97#define T_DEBUG_L(level, format_string, ...) \
98 if ((level) > 0) { \
99 fprintf(stderr, "[%s,%d] " format_string " \n", __FILE__, __LINE__, ##__VA_ARGS__); \
100 }
101
102/**
103 * Explicit error logging. Prints time, file name and line number
104 *
105 * @param string format_string input: printf style format string
106 */
107#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__); \
120 }
121
122/**
123 * Analogous to T_ERROR, additionally aborting the process.
124 * WARNING: macro calls abort(), ending program execution
125 *
126 * @param string format_string input: printf style format string
127 */
128#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); \
142 }
143
144/**
145 * Log input message
146 *
147 * @param string format_string input: printf style format string
148 */
149#if T_GLOBAL_LOGGING_LEVEL > 0
150#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 }
161#else
162#define T_LOG_OPER(format_string, ...)
163#endif
164
165/**
166 * 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()
173 */
174#if T_GLOBAL_DEBUG_VIRTUAL > 1
175#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)
182#elif T_GLOBAL_DEBUG_VIRTUAL == 1
183#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)
190#else
191#define T_VIRTUAL_CALL()
192#define T_GENERIC_PROTOCOL(template_class, generic_prot, specific_prot)
193#endif
194
195#endif // #ifndef _THRIFT_TLOGGING_H_
196