aboutsummaryrefslogtreecommitdiffstats
path: root/log.h
blob: 6c7a8607a432b57f01b2754087f5d4d7a62cff17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#ifndef __AGNI_LOG_H
#define __AGNI_LOG_H

#include <unistd.h>
#include <stdio.h>

//bunch of macroses that will just disable log code and save some binary space
#define LOG_DEBUG_ENABLE
#define LOG_ERROR_ENABLE
#define LOG_WARNING_ENABLE
#define LOG_FATAL_ENABLE
#define LOG_INFO_ENABLE

#define SUB_MEM
#define SUB_CMDS
#define SUB_MSG
#define SUB_NET

//log level
#define LOG_LEVEL_NONE    0   
#define LOG_LEVEL_FATAL   1
#define LOG_LEVEL_ERROR   2
#define LOG_LEVEL_WARNING 3
#define LOG_LEVEL_INFO    4
#define LOG_LEVEL_DEBUG   5

//logging subsystem
#define LOG_SUB_NONE  0
#define LOG_SUB_MEM   1
#define LOG_SUB_CMDS  2
#define LOG_SUB_MSG   3
#define LOG_SUB_NET   4

//loging location
#define LOG_LOC_STDIO   0
#define LOG_LOC_FILE    1

typedef struct logging_t
{
	int disabled;
	int sub;
	int level;
	int location;
	FILE *fd;
	char *fname;
} logging_t;

#define NUM_OF_LOG 5
logging_t __logs[NUM_OF_LOG]; //<-be carefull with this number woop woop

int log_init();
int log_register_sub(int sub, int level, int type, char *location);

#define LOG_PRINTF fprintf
#define LOG_COLORIZE
#define LOG_PRINT_LINENUM
#define LOG_PRINT_FILENAME
#define LOG_PRINT_DEBUG

//use color
#ifdef LOG_COLORIZE
	#define LOG_D_COLOR "7;32m"
	#define LOG_D_COLOR_S "\033[" LOG_D_COLOR
	#define LOG_D_COLOR_E "\033[0m"
	#define LOG_E_COLOR "0;31m"
	#define LOG_E_COLOR_S "\033[" LOG_E_COLOR
	#define LOG_E_COLOR_E "\033[0m"
	#define LOG_W_COLOR "0;35m"
	#define LOG_W_COLOR_S "\033[" LOG_W_COLOR
	#define LOG_W_COLOR_E "\033[0m"
	#define LOG_F_COLOR "5;31m"
	#define LOG_F_COLOR_S "\033[" LOG_F_COLOR
	#define LOG_F_COLOR_E "\033[0m"
	#define LOG_I_COLOR "0;36m"
	#define LOG_I_COLOR_S "\033[" LOG_I_COLOR
	#define LOG_I_COLOR_E "\033[0m"
#else
	#define LOG_D_COLOR
	#define LOG_D_COLOR_S
	#define LOG_D_COLOR_E
	#define LOG_E_COLOR
	#define LOG_E_COLOR_S
	#define LOG_E_COLOR_E
	#define LOG_W_COLOR
	#define LOG_W_COLOR_S
	#define LOG_W_COLOR_E
	#define LOG_F_COLOR
	#define LOG_F_COLOR_S
	#define LOG_F_COLOR_E
	#define LOG_I_COLOR
	#define LOG_I_COLOR_S
	#define LOG_I_COLOR_E
#endif

//print debug line
#ifdef LOG_PRINT_LINENUM
	#define LOG_PRINT_LINE_F ":%d "
	#define LOG_PRINT_LINE_D __LINE__
#else
	#define LOG_PRINT_LINE_F "%s"
	#define LOG_PRINT_LINE_D " "
#endif

//print
#ifdef LOG_PRINT_FILENAME
	#define LOG_PRINT_FILE_F "%s"
	#define LOG_PRINT_FILE_D __FILE__
#else
	#define LOG_PRINT_FILE_F "%s"
	#define LOG_PRINT_FILE_D " "
#endif

//print debug string
#ifdef LOG_PRINT_DEBUG
	#define LOG_PRINT_DEBUG_F   "DEBUG: "
	#define LOG_PRINT_WARNING_F "WARN:  "
	#define LOG_PRINT_FATAL_F   "FATAL: "
	#define LOG_PRINT_INFO_F    "INFO:  "
	#define LOG_PRINT_ERROR_F   "ERROR: "
#else
	#define LOG_PRINT_DEBUG_F ""
	#define LOG_PRINT_WARNING_F ""
	#define LOG_PRINT_FATAL_F ""
	#define LOG_PRINT_ERROR_F ""
	#define LOG_PRINT_INFO_F ""
#endif

#define LOG_INFO( fd, format, args ... ) LOG_PRINTF( fd, LOG_I_COLOR_S LOG_PRINT_INFO_F \
	LOG_PRINT_FILE_F LOG_PRINT_LINE_F format LOG_I_COLOR_E, LOG_PRINT_FILE_D, \
	LOG_PRINT_LINE_D, ##args);

#define LOG_DEBUG( fd, format, args ... ) LOG_PRINTF( fd, LOG_D_COLOR_S LOG_PRINT_DEBUG_F \
	LOG_PRINT_FILE_F LOG_PRINT_LINE_F format LOG_D_COLOR_E, LOG_PRINT_FILE_D, \
	LOG_PRINT_LINE_D, ##args);

#define LOG_ERROR( fd, format, args ... ) LOG_PRINTF( fd, LOG_E_COLOR_S LOG_PRINT_ERROR_F \
	LOG_PRINT_FILE_F LOG_PRINT_LINE_F format LOG_E_COLOR_E, LOG_PRINT_FILE_D, \
	LOG_PRINT_LINE_D, ##args);

#define LOG_WARNING( fd, format, args ... ) LOG_PRINTF( fd, LOG_W_COLOR_S LOG_PRINT_WARNING_F \
	LOG_PRINT_FILE_F LOG_PRINT_LINE_F format LOG_W_COLOR_E, LOG_PRINT_FILE_D, \
	LOG_PRINT_LINE_D, ##args);

#define LOG_FATAL( fd, format, args ... ) LOG_PRINTF( fd, LOG_F_COLOR_S LOG_PRINT_FATAL_F \
	LOG_PRINT_FILE_F LOG_PRINT_LINE_F format LOG_F_COLOR_E, LOG_PRINT_FILE_D, \
	LOG_PRINT_LINE_D, ##args);

//------------------------------------------------------------------------------
#ifdef LOG_INFO_ENABLE
	#define LOG_NONE_I(format,args ... ) {\
			if (0 == __logs[LOG_SUB_NONE].disabled){\
			if (__logs[LOG_SUB_NONE].level >= LOG_LEVEL_INFO){\
			if (__logs[LOG_SUB_NONE].location == LOG_LOC_STDIO){\
				LOG_INFO(stdout, format, ##args );\
			} else {\
				LOG_INFO(__logs[LOG_SUB_NONE].fd, format, ##args );\
			}}}\
	}
	#ifdef SUB_MEM
		#define LOG_MEM_I(format,args ... ) {\
			if (0 == __logs[LOG_SUB_MEM].disabled){\
			if (__logs[LOG_SUB_MEM].level >= LOG_LEVEL_INFO){\
			if (__logs[LOG_SUB_MEM].location == LOG_LOC_STDIO){\
				LOG_INFO(stdout, "MEM-" );\
				LOG_INFO(stdout, format, ##args );\
			} else {\
				LOG_INFO(stdout, "MEM-" );\
				LOG_INFO(__logs[LOG_SUB_MEM].fd, format, ##args );\
			}}}\
		}
	#else
		#define LOG_MEM_I(format,args ...) {}
	#endif
#else
	LOG_NONE_I(format,args ...) {}
#endif

//------------------------------------------------------------------------------
#ifdef LOG_INFO_ENABLE
	#define LOG_NONE_D(format,args ... ) {\
			if (0 == __logs[LOG_SUB_NONE].disabled){\
			if (__logs[LOG_SUB_NONE].level >= LOG_LEVEL_DEBUG){\
			if (__logs[LOG_SUB_NONE].location == LOG_LOC_STDIO){\
				LOG_DEBUG(stdout, format, ##args );\
			} else {\
				LOG_DEBUG(__logs[LOG_SUB_NONE].fd, format, ##args );\
			}}}\
	}
	#ifdef SUB_MEM
		#define LOG_MEM_D(format,args ... ) {\
			if (0 == __logs[LOG_SUB_MEM].disabled){\
			if (__logs[LOG_SUB_MEM].level >= LOG_LEVEL_DEBUG){\
			if (__logs[LOG_SUB_MEM].location == LOG_LOC_STDIO){\
				LOG_DEBUG(stdout, "MEM-" );\
				LOG_DEBUG(stdout, format, ##args );\
			} else {\
				LOG_DEBUG(__logs[LOG_SUB_MEM].fd, "MEM-" );\
				LOG_DEBUG(__logs[LOG_SUB_MEM].fd, format, ##args );\
			}}}\
		}
	#else
		#define LOG_MEM_D(format,args ...) {}
	#endif
#else
	LOG_NONE_D(format,args ...) {}
#endif

//------------------------------------------------------------------------------
#ifdef LOG_INFO_ENABLE
	#define LOG_NONE_E(format,args ... ) {\
			if (0 == __logs[LOG_SUB_NONE].disabled){\
			if (__logs[LOG_SUB_NONE].level >= LOG_LEVEL_ERROR){\
			if (__logs[LOG_SUB_NONE].location == LOG_LOC_STDIO){\
				LOG_ERROR(stdout, format, ##args );\
			} else {\
				LOG_ERROR(__logs[LOG_SUB_NONE].fd, format, ##args );\
			}}}\
	}
	#ifdef SUB_MEM
		#define LOG_MEM_E(format,args ... ) {\
			if (0 == __logs[LOG_SUB_MEM].disabled){\
			if (__logs[LOG_SUB_MEM].level >= LOG_LEVEL_ERROR){\
			if (__logs[LOG_SUB_MEM].location == LOG_LOC_STDIO){\
				LOG_ERROR(stdout, "MEM-" );\
				LOG_ERROR(stdout, format, ##args );\
			} else {\
				LOG_ERROR(__logs[LOG_SUB_MEM].fd, "MEM-" );\
				LOG_ERROR(__logs[LOG_SUB_MEM].fd, format, ##args );\
			}}}\
		}
	#else
		#define LOG_MEM_E(format,args ...) {}
	#endif
#else
	LOG_NONE_E(format,args ...) {}
#endif

//------------------------------------------------------------------------------
#ifdef LOG_INFO_ENABLE
	#define LOG_NONE_W(format,args ... ) {\
			if (0 == __logs[LOG_SUB_NONE].disabled){\
			if (__logs[LOG_SUB_NONE].level >= LOG_LEVEL_WARNING){\
			if (__logs[LOG_SUB_NONE].location == LOG_LOC_STDIO){\
				LOG_WARNING(stdout, format, ##args );\
			} else {\
				LOG_WARNING(__logs[LOG_SUB_NONE].fd, format, ##args );\
			}}}\
	}
	#ifdef SUB_MEM
		#define LOG_MEM_W(format,args ... ) {\
			if (0 == __logs[LOG_SUB_MEM].disabled){\
			if (__logs[LOG_SUB_MEM].level >= LOG_LEVEL_WARNING){\
			if (__logs[LOG_SUB_MEM].location == LOG_LOC_STDIO){\
				LOG_WARNING(stdout, "MEM-" );\
				LOG_WARNING(stdout, format, ##args );\
			} else {\
				LOG_WARNING(__logs[LOG_SUB_MEM].fd, "MEM-" );\
				LOG_WARNING(__logs[LOG_SUB_MEM].fd, format, ##args );\
			}}}\
		}
	#else
		#define LOG_MEM_W(format,args ...) {}
	#endif
#else
	LOG_NONE_W(format,args ...) {}
#endif

//------------------------------------------------------------------------------
#ifdef LOG_INFO_ENABLE
	#define LOG_NONE_F(format,args ... ) {\
			if (0 == __logs[LOG_SUB_NONE].disabled){\
			if (__logs[LOG_SUB_NONE].level >= LOG_LEVEL_FATAL){\
			if (__logs[LOG_SUB_NONE].location == LOG_LOC_STDIO){\
				LOG_FATAL(stdout, format, ##args );\
			} else {\
				LOG_FATAL(__logs[LOG_SUB_NONE].fd, format, ##args );\
			}}}\
	}
	#ifdef SUB_MEM
		#define LOG_MEM_F(format,args ... ) {\
			if (0 == __logs[LOG_SUB_MEM].disabled){\
			if (__logs[LOG_SUB_MEM].level >= LOG_LEVEL_FATAL){\
			if (__logs[LOG_SUB_MEM].location == LOG_LOC_STDIO){\
				LOG_FATAL(stdout, "MEM-" );\
				LOG_FATAL(stdout, format, ##args );\
			} else {\
				LOG_FATAL(__logs[LOG_SUB_MEM].fd, "MEM-" );\
				LOG_FATAL(__logs[LOG_SUB_MEM].fd, format, ##args );\
			}}}\
		}
	#else
		#define LOG_MEM_F(format,args ...) {}
	#endif
#else
	LOG_NONE_f(format,args ...) {}
#endif

#endif