My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
logging.h
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
35 #ifndef LOGGING_H_
36 #define LOGGING_H_
37 
38 #include <string.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <pthread.h>
42 
43 #define ANSI_COLOR_RED "\x1b[31m"
44 #define ANSI_COLOR_GREEN "\x1b[32m"
45 #define ANSI_COLOR_YELLOW "\x1b[33m"
46 #define ANSI_COLOR_BLUE "\x1b[34m"
47 #define ANSI_COLOR_RESET "\x1b[0m"
48 #define ANSI_COLOR_PURPLE "\x1b[35m"
49 
50 #define PICO_SUPPORT_NDEBUG
51 
53 #define LOG_FD stderr
54 
55 // Switch LOG_ALL definition to a 1 or 0. Makes for easier checking of the condition
56 // in log()
57 #ifndef LOG_ALL
58 #define LOG_ALL 0
59 #else
60 #undef LOG_ALL
61 #define LOG_ALL 1
62 #endif
63 
65 #define log_at_level(lvl_label, color, fd, fmt, ...)\
66  fprintf(fd, "" color "%lu:%s:%d:%s(): " lvl_label ": " fmt ANSI_COLOR_RESET "\n", \
67  pthread_self(), __FILE__, __LINE__, __func__, ##__VA_ARGS__)
68 
69 #ifndef log_debug
70 #ifdef LOG_DEBUG
71 
72 #define log_debug(fmt, ...)\
73  log_at_level("DEBUG", ANSI_COLOR_RESET, LOG_FD, fmt, ##__VA_ARGS__)
74 #else
75 
76 #define log_debug(...)
77 #endif
78 #define debug(fmt, ...) log_debug(__VA_ARGS__)
79 #endif
80 
81 #ifndef log_info
82 #ifdef LOG_INFO
83 #ifndef log_info
84 #define log_info(fmt, ...)\
85  log_at_level("INFO", ANSI_COLOR_GREEN, LOG_FD, fmt, ##__VA_ARGS__)
86 #endif
87 #else
88 #define log_info(fmt, ...)
89 #endif
90 #endif
91 
92 #ifndef log_error
93 #ifdef LOG_ERROR
94 #ifndef log_error
95 #define log_error(fmt, ...)\
96  log_at_level("ERROR", ANSI_COLOR_RED, LOG_FD, fmt, ##__VA_ARGS__)
97 #endif
98 #define log_perror(fmt, ...)\
99  log_at_level("ERROR", ANSI_COLOR_RED, LOG_FD, fmt ": %s", ##__VA_ARGS__, strerror(errno))
100 #else
101 #define log_error(fmt, ...)
102 #define log_perror(fmt, ...)
103 #endif
104 #endif
105 
106 #ifndef log_warn
107 #ifdef LOG_WARN
108 #ifndef log_warn
109 #define log_warn(fmt, ...)\
110  log_at_level("WARN", ANSI_COLOR_YELLOW, LOG_FD, fmt, ##__VA_ARGS__)
111 #endif
112 #else
113 #define log_warn(fmt, ...)
114 #endif
115 #endif
116 
117 #ifndef log_critical
118 #if defined(LOG_CRITICAL) && LOG_CRITICAL
119 #ifndef log_critical
120 #define log_critical(fmt, ...)\
121  log_at_level("CRITICAL", ANSI_COLOR_PURPLE, LOG_FD, fmt, ##__VA_ARGS__)
122 #endif
123 #else
124 #define log_critical(fmt, ...)
125 #endif
126 #endif
127 
128 #ifndef log
129 #ifdef LOG_CUSTOM
130 
131 #define LOG_CUSTOM_STRINGIFY(val) "" #val
132 #define LOG_CUSTOM_STRINGIFY2(val) LOG_CUSTOM_STRINGIFY(val)
133 
136 #define log(level, fmt, ...)\
137  do { \
138  if ((LOG_ALL || strcmp( "" #level, LOG_CUSTOM_STRINGIFY(level))) \
139  && !strcmp( "NO_" #level, LOG_CUSTOM_STRINGIFY2(NO_##level))) { \
140  log_at_level(#level, ANSI_COLOR_BLUE, LOG_FD, fmt, ##__VA_ARGS__); \
141  } \
142  } while (0)
143 #else
144 
147 #define log(level, fmt, ...)
148 #endif
149 #endif
150 
151 #ifndef log_profile
152 #if defined(DATA_PROFILING_PRINT) && DATA_PROFILING_PRINT
153 #define log_profile(fmt, ...) \
154  log_at_level("DATA_PROFILE", ANSI_COLOR_RESET, LOG_FD, fmt, ##__VA_ARGS__)
155 #else
156 #define log_profile(...)
157 #endif
158 #endif
159 
160 #ifndef tcp_dbg
161 #if defined(LOG_TCP_DBG) && LOG_TCP_DBG
162 #define tcp_dbg(fmt, ...) \
163  log_at_level("TCP_DEBUG", ANSI_COLOR_RESET, LOG_FD, fmt, ##__VA_ARGS__)
164 #else
165 #define tcp_dbg(...)
166 #endif
167 #endif
168 
169 #endif