blob: e814e417091d2b6b32ab69ddec65bd2d8f6b89ce [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +00007#ifndef _THRIFT_LOGGING_H
8#define _THRIFT_LOGGING_H 1
9
10/**
11 * Contains utility macros for debugging and logging.
12 *
13 * @author Aditya Agarwal
14 */
15
Mark Slee63608e82006-10-26 05:06:26 +000016#include <time.h>
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000017#include <stdint.h>
18
19/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000020 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
21 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000022 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000023#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000024
25
26/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000027 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
28 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000029 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000030#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000031
32
33/**
34 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000035 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000036 * turned on or off.
37 *
38 * @param format_string
39 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000040#if T_GLOBAL_DEBUGGING_LEVEL > 0
41 #define T_DEBUG(format_string,...) \
42 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000043 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
44 }
45#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000046 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000047#endif
48
49
50/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000051 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000052 *
53 * @param string format_string input: printf style format string
54 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000055#if T_GLOBAL_DEBUGGING_LEVEL > 0
56 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000057 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000058 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000059 time_t now; \
60 char dbgtime[26] ; \
61 time(&now); \
62 ctime_r(&now, dbgtime); \
63 dbgtime[24] = '\0'; \
64 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
65 } \
66 }
67#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000068 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000069#endif
70
71
72/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000073 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000074 * should be logged.
75 *
76 * @param int level: specified debug level
77 * @param string format_string input: format string
78 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000079#define T_DEBUG_L(level, format_string,...) \
80 if ((level) > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000081 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
82 }
83
84
85/**
86 * Explicit error logging. Prints time, file name and line number
87 *
88 * @param string format_string input: printf style format string
89 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000090#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000091 { \
92 time_t now; \
93 char dbgtime[26] ; \
94 time(&now); \
95 ctime_r(&now, dbgtime); \
96 dbgtime[24] = '\0'; \
97 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
98 }
99
100
101/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000102 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000103 * WARNING: macro calls abort(), ending program execution
104 *
105 * @param string format_string input: printf style format string
106 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000107#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000108 { \
109 time_t now; \
110 char dbgtime[26] ; \
111 time(&now); \
112 ctime_r(&now, dbgtime); \
113 dbgtime[24] = '\0'; \
114 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
115 exit(1); \
116 }
117
118
119/**
120 * Log input message
121 *
122 * @param string format_string input: printf style format string
123 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000124#if T_GLOBAL_LOGGING_LEVEL > 0
125 #define T_LOG_OPER(format_string,...) \
126 { \
127 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
128 time_t now; \
129 char dbgtime[26] ; \
130 time(&now); \
131 ctime_r(&now, dbgtime); \
132 dbgtime[24] = '\0'; \
133 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
134 } \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000135 }
136#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000137 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000138#endif
139
140#endif // _THRIFT_LOGGING_H