/src/dbus-broker/src/util/log.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | /* |
4 | | * Log Context |
5 | | */ |
6 | | |
7 | | #include <c-stdaux.h> |
8 | | #include <stdlib.h> |
9 | | #include <sys/mman.h> |
10 | | #include <sys/syslog.h> |
11 | | |
12 | | typedef struct Log Log; |
13 | | |
14 | | enum { |
15 | | _LOG_E_SUCCESS, |
16 | | |
17 | | LOG_E_OVERSIZED, |
18 | | LOG_E_TRUNCATED, |
19 | | }; |
20 | | |
21 | | enum { |
22 | | LOG_MODE_NONE, |
23 | | LOG_MODE_STDERR, |
24 | | LOG_MODE_JOURNAL, |
25 | | }; |
26 | | |
27 | | struct Log { |
28 | | int log_fd; |
29 | | unsigned short mode; |
30 | | bool consumed : 1; |
31 | | bool lossy : 1; |
32 | | uint64_t n_dropped; |
33 | | |
34 | | int error; |
35 | | int level; |
36 | | |
37 | | int mem_fd; |
38 | | void *map; |
39 | | size_t map_size; |
40 | | size_t offset; |
41 | | }; |
42 | | |
43 | 0 | #define LOG_NULL { \ |
44 | 0 | .log_fd = -1, \ |
45 | 0 | .mem_fd = -1, \ |
46 | 0 | .map = MAP_FAILED, \ |
47 | 0 | } |
48 | | |
49 | | /* log context */ |
50 | | |
51 | | void log_init(Log *log); |
52 | | void log_init_stderr(Log *log, int stderr_fd); |
53 | | void log_init_journal(Log *log, int journal_fd); |
54 | | void log_init_journal_consume(Log *log, int journal_fd); |
55 | | void log_deinit(Log *log); |
56 | | |
57 | | int log_get_fd(Log *log); |
58 | | void log_set_lossy(Log *log, bool lossy); |
59 | | |
60 | | int log_vcommitf(Log *log, const char *format, va_list args); |
61 | | |
62 | | void log_append(Log *log, const void *data, size_t n_data); |
63 | | void log_vappendf(Log *log, const char *format, va_list args); |
64 | | void log_append_common(Log *log, |
65 | | int level, |
66 | | int error, |
67 | | const char *id, |
68 | | const char *file, |
69 | | int line, |
70 | | const char *func); |
71 | | |
72 | | /* inline helpers */ |
73 | | |
74 | 0 | static inline int log_commitf(Log *log, const char *format, ...) { |
75 | 0 | va_list args; |
76 | 0 | int r; |
77 | 0 |
|
78 | 0 | va_start(args, format); |
79 | 0 | r = log_vcommitf(log, format, args); |
80 | 0 | va_end(args); |
81 | 0 |
|
82 | 0 | return r; |
83 | 0 | } Unexecuted instantiation: message.c:log_commitf Unexecuted instantiation: log.c:log_commitf |
84 | | |
85 | 0 | static inline int log_commit_silent(Log *log) { |
86 | 0 | return log_commitf(log, NULL); |
87 | 0 | } Unexecuted instantiation: message.c:log_commit_silent Unexecuted instantiation: log.c:log_commit_silent |
88 | | |
89 | 0 | static inline void log_appends(Log *log, const char *string) { |
90 | 0 | return log_append(log, string, strlen(string)); |
91 | 0 | } Unexecuted instantiation: message.c:log_appends Unexecuted instantiation: log.c:log_appends |
92 | | |
93 | 0 | static inline void log_appendf(Log *log, const char *format, ...) { |
94 | 0 | va_list args; |
95 | |
|
96 | 0 | va_start(args, format); |
97 | 0 | log_vappendf(log, format, args); |
98 | 0 | va_end(args); |
99 | 0 | } Unexecuted instantiation: message.c:log_appendf Unexecuted instantiation: log.c:log_appendf |
100 | | |
101 | | #define log_append_here(_log, _level, _r, _id) \ |
102 | | log_append_common((_log), (_level), (_r), (_id), __FILE__, __LINE__, __func__) |