blob: 9224289d29005a200e0399e91f0a4e2ce439e5f0 [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
David Reiss23753122007-08-27 19:57:34 +00007#ifndef _THRIFT_TLOGGING_H_
8#define _THRIFT_TLOGGING_H_ 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +00009
Mark Slee4f261c52007-04-13 00:33:24 +000010#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000014/**
15 * Contains utility macros for debugging and logging.
16 *
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000017 */
18
Mark Slee4f261c52007-04-13 00:33:24 +000019#ifndef HAVE_CLOCK_GETTIME
Mark Slee63608e82006-10-26 05:06:26 +000020#include <time.h>
Mark Slee4f261c52007-04-13 00:33:24 +000021#else
22#include <sys/time.h>
23#endif
24
25#ifdef HAVE_STDINT_H
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000026#include <stdint.h>
Mark Slee4f261c52007-04-13 00:33:24 +000027#endif
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000028
29/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000030 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000031 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000032 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000033#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000034
35
36/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000037 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
David Reiss0c90f6f2008-02-06 22:18:40 +000038 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000039 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000040#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000041
42
David Reiss0c90f6f2008-02-06 22:18:40 +000043/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000044 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000045 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000046 * turned on or off.
47 *
David Reiss0c90f6f2008-02-06 22:18:40 +000048 * @param format_string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000049 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000050#if T_GLOBAL_DEBUGGING_LEVEL > 0
51 #define T_DEBUG(format_string,...) \
52 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000053 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
David Reiss0c90f6f2008-02-06 22:18:40 +000054 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000055#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000056 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000057#endif
58
59
David Reiss0c90f6f2008-02-06 22:18:40 +000060/**
61 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000062 *
63 * @param string format_string input: printf style format string
64 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000065#if T_GLOBAL_DEBUGGING_LEVEL > 0
66 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000067 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000068 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000069 time_t now; \
70 char dbgtime[26] ; \
71 time(&now); \
72 ctime_r(&now, dbgtime); \
73 dbgtime[24] = '\0'; \
74 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
75 } \
76 }
77#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000078 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000079#endif
80
81
David Reiss0c90f6f2008-02-06 22:18:40 +000082/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000083 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000084 * should be logged.
85 *
86 * @param int level: specified debug level
87 * @param string format_string input: format string
88 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000089#define T_DEBUG_L(level, format_string,...) \
90 if ((level) > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000091 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
92 }
93
94
David Reiss0c90f6f2008-02-06 22:18:40 +000095/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000096 * Explicit error logging. Prints time, file name and line number
97 *
98 * @param string format_string input: printf style format string
99 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000100#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000101 { \
102 time_t now; \
103 char dbgtime[26] ; \
104 time(&now); \
105 ctime_r(&now, dbgtime); \
106 dbgtime[24] = '\0'; \
107 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
108 }
109
110
David Reiss0c90f6f2008-02-06 22:18:40 +0000111/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000112 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000113 * WARNING: macro calls abort(), ending program execution
114 *
115 * @param string format_string input: printf style format string
116 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000117#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000118 { \
119 time_t now; \
120 char dbgtime[26] ; \
121 time(&now); \
122 ctime_r(&now, dbgtime); \
123 dbgtime[24] = '\0'; \
124 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
125 exit(1); \
126 }
127
128
David Reiss0c90f6f2008-02-06 22:18:40 +0000129/**
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000130 * Log input message
131 *
132 * @param string format_string input: printf style format string
133 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000134#if T_GLOBAL_LOGGING_LEVEL > 0
135 #define T_LOG_OPER(format_string,...) \
136 { \
137 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
138 time_t now; \
139 char dbgtime[26] ; \
140 time(&now); \
141 ctime_r(&now, dbgtime); \
142 dbgtime[24] = '\0'; \
143 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
144 } \
David Reiss0c90f6f2008-02-06 22:18:40 +0000145 }
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000146#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000147 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000148#endif
149
David Reiss23753122007-08-27 19:57:34 +0000150#endif // #ifndef _THRIFT_TLOGGING_H_