-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing.h
118 lines (98 loc) · 3.77 KB
/
printing.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef SRD_PRINTING_H
#define SRD_PRINTING_H
#include <pthread.h>
/*
* Log level of each target.
*/
enum loglevel
{
LOGLEVEL_DEBUG, // Everything
LOGLEVEL_INFO, // When a ping fails, when an action is performed
LOGLEVEL_QUIET, // reserved for later
LOGLEVEL_ERROR, // Nothing connection related, only when started and when stopped
INVALID_LOGLEVEL, // This should never happen
};
typedef struct logger_t
{
// for avoiding race conditions when writing
pthread_mutex_t *stdout_mut;
// the current loglevel
enum loglevel *level;
// prefix for each message
char* prefix;
} logger_t;
/* Define some macros to print inside a mutex */
#define sprint(logger, ...) \
if (pthread_mutex_lock(logger->stdout_mut) != 0) \
{ \
fprintf(stdout, "Unable to get lock: "); \
fprintf(stdout, "%s", logger->prefix); \
fprintf(stdout, __VA_ARGS__); \
} \
else \
{ \
fprintf(stdout, "%s", logger->prefix); \
fprintf(stdout, __VA_ARGS__); \
pthread_mutex_unlock(logger->stdout_mut); \
}
#define sprint_raw(logger, ...) \
if (pthread_mutex_lock(logger->stdout_mut) != 0) \
{ \
fprintf(stdout, "Unable to get lock: \n"); \
fprintf(stdout, __VA_ARGS__); \
} \
else \
{ \
fprintf(stdout, __VA_ARGS__); \
pthread_mutex_unlock(logger->stdout_mut); \
}
#define uprint(logger, ...) \
fprintf(stdout, "%s", logger->prefix); \
fprintf(stdout, __VA_ARGS__);
#define sprint_debug(logger, ...) \
if (*logger->level <= LOGLEVEL_DEBUG) \
{ \
sprint(logger, "DEBUG: " __VA_ARGS__); \
}
#define sprint_debug_raw(logger, ...) \
if (*logger->level <= LOGLEVEL_DEBUG) \
{ \
sprint_raw(logger, __VA_ARGS__); \
}
#define sprint_info(logger, ...) \
if (*logger->level <= LOGLEVEL_INFO) \
{ \
sprint(logger, __VA_ARGS__); \
}
#define sprint_quiet(logger, ...) \
if (*logger->level <= LOGLEVEL_QUIET) \
{ \
sprint(logger, __VA_ARGS__); \
}
#define sprint_error(logger, ...) \
if (*logger->level <= LOGLEVEL_ERROR) \
{ \
sprint(logger, "ERROR: " __VA_ARGS__); \
}
#define print_debug(logger, ...) \
if (*logger->level <= LOGLEVEL_DEBUG) \
{ \
uprint(logger, "DEBUG: " __VA_ARGS__); \
}
#define print_info(logger, ...) \
if (*logger->level <= LOGLEVEL_INFO) \
{ \
uprint(logger, __VA_ARGS__); \
}
#define print_quiet(logger, ...) \
if (*logger->level <= LOGLEVEL_QUIET) \
{ \
uprint(logger, __VA_ARGS__); \
}
#define print_error(logger, ...) \
if (*logger->level <= LOGLEVEL_ERROR) \
{ \
uprint(logger, "ERROR: " __VA_ARGS__); \
}
enum loglevel to_loglevel(const char* str_loglevel);
#endif