master
h 178 lines 7.91 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_ND_LOG_H
4 #define NETDATA_ND_LOG_H 1
5
6 # ifdef __cplusplus
7 extern "C" {
8 # endif
9
10 #include "../libnetdata.h"
11 #include "nd_log-common.h"
12 #include "nd_log-fatal.h"
13
14 #define ND_LOG_DEFAULT_THROTTLE_LOGS 1000
15 #define ND_LOG_DEFAULT_THROTTLE_PERIOD 60
16
17 void errno_clear(void);
18
19 void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting);
20 void nd_log_set_facility(const char *facility);
21 void nd_log_set_priority_level(const char *setting);
22 void nd_log_initialize_mutexes(void);
23 void nd_log_initialize(void);
24 void nd_log_reopen_log_files(bool log);
25 void chown_open_file(int fd, uid_t uid, gid_t gid);
26 void nd_log_chown_log_files(uid_t uid, gid_t gid);
27 void nd_log_set_flood_protection(size_t logs, time_t period);
28 void nd_log_initialize_for_external_plugins(const char *name);
29 void nd_log_reopen_log_files_for_spawn_server(const char *name);
30 bool nd_log_journal_socket_available(void);
31 ND_LOG_FIELD_ID nd_log_field_id_by_journal_name(const char *field, size_t len);
32 int nd_log_priority2id(const char *priority);
33 const char *nd_log_id2priority(ND_LOG_FIELD_PRIORITY priority);
34 const char *nd_log_method_for_external_plugins(const char *s);
35 ND_UUID nd_log_get_invocation_id(void);
36
37 typedef void (*log_event_t)(const char *filename, const char *function, const char *message, const char *errno_str, const char *stack_trace, long line);
38 void nd_log_register_fatal_hook_cb(log_event_t cb);
39
40 typedef void (*fatal_event_t)(void);
41 void nd_log_register_fatal_final_cb(fatal_event_t cb);
42
43 int nd_log_systemd_journal_fd(void);
44 int nd_log_health_fd(void);
45 int nd_log_collectors_fd(void);
46
47 typedef bool (*log_formatter_callback_t)(BUFFER *wb, void *data);
48
49 struct log_stack_entry {
50 ND_LOG_FIELD_ID id;
51 ND_LOG_STACK_FIELD_TYPE type;
52 bool set;
53 union {
54 const char *txt;
55 struct netdata_string *str;
56 BUFFER *bfr;
57 uint64_t u64;
58 int64_t i64;
59 double dbl;
60 const nd_uuid_t *uuid;
61 struct {
62 log_formatter_callback_t formatter;
63 void *formatter_data;
64 } cb;
65 };
66 };
67
68 #define ND_LOG_STACK _cleanup_(log_stack_pop) struct log_stack_entry
69 #define ND_LOG_STACK_PUSH(lgs) log_stack_push(lgs)
70
71 #define ND_LOG_FIELD_TXT(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_TXT, .txt = (value), .set = true, }
72 #define ND_LOG_FIELD_STR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_STR, .str = (value), .set = true, }
73 #define ND_LOG_FIELD_BFR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_BFR, .bfr = (value), .set = true, }
74 #define ND_LOG_FIELD_U64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_U64, .u64 = (value), .set = true, }
75 #define ND_LOG_FIELD_I64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_I64, .i64 = (value), .set = true, }
76 #define ND_LOG_FIELD_DBL(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_DBL, .dbl = (value), .set = true, }
77 #define ND_LOG_FIELD_CB(field, func, data) (struct log_stack_entry){ .id = (field), .type = NDFT_CALLBACK, .cb = { .formatter = (func), .formatter_data = (data) }, .set = true, }
78 #define ND_LOG_FIELD_UUID(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_UUID, .uuid = (value), .set = true, }
79 #define ND_LOG_FIELD_END() (struct log_stack_entry){ .id = NDF_STOP, .type = NDFT_UNSET, .set = false, }
80
81 void log_stack_pop(void *ptr);
82 void log_stack_push(struct log_stack_entry *lgs);
83
84 #define D_WEB_BUFFER (1ULL << 0)
85 #define D_WEB_CLIENT (1ULL << 1)
86 #define D_LISTENER (1ULL << 2)
87 #define D_WEB_DATA (1ULL << 3)
88 #define D_OPTIONS (1ULL << 4)
89 #define D_PROCNETDEV_LOOP (1ULL << 5)
90 #define D_RRD_STATS (1ULL << 6)
91 #define D_WEB_CLIENT_ACCESS (1ULL << 7)
92 #define D_TC_LOOP (1ULL << 8)
93 #define D_DEFLATE (1ULL << 9)
94 #define D_CONFIG (1ULL << 10)
95 #define D_PLUGINSD (1ULL << 11)
96 #define D_PROCFILE (1ULL << 12)
97 #define D_RRD_CALLS (1ULL << 13)
98 #define D_DICTIONARY (1ULL << 14)
99 #define D_CGROUP (1ULL << 15)
100 #define D_REGISTRY (1ULL << 16)
101 #define D_HEALTH (1ULL << 17)
102 #define D_LOCKS (1ULL << 18)
103 #define D_EXPORTING (1ULL << 19)
104 #define D_STATSD (1ULL << 20)
105 #define D_STREAM (1ULL << 21)
106 #define D_ANALYTICS (1ULL << 22)
107 #define D_RRDENGINE (1ULL << 23)
108 #define D_ACLK (1ULL << 24)
109 #define D_WEBSOCKET (1ULL << 25)
110 #define D_MCP (1ULL << 26)
111 #define D_SYSTEM (1ULL << 27)
112
113 extern uint64_t debug_flags;
114 extern const char *program_name;
115 extern int aclklog_enabled;
116
117 #define LOG_DATE_LENGTH 26
118 void log_date(char *buffer, size_t len, time_t now);
119
120 static inline void debug_dummy(void) {}
121
122 void nd_log_limits_reset(void);
123 void nd_log_limits_unlimited(void);
124
125 #define NDLP_INFO_STR "info"
126
127 #ifdef NETDATA_INTERNAL_CHECKS
128 #define netdata_log_debug(type, args...) do { if(unlikely(debug_flags & type)) netdata_logger(NDLS_DEBUG, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
129 #define internal_error(condition, args...) do { if(unlikely(condition)) netdata_logger(NDLS_DAEMON, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
130 #else
131 #define netdata_log_debug(type, args...) debug_dummy()
132 #define internal_error(args...) debug_dummy()
133 #endif
134
135 // ----------------------------------------------------------------------------
136 // normal logging
137
138 void netdata_logger(ND_LOG_SOURCES source, ND_LOG_FIELD_PRIORITY priority, const char *file, const char *function, unsigned long line, const char *fmt, ... ) PRINTFLIKE(6, 7);
139 #define nd_log(NDLS, NDLP, args...) netdata_logger(NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
140 #define nd_log_daemon(NDLP, args...) netdata_logger(NDLS_DAEMON, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
141 #define nd_log_collector(NDLP, args...) netdata_logger(NDLS_COLLECTORS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
142
143 #define netdata_log_info(args...) netdata_logger(NDLS_DAEMON, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
144 #define netdata_log_error(args...) netdata_logger(NDLS_DAEMON, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
145 #define collector_info(args...) netdata_logger(NDLS_COLLECTORS, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
146 #define collector_error(args...) netdata_logger(NDLS_COLLECTORS, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
147
148 #define log_aclk_message_bin(__data, __data_len, __tx, __mqtt_topic, __message_name) \
149 nd_log(NDLS_ACLK, NDLP_INFO, \
150 "direction:%s message:'%s' topic:'%s' json:'%.*s'", \
151 (__tx) ? "OUTGOING" : "INCOMING", __message_name, __mqtt_topic, (int)(__data_len), __data)
152
153 // ----------------------------------------------------------------------------
154 // logging with limits
155
156 typedef struct error_with_limit {
157 SPINLOCK spinlock;
158 time_t log_every;
159 size_t count;
160 time_t last_logged;
161 usec_t sleep_ut;
162 } ERROR_LIMIT;
163
164 #define nd_log_limit_static_global_var(var, log_every_secs, sleep_usecs) static ERROR_LIMIT var = { .spinlock = SPINLOCK_INITIALIZER, .log_every = (log_every_secs), .count = 0, .last_logged = 0, .sleep_ut = (sleep_usecs) }
165 #define nd_log_limit_static_thread_var(var, log_every_secs, sleep_usecs) static __thread ERROR_LIMIT var = { .last_logged = 0, .count = 0, .log_every = (log_every_secs), .sleep_ut = (sleep_usecs) }
166 void netdata_logger_with_limit(ERROR_LIMIT *erl, ND_LOG_SOURCES source, ND_LOG_FIELD_PRIORITY priority, const char *file, const char *function, unsigned long line, const char *fmt, ... ) PRINTFLIKE(7, 8);
167 #define nd_log_limit(erl, NDLS, NDLP, args...) netdata_logger_with_limit(erl, NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
168
169 // ----------------------------------------------------------------------------
170
171
172 #define error_report(x, args...) do { errno_clear(); netdata_log_error(x, ##args); } while(0)
173
174 # ifdef __cplusplus
175 }
176 # endif
177
178 #endif /* NETDATA_ND_LOG_H */