log.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_LOG_H
  3. #define NETDATA_LOG_H 1
  4. # ifdef __cplusplus
  5. extern "C" {
  6. # endif
  7. #include "../libnetdata.h"
  8. #define ND_LOG_DEFAULT_THROTTLE_LOGS 1000
  9. #define ND_LOG_DEFAULT_THROTTLE_PERIOD 60
  10. typedef enum __attribute__((__packed__)) {
  11. NDLS_UNSET = 0, // internal use only
  12. NDLS_ACCESS, // access.log
  13. NDLS_ACLK, // aclk.log
  14. NDLS_COLLECTORS, // collectors.log
  15. NDLS_DAEMON, // error.log
  16. NDLS_HEALTH, // health.log
  17. NDLS_DEBUG, // debug.log
  18. // terminator
  19. _NDLS_MAX,
  20. } ND_LOG_SOURCES;
  21. typedef enum __attribute__((__packed__)) {
  22. NDLP_EMERG = LOG_EMERG,
  23. NDLP_ALERT = LOG_ALERT,
  24. NDLP_CRIT = LOG_CRIT,
  25. NDLP_ERR = LOG_ERR,
  26. NDLP_WARNING = LOG_WARNING,
  27. NDLP_NOTICE = LOG_NOTICE,
  28. NDLP_INFO = LOG_INFO,
  29. NDLP_DEBUG = LOG_DEBUG,
  30. } ND_LOG_FIELD_PRIORITY;
  31. typedef enum __attribute__((__packed__)) {
  32. // KEEP THESE IN THE SAME ORDER AS in thread_log_fields (log.c)
  33. // so that it easy to audit for missing fields
  34. NDF_STOP = 0,
  35. NDF_TIMESTAMP_REALTIME_USEC, // the timestamp of the log message - added automatically
  36. NDF_SYSLOG_IDENTIFIER, // the syslog identifier of the application - added automatically
  37. NDF_LOG_SOURCE, // DAEMON, COLLECTORS, HEALTH, ACCESS, ACLK - set at the log call
  38. NDF_PRIORITY, // the syslog priority (severity) - set at the log call
  39. NDF_ERRNO, // the ERRNO at the time of the log call - added automatically
  40. NDF_INVOCATION_ID, // the INVOCATION_ID of Netdata - added automatically
  41. NDF_LINE, // the source code file line number - added automatically
  42. NDF_FILE, // the source code filename - added automatically
  43. NDF_FUNC, // the source code function - added automatically
  44. NDF_TID, // the thread ID of the thread logging - added automatically
  45. NDF_THREAD_TAG, // the thread tag of the thread logging - added automatically
  46. NDF_MESSAGE_ID, // for specific events
  47. NDF_MODULE, // for internal plugin module, all other get the NDF_THREAD_TAG
  48. NDF_NIDL_NODE, // the node / rrdhost currently being worked
  49. NDF_NIDL_INSTANCE, // the instance / rrdset currently being worked
  50. NDF_NIDL_CONTEXT, // the context of the instance currently being worked
  51. NDF_NIDL_DIMENSION, // the dimension / rrddim currently being worked
  52. // web server, aclk and stream receiver
  53. NDF_SRC_TRANSPORT, // the transport we received the request, one of: http, https, pluginsd
  54. // Netdata Cloud Related
  55. NDF_ACCOUNT_ID,
  56. NDF_USER_NAME,
  57. NDF_USER_ROLE,
  58. // web server and stream receiver
  59. NDF_SRC_IP, // the streaming / web server source IP
  60. NDF_SRC_PORT, // the streaming / web server source Port
  61. NDF_SRC_FORWARDED_HOST,
  62. NDF_SRC_FORWARDED_FOR,
  63. NDF_SRC_CAPABILITIES, // the stream receiver capabilities
  64. // stream sender (established links)
  65. NDF_DST_TRANSPORT, // the transport we send the request, one of: http, https
  66. NDF_DST_IP, // the destination streaming IP
  67. NDF_DST_PORT, // the destination streaming Port
  68. NDF_DST_CAPABILITIES, // the destination streaming capabilities
  69. // web server, aclk and stream receiver
  70. NDF_REQUEST_METHOD, // for http like requests, the http request method
  71. NDF_RESPONSE_CODE, // for http like requests, the http response code, otherwise a status string
  72. // web server (all), aclk (queries)
  73. NDF_CONNECTION_ID, // the web server connection ID
  74. NDF_TRANSACTION_ID, // the web server and API transaction ID
  75. NDF_RESPONSE_SENT_BYTES, // for http like requests, the response bytes
  76. NDF_RESPONSE_SIZE_BYTES, // for http like requests, the uncompressed response size
  77. NDF_RESPONSE_PREPARATION_TIME_USEC, // for http like requests, the preparation time
  78. NDF_RESPONSE_SENT_TIME_USEC, // for http like requests, the time to send the response back
  79. NDF_RESPONSE_TOTAL_TIME_USEC, // for http like requests, the total time to complete the response
  80. // health alerts
  81. NDF_ALERT_ID,
  82. NDF_ALERT_UNIQUE_ID,
  83. NDF_ALERT_EVENT_ID,
  84. NDF_ALERT_TRANSITION_ID,
  85. NDF_ALERT_CONFIG_HASH,
  86. NDF_ALERT_NAME,
  87. NDF_ALERT_CLASS,
  88. NDF_ALERT_COMPONENT,
  89. NDF_ALERT_TYPE,
  90. NDF_ALERT_EXEC,
  91. NDF_ALERT_RECIPIENT,
  92. NDF_ALERT_DURATION,
  93. NDF_ALERT_VALUE,
  94. NDF_ALERT_VALUE_OLD,
  95. NDF_ALERT_STATUS,
  96. NDF_ALERT_STATUS_OLD,
  97. NDF_ALERT_SOURCE,
  98. NDF_ALERT_UNITS,
  99. NDF_ALERT_SUMMARY,
  100. NDF_ALERT_INFO,
  101. NDF_ALERT_NOTIFICATION_REALTIME_USEC,
  102. // NDF_ALERT_FLAGS,
  103. // put new items here
  104. // leave the request URL and the message last
  105. NDF_REQUEST, // the request we are currently working on
  106. NDF_MESSAGE, // the log message, if any
  107. // terminator
  108. _NDF_MAX,
  109. } ND_LOG_FIELD_ID;
  110. typedef enum __attribute__((__packed__)) {
  111. NDFT_UNSET = 0,
  112. NDFT_TXT,
  113. NDFT_STR,
  114. NDFT_BFR,
  115. NDFT_U64,
  116. NDFT_I64,
  117. NDFT_DBL,
  118. NDFT_UUID,
  119. NDFT_CALLBACK,
  120. } ND_LOG_STACK_FIELD_TYPE;
  121. void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting);
  122. void nd_log_set_facility(const char *facility);
  123. void nd_log_set_priority_level(const char *setting);
  124. void nd_log_initialize(void);
  125. void nd_log_reopen_log_files(void);
  126. void chown_open_file(int fd, uid_t uid, gid_t gid);
  127. void nd_log_chown_log_files(uid_t uid, gid_t gid);
  128. void nd_log_set_flood_protection(size_t logs, time_t period);
  129. void nd_log_initialize_for_external_plugins(const char *name);
  130. void nd_log_set_thread_source(ND_LOG_SOURCES source);
  131. bool nd_log_journal_socket_available(void);
  132. ND_LOG_FIELD_ID nd_log_field_id_by_name(const char *field, size_t len);
  133. int nd_log_priority2id(const char *priority);
  134. const char *nd_log_id2priority(ND_LOG_FIELD_PRIORITY priority);
  135. const char *nd_log_method_for_external_plugins(const char *s);
  136. int nd_log_health_fd(void);
  137. typedef bool (*log_formatter_callback_t)(BUFFER *wb, void *data);
  138. struct log_stack_entry {
  139. ND_LOG_FIELD_ID id;
  140. ND_LOG_STACK_FIELD_TYPE type;
  141. bool set;
  142. union {
  143. const char *txt;
  144. struct netdata_string *str;
  145. BUFFER *bfr;
  146. uint64_t u64;
  147. int64_t i64;
  148. double dbl;
  149. const uuid_t *uuid;
  150. struct {
  151. log_formatter_callback_t formatter;
  152. void *formatter_data;
  153. } cb;
  154. };
  155. };
  156. #define ND_LOG_STACK _cleanup_(log_stack_pop) struct log_stack_entry
  157. #define ND_LOG_STACK_PUSH(lgs) log_stack_push(lgs)
  158. #define ND_LOG_FIELD_TXT(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_TXT, .txt = (value), .set = true, }
  159. #define ND_LOG_FIELD_STR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_STR, .str = (value), .set = true, }
  160. #define ND_LOG_FIELD_BFR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_BFR, .bfr = (value), .set = true, }
  161. #define ND_LOG_FIELD_U64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_U64, .u64 = (value), .set = true, }
  162. #define ND_LOG_FIELD_I64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_I64, .i64 = (value), .set = true, }
  163. #define ND_LOG_FIELD_DBL(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_DBL, .dbl = (value), .set = true, }
  164. #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, }
  165. #define ND_LOG_FIELD_UUID(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_UUID, .uuid = (value), .set = true, }
  166. #define ND_LOG_FIELD_END() (struct log_stack_entry){ .id = NDF_STOP, .type = NDFT_UNSET, .set = false, }
  167. void log_stack_pop(void *ptr);
  168. void log_stack_push(struct log_stack_entry *lgs);
  169. #define D_WEB_BUFFER 0x0000000000000001
  170. #define D_WEB_CLIENT 0x0000000000000002
  171. #define D_LISTENER 0x0000000000000004
  172. #define D_WEB_DATA 0x0000000000000008
  173. #define D_OPTIONS 0x0000000000000010
  174. #define D_PROCNETDEV_LOOP 0x0000000000000020
  175. #define D_RRD_STATS 0x0000000000000040
  176. #define D_WEB_CLIENT_ACCESS 0x0000000000000080
  177. #define D_TC_LOOP 0x0000000000000100
  178. #define D_DEFLATE 0x0000000000000200
  179. #define D_CONFIG 0x0000000000000400
  180. #define D_PLUGINSD 0x0000000000000800
  181. #define D_CHILDS 0x0000000000001000
  182. #define D_EXIT 0x0000000000002000
  183. #define D_CHECKS 0x0000000000004000
  184. #define D_NFACCT_LOOP 0x0000000000008000
  185. #define D_PROCFILE 0x0000000000010000
  186. #define D_RRD_CALLS 0x0000000000020000
  187. #define D_DICTIONARY 0x0000000000040000
  188. #define D_MEMORY 0x0000000000080000
  189. #define D_CGROUP 0x0000000000100000
  190. #define D_REGISTRY 0x0000000000200000
  191. #define D_VARIABLES 0x0000000000400000
  192. #define D_HEALTH 0x0000000000800000
  193. #define D_CONNECT_TO 0x0000000001000000
  194. #define D_RRDHOST 0x0000000002000000
  195. #define D_LOCKS 0x0000000004000000
  196. #define D_EXPORTING 0x0000000008000000
  197. #define D_STATSD 0x0000000010000000
  198. #define D_POLLFD 0x0000000020000000
  199. #define D_STREAM 0x0000000040000000
  200. #define D_ANALYTICS 0x0000000080000000
  201. #define D_RRDENGINE 0x0000000100000000
  202. #define D_ACLK 0x0000000200000000
  203. #define D_REPLICATION 0x0000002000000000
  204. #define D_SYSTEM 0x8000000000000000
  205. extern uint64_t debug_flags;
  206. extern const char *program_name;
  207. #ifdef ENABLE_ACLK
  208. extern int aclklog_enabled;
  209. #endif
  210. #define LOG_DATE_LENGTH 26
  211. void log_date(char *buffer, size_t len, time_t now);
  212. static inline void debug_dummy(void) {}
  213. void nd_log_limits_reset(void);
  214. void nd_log_limits_unlimited(void);
  215. #define NDLP_INFO_STR "info"
  216. #ifdef NETDATA_INTERNAL_CHECKS
  217. #define netdata_log_debug(type, args...) do { if(unlikely(debug_flags & type)) netdata_logger(NDLS_DEBUG, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  218. #define internal_error(condition, args...) do { if(unlikely(condition)) netdata_logger(NDLS_DAEMON, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  219. #define internal_fatal(condition, args...) do { if(unlikely(condition)) netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  220. #else
  221. #define netdata_log_debug(type, args...) debug_dummy()
  222. #define internal_error(args...) debug_dummy()
  223. #define internal_fatal(args...) debug_dummy()
  224. #endif
  225. #define fatal(args...) netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, ##args)
  226. #define fatal_assert(expr) ((expr) ? (void)(0) : netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, "Assertion `%s' failed", #expr))
  227. // ----------------------------------------------------------------------------
  228. // normal logging
  229. 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);
  230. #define nd_log(NDLS, NDLP, args...) netdata_logger(NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  231. #define nd_log_daemon(NDLP, args...) netdata_logger(NDLS_DAEMON, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  232. #define nd_log_collector(NDLP, args...) netdata_logger(NDLS_COLLECTORS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  233. #define netdata_log_info(args...) netdata_logger(NDLS_DAEMON, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
  234. #define netdata_log_error(args...) netdata_logger(NDLS_DAEMON, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
  235. #define collector_info(args...) netdata_logger(NDLS_COLLECTORS, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
  236. #define collector_error(args...) netdata_logger(NDLS_COLLECTORS, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
  237. #define log_aclk_message_bin(__data, __data_len, __tx, __mqtt_topic, __message_name) \
  238. nd_log(NDLS_ACLK, NDLP_INFO, \
  239. "direction:%s message:'%s' topic:'%s' json:'%.*s'", \
  240. (__tx) ? "OUTGOING" : "INCOMING", __message_name, __mqtt_topic, (int)(__data_len), __data)
  241. // ----------------------------------------------------------------------------
  242. // logging with limits
  243. typedef struct error_with_limit {
  244. SPINLOCK spinlock;
  245. time_t log_every;
  246. size_t count;
  247. time_t last_logged;
  248. usec_t sleep_ut;
  249. } ERROR_LIMIT;
  250. #define nd_log_limit_static_global_var(var, log_every_secs, sleep_usecs) static ERROR_LIMIT var = { .last_logged = 0, .count = 0, .log_every = (log_every_secs), .sleep_ut = (sleep_usecs) }
  251. #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) }
  252. 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);
  253. #define nd_log_limit(erl, NDLS, NDLP, args...) netdata_logger_with_limit(erl, NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  254. // ----------------------------------------------------------------------------
  255. void netdata_logger_fatal( const char *file, const char *function, unsigned long line, const char *fmt, ... ) NORETURN PRINTFLIKE(4, 5);
  256. # ifdef __cplusplus
  257. }
  258. # endif
  259. #endif /* NETDATA_LOG_H */