-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.h
96 lines (82 loc) · 2.52 KB
/
log.h
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
// Copyright Contributors to the L3AF Project.
// SPDX-License-Identifier: GPL-2.0
/*
* file log.h
*
* brief error reporting
*
*/
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TIMESTAMP_LEN 64
FILE *info;
typedef enum log_level {
LOG_OFF = 0,
LOG_DEBUG = 1,
LOG_INFO = 2,
LOG_WARN = 3,
LOG_ERR = 4,
LOG_CRIT = 5
} log_level_e;
#define LOG_DEBUG_STR "DEBUG"
#define LOG_INFO_STR "INFO"
#define LOG_WARN_STR "WARN"
#define LOG_ERR_STR "ERR"
#define LOG_CRIT_STR "CRIT"
int verbosity ;
#define log_debug(...) { \
if (verbosity != LOG_OFF && verbosity <= LOG_DEBUG) { \
char log_ts[TIMESTAMP_LEN]; \
log_timestamp(log_ts); \
fprintf(info, "%s: ", log_ts); \
fprintf(info, "%s: %s: %d: ", LOG_DEBUG_STR, __FUNCTION__, __LINE__); \
fprintf(info, __VA_ARGS__); \
fprintf(info, "\n"); \
} \
}
#define log_info(...) { \
if (verbosity != LOG_OFF && verbosity <= LOG_INFO) { \
char log_ts[TIMESTAMP_LEN]; \
log_timestamp(log_ts); \
fprintf(info, "%s: ", log_ts); \
fprintf(info, "%s: %s: %d: ", LOG_INFO_STR, __FUNCTION__, __LINE__); \
fprintf(info, __VA_ARGS__); \
fprintf(info, "\n"); \
} \
}
#define log_warn(...) { \
if (verbosity != LOG_OFF && verbosity <= LOG_WARN) { \
char log_ts[TIMESTAMP_LEN]; \
log_timestamp(log_ts); \
fprintf(info, "%s: ", log_ts); \
fprintf(info, "%s: %s: %d: ", LOG_WARN_STR, __FUNCTION__, __LINE__); \
fprintf(info, __VA_ARGS__); \
fprintf(info, "\n"); \
} \
}
#define log_err(...) { \
if (verbosity != LOG_OFF && verbosity <= LOG_ERR) { \
char log_ts[TIMESTAMP_LEN]; \
log_timestamp(log_ts); \
fprintf(info, "%s: ", log_ts); \
fprintf(info, "%s: %s: %d: ", LOG_ERR_STR, __FUNCTION__, __LINE__); \
fprintf(info, __VA_ARGS__); \
fprintf(info, "\n"); \
} \
}
#define log_crit(...) { \
if (verbosity != LOG_OFF && verbosity <= LOG_CRIT) { \
char log_ts[TIMESTAMP_LEN]; \
log_timestamp(log_ts); \
fprintf(info, "%s: ", log_ts); \
fprintf(info, "%s: %s: %d: ", LOG_CRIT_STR, __FUNCTION__, __LINE__); \
fprintf(info, __VA_ARGS__); \
fprintf(info, "\n"); \
} \
}
void log_timestamp(char *log_ts);
#endif /* LOG_H */