blob: 2df82dd7e35c7e6a2734e1c53171d3eb9085c749 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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 */
19
20#ifndef _THRIFT_TLOGGING_H_
21#define _THRIFT_TLOGGING_H_ 1
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27/**
28 * Contains utility macros for debugging and logging.
29 *
30 */
31
32#ifndef HAVE_CLOCK_GETTIME
33#include <time.h>
34#else
35#include <sys/time.h>
36#endif
37
38#ifdef HAVE_STDINT_H
39#include <stdint.h>
40#endif
41
42/**
43 * T_GLOBAL_DEBUGGING_LEVEL = 0: all debugging turned off, debug macros undefined
44 * T_GLOBAL_DEBUGGING_LEVEL = 1: all debugging turned on
45 */
46#define T_GLOBAL_DEBUGGING_LEVEL 0
47
48
49/**
50 * T_GLOBAL_LOGGING_LEVEL = 0: all logging turned off, logging macros undefined
51 * T_GLOBAL_LOGGING_LEVEL = 1: all logging turned on
52 */
53#define T_GLOBAL_LOGGING_LEVEL 1
54
55
56/**
57 * Standard wrapper around fprintf what will prefix the file name and line
58 * number to the line. Uses T_GLOBAL_DEBUGGING_LEVEL to control whether it is
59 * turned on or off.
60 *
61 * @param format_string
62 */
63#if T_GLOBAL_DEBUGGING_LEVEL > 0
64 #define T_DEBUG(format_string,...) \
65 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
66 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
67 }
68#else
69 #define T_DEBUG(format_string,...)
70#endif
71
72
73/**
74 * analagous to T_DEBUG but also prints the time
75 *
76 * @param string format_string input: printf style format string
77 */
78#if T_GLOBAL_DEBUGGING_LEVEL > 0
79 #define T_DEBUG_T(format_string,...) \
80 { \
81 if (T_GLOBAL_DEBUGGING_LEVEL > 0) { \
82 time_t now; \
83 char dbgtime[26] ; \
84 time(&now); \
85 ctime_r(&now, dbgtime); \
86 dbgtime[24] = '\0'; \
87 fprintf(stderr,"[%s,%d] [%s] " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
88 } \
89 }
90#else
91 #define T_DEBUG_T(format_string,...)
92#endif
93
94
95/**
96 * analagous to T_DEBUG but uses input level to determine whether or not the string
97 * should be logged.
98 *
99 * @param int level: specified debug level
100 * @param string format_string input: format string
101 */
102#define T_DEBUG_L(level, format_string,...) \
103 if ((level) > 0) { \
104 fprintf(stderr,"[%s,%d] " #format_string " \n", __FILE__, __LINE__,##__VA_ARGS__); \
105 }
106
107
108/**
109 * Explicit error logging. Prints time, file name and line number
110 *
111 * @param string format_string input: printf style format string
112 */
113#define T_ERROR(format_string,...) \
114 { \
115 time_t now; \
116 char dbgtime[26] ; \
117 time(&now); \
118 ctime_r(&now, dbgtime); \
119 dbgtime[24] = '\0'; \
120 fprintf(stderr,"[%s,%d] [%s] ERROR: " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
121 }
122
123
124/**
125 * Analagous to T_ERROR, additionally aborting the process.
126 * WARNING: macro calls abort(), ending program execution
127 *
128 * @param string format_string input: printf style format string
129 */
130#define T_ERROR_ABORT(format_string,...) \
131 { \
132 time_t now; \
133 char dbgtime[26] ; \
134 time(&now); \
135 ctime_r(&now, dbgtime); \
136 dbgtime[24] = '\0'; \
137 fprintf(stderr,"[%s,%d] [%s] ERROR: Going to abort " #format_string " \n", __FILE__, __LINE__,dbgtime,##__VA_ARGS__); \
138 exit(1); \
139 }
140
141
142/**
143 * Log input message
144 *
145 * @param string format_string input: printf style format string
146 */
147#if T_GLOBAL_LOGGING_LEVEL > 0
148 #define T_LOG_OPER(format_string,...) \
149 { \
150 if (T_GLOBAL_LOGGING_LEVEL > 0) { \
151 time_t now; \
152 char dbgtime[26] ; \
153 time(&now); \
154 ctime_r(&now, dbgtime); \
155 dbgtime[24] = '\0'; \
156 fprintf(stderr,"[%s] " #format_string " \n", dbgtime,##__VA_ARGS__); \
157 } \
158 }
159#else
160 #define T_LOG_OPER(format_string,...)
161#endif
162
163#endif // #ifndef _THRIFT_TLOGGING_H_