blob: 15c9c06b5c282660b8f3f93780f8084f5463c5b3 [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
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 *
17 * @author Aditya Agarwal
18 */
19
Mark Slee4f261c52007-04-13 00:33:24 +000020#ifndef HAVE_CLOCK_GETTIME
Mark Slee63608e82006-10-26 05:06:26 +000021#include <time.h>
Mark Slee4f261c52007-04-13 00:33:24 +000022#else
23#include <sys/time.h>
24#endif
25
26#ifdef HAVE_STDINT_H
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000027#include <stdint.h>
Mark Slee4f261c52007-04-13 00:33:24 +000028#endif
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000029
30/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000031 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
32 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000033 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000034#define T_GLOBAL_DEBUGGING_LEVEL 0
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000035
36
37/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000038 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
39 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000040 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000041#define T_GLOBAL_LOGGING_LEVEL 1
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000042
43
44/**
45 * Standard wrapper around fprintf what will prefix the file name and line
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000046 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000047 * turned on or off.
48 *
49 * @param format_string
50 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000051#if T_GLOBAL_DEBUGGING_LEVEL > 0
52 #define T_DEBUG(format_string,...) \
53 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000054 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
55 }
56#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000057 #define T_DEBUG(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000058#endif
59
60
61/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000062 * analagous to T_DEBUG but also prints the time
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000063 *
64 * @param string format_string input: printf style format string
65 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000066#if T_GLOBAL_DEBUGGING_LEVEL > 0
67 #define T_DEBUG_T(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000068 { \
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000069 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000070 time_t now; \
71 char dbgtime[26] ; \
72 time(&now); \
73 ctime_r(&now, dbgtime); \
74 dbgtime[24] = '\0'; \
75 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
76 } \
77 }
78#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000079 #define T_DEBUG_T(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000080#endif
81
82
83/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000084 * analagous to T_DEBUG but uses input level to determine whether or not the string
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000085 * should be logged.
86 *
87 * @param int level: specified debug level
88 * @param string format_string input: format string
89 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +000090#define T_DEBUG_L(level, format_string,...) \
91 if ((level) > 0) { \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +000092 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
93 }
94
95
96/**
97 * Explicit error logging. Prints time, file name and line number
98 *
99 * @param string format_string input: printf style format string
100 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000101#define T_ERROR(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000102 { \
103 time_t now; \
104 char dbgtime[26] ; \
105 time(&now); \
106 ctime_r(&now, dbgtime); \
107 dbgtime[24] = '\0'; \
108 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
109 }
110
111
112/**
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000113 * Analagous to T_ERROR, additionally aborting the process.
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000114 * WARNING: macro calls abort(), ending program execution
115 *
116 * @param string format_string input: printf style format string
117 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000118#define T_ERROR_ABORT(format_string,...) \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000119 { \
120 time_t now; \
121 char dbgtime[26] ; \
122 time(&now); \
123 ctime_r(&now, dbgtime); \
124 dbgtime[24] = '\0'; \
125 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
126 exit(1); \
127 }
128
129
130/**
131 * Log input message
132 *
133 * @param string format_string input: printf style format string
134 */
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000135#if T_GLOBAL_LOGGING_LEVEL > 0
136 #define T_LOG_OPER(format_string,...) \
137 { \
138 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
139 time_t now; \
140 char dbgtime[26] ; \
141 time(&now); \
142 ctime_r(&now, dbgtime); \
143 dbgtime[24] = '\0'; \
144 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
145 } \
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000146 }
147#else
Aditya Agarwal35ae1c72006-10-26 03:31:34 +0000148 #define T_LOG_OPER(format_string,...)
Aditya Agarwalbe3f8d82006-10-11 02:43:25 +0000149#endif
150
151#endif // _THRIFT_LOGGING_H