log.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // web server and stream receiver
  55. NDF_SRC_IP, // the streaming / web server source IP
  56. NDF_SRC_PORT, // the streaming / web server source Port
  57. NDF_SRC_CAPABILITIES, // the stream receiver capabilities
  58. // stream sender (established links)
  59. NDF_DST_TRANSPORT, // the transport we send the request, one of: http, https
  60. NDF_DST_IP, // the destination streaming IP
  61. NDF_DST_PORT, // the destination streaming Port
  62. NDF_DST_CAPABILITIES, // the destination streaming capabilities
  63. // web server, aclk and stream receiver
  64. NDF_REQUEST_METHOD, // for http like requests, the http request method
  65. NDF_RESPONSE_CODE, // for http like requests, the http response code, otherwise a status string
  66. // web server (all), aclk (queries)
  67. NDF_CONNECTION_ID, // the web server connection ID
  68. NDF_TRANSACTION_ID, // the web server and API transaction ID
  69. NDF_RESPONSE_SENT_BYTES, // for http like requests, the response bytes
  70. NDF_RESPONSE_SIZE_BYTES, // for http like requests, the uncompressed response size
  71. NDF_RESPONSE_PREPARATION_TIME_USEC, // for http like requests, the preparation time
  72. NDF_RESPONSE_SENT_TIME_USEC, // for http like requests, the time to send the response back
  73. NDF_RESPONSE_TOTAL_TIME_USEC, // for http like requests, the total time to complete the response
  74. // health alerts
  75. NDF_ALERT_ID,
  76. NDF_ALERT_UNIQUE_ID,
  77. NDF_ALERT_EVENT_ID,
  78. NDF_ALERT_TRANSITION_ID,
  79. NDF_ALERT_CONFIG_HASH,
  80. NDF_ALERT_NAME,
  81. NDF_ALERT_CLASS,
  82. NDF_ALERT_COMPONENT,
  83. NDF_ALERT_TYPE,
  84. NDF_ALERT_EXEC,
  85. NDF_ALERT_RECIPIENT,
  86. NDF_ALERT_DURATION,
  87. NDF_ALERT_VALUE,
  88. NDF_ALERT_VALUE_OLD,
  89. NDF_ALERT_STATUS,
  90. NDF_ALERT_STATUS_OLD,
  91. NDF_ALERT_SOURCE,
  92. NDF_ALERT_UNITS,
  93. NDF_ALERT_SUMMARY,
  94. NDF_ALERT_INFO,
  95. NDF_ALERT_NOTIFICATION_REALTIME_USEC,
  96. // NDF_ALERT_FLAGS,
  97. // put new items here
  98. // leave the request URL and the message last
  99. NDF_REQUEST, // the request we are currently working on
  100. NDF_MESSAGE, // the log message, if any
  101. // terminator
  102. _NDF_MAX,
  103. } ND_LOG_FIELD_ID;
  104. typedef enum __attribute__((__packed__)) {
  105. NDFT_UNSET = 0,
  106. NDFT_TXT,
  107. NDFT_STR,
  108. NDFT_BFR,
  109. NDFT_U64,
  110. NDFT_I64,
  111. NDFT_DBL,
  112. NDFT_UUID,
  113. NDFT_CALLBACK,
  114. } ND_LOG_STACK_FIELD_TYPE;
  115. void nd_log_set_user_settings(ND_LOG_SOURCES source, const char *setting);
  116. void nd_log_set_facility(const char *facility);
  117. void nd_log_set_priority_level(const char *setting);
  118. void nd_log_initialize(void);
  119. void nd_log_reopen_log_files(void);
  120. void chown_open_file(int fd, uid_t uid, gid_t gid);
  121. void nd_log_chown_log_files(uid_t uid, gid_t gid);
  122. void nd_log_set_flood_protection(size_t logs, time_t period);
  123. void nd_log_initialize_for_external_plugins(const char *name);
  124. void nd_log_set_thread_source(ND_LOG_SOURCES source);
  125. bool nd_log_journal_socket_available(void);
  126. ND_LOG_FIELD_ID nd_log_field_id_by_name(const char *field, size_t len);
  127. int nd_log_priority2id(const char *priority);
  128. const char *nd_log_id2priority(ND_LOG_FIELD_PRIORITY priority);
  129. const char *nd_log_method_for_external_plugins(const char *s);
  130. int nd_log_health_fd(void);
  131. typedef bool (*log_formatter_callback_t)(BUFFER *wb, void *data);
  132. struct log_stack_entry {
  133. ND_LOG_FIELD_ID id;
  134. ND_LOG_STACK_FIELD_TYPE type;
  135. bool set;
  136. union {
  137. const char *txt;
  138. struct netdata_string *str;
  139. BUFFER *bfr;
  140. uint64_t u64;
  141. int64_t i64;
  142. double dbl;
  143. const uuid_t *uuid;
  144. struct {
  145. log_formatter_callback_t formatter;
  146. void *formatter_data;
  147. } cb;
  148. };
  149. };
  150. #define ND_LOG_STACK _cleanup_(log_stack_pop) struct log_stack_entry
  151. #define ND_LOG_STACK_PUSH(lgs) log_stack_push(lgs)
  152. #define ND_LOG_FIELD_TXT(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_TXT, .txt = (value), .set = true, }
  153. #define ND_LOG_FIELD_STR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_STR, .str = (value), .set = true, }
  154. #define ND_LOG_FIELD_BFR(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_BFR, .bfr = (value), .set = true, }
  155. #define ND_LOG_FIELD_U64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_U64, .u64 = (value), .set = true, }
  156. #define ND_LOG_FIELD_I64(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_I64, .i64 = (value), .set = true, }
  157. #define ND_LOG_FIELD_DBL(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_DBL, .dbl = (value), .set = true, }
  158. #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, }
  159. #define ND_LOG_FIELD_UUID(field, value) (struct log_stack_entry){ .id = (field), .type = NDFT_UUID, .uuid = (value), .set = true, }
  160. #define ND_LOG_FIELD_END() (struct log_stack_entry){ .id = NDF_STOP, .type = NDFT_UNSET, .set = false, }
  161. void log_stack_pop(void *ptr);
  162. void log_stack_push(struct log_stack_entry *lgs);
  163. #define D_WEB_BUFFER 0x0000000000000001
  164. #define D_WEB_CLIENT 0x0000000000000002
  165. #define D_LISTENER 0x0000000000000004
  166. #define D_WEB_DATA 0x0000000000000008
  167. #define D_OPTIONS 0x0000000000000010
  168. #define D_PROCNETDEV_LOOP 0x0000000000000020
  169. #define D_RRD_STATS 0x0000000000000040
  170. #define D_WEB_CLIENT_ACCESS 0x0000000000000080
  171. #define D_TC_LOOP 0x0000000000000100
  172. #define D_DEFLATE 0x0000000000000200
  173. #define D_CONFIG 0x0000000000000400
  174. #define D_PLUGINSD 0x0000000000000800
  175. #define D_CHILDS 0x0000000000001000
  176. #define D_EXIT 0x0000000000002000
  177. #define D_CHECKS 0x0000000000004000
  178. #define D_NFACCT_LOOP 0x0000000000008000
  179. #define D_PROCFILE 0x0000000000010000
  180. #define D_RRD_CALLS 0x0000000000020000
  181. #define D_DICTIONARY 0x0000000000040000
  182. #define D_MEMORY 0x0000000000080000
  183. #define D_CGROUP 0x0000000000100000
  184. #define D_REGISTRY 0x0000000000200000
  185. #define D_VARIABLES 0x0000000000400000
  186. #define D_HEALTH 0x0000000000800000
  187. #define D_CONNECT_TO 0x0000000001000000
  188. #define D_RRDHOST 0x0000000002000000
  189. #define D_LOCKS 0x0000000004000000
  190. #define D_EXPORTING 0x0000000008000000
  191. #define D_STATSD 0x0000000010000000
  192. #define D_POLLFD 0x0000000020000000
  193. #define D_STREAM 0x0000000040000000
  194. #define D_ANALYTICS 0x0000000080000000
  195. #define D_RRDENGINE 0x0000000100000000
  196. #define D_ACLK 0x0000000200000000
  197. #define D_REPLICATION 0x0000002000000000
  198. #define D_SYSTEM 0x8000000000000000
  199. extern uint64_t debug_flags;
  200. extern const char *program_name;
  201. #ifdef ENABLE_ACLK
  202. extern int aclklog_enabled;
  203. #endif
  204. #define LOG_DATE_LENGTH 26
  205. void log_date(char *buffer, size_t len, time_t now);
  206. static inline void debug_dummy(void) {}
  207. void nd_log_limits_reset(void);
  208. void nd_log_limits_unlimited(void);
  209. #define NDLP_INFO_STR "info"
  210. #ifdef NETDATA_INTERNAL_CHECKS
  211. #define netdata_log_debug(type, args...) do { if(unlikely(debug_flags & type)) netdata_logger(NDLS_DEBUG, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  212. #define internal_error(condition, args...) do { if(unlikely(condition)) netdata_logger(NDLS_DAEMON, NDLP_DEBUG, __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  213. #define internal_fatal(condition, args...) do { if(unlikely(condition)) netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  214. #else
  215. #define netdata_log_debug(type, args...) debug_dummy()
  216. #define internal_error(args...) debug_dummy()
  217. #define internal_fatal(args...) debug_dummy()
  218. #endif
  219. #define fatal(args...) netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, ##args)
  220. #define fatal_assert(expr) ((expr) ? (void)(0) : netdata_logger_fatal(__FILE__, __FUNCTION__, __LINE__, "Assertion `%s' failed", #expr))
  221. // ----------------------------------------------------------------------------
  222. // normal logging
  223. 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);
  224. #define nd_log(NDLS, NDLP, args...) netdata_logger(NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  225. #define nd_log_daemon(NDLP, args...) netdata_logger(NDLS_DAEMON, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  226. #define nd_log_collector(NDLP, args...) netdata_logger(NDLS_COLLECTORS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  227. #define netdata_log_info(args...) netdata_logger(NDLS_DAEMON, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
  228. #define netdata_log_error(args...) netdata_logger(NDLS_DAEMON, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
  229. #define collector_info(args...) netdata_logger(NDLS_COLLECTORS, NDLP_INFO, __FILE__, __FUNCTION__, __LINE__, ##args)
  230. #define collector_error(args...) netdata_logger(NDLS_COLLECTORS, NDLP_ERR, __FILE__, __FUNCTION__, __LINE__, ##args)
  231. #define log_aclk_message_bin(__data, __data_len, __tx, __mqtt_topic, __message_name) \
  232. nd_log(NDLS_ACLK, NDLP_INFO, \
  233. "direction:%s message:'%s' topic:'%s' json:'%.*s'", \
  234. (__tx) ? "OUTGOING" : "INCOMING", __message_name, __mqtt_topic, (int)(__data_len), __data)
  235. // ----------------------------------------------------------------------------
  236. // logging with limits
  237. typedef struct error_with_limit {
  238. SPINLOCK spinlock;
  239. time_t log_every;
  240. size_t count;
  241. time_t last_logged;
  242. usec_t sleep_ut;
  243. } ERROR_LIMIT;
  244. #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) }
  245. #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) }
  246. 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);;
  247. #define nd_log_limit(erl, NDLS, NDLP, args...) netdata_logger_with_limit(erl, NDLS, NDLP, __FILE__, __FUNCTION__, __LINE__, ##args)
  248. // ----------------------------------------------------------------------------
  249. void send_statistics(const char *action, const char *action_result, const char *action_data);
  250. void netdata_logger_fatal( const char *file, const char *function, unsigned long line, const char *fmt, ... ) NORETURN PRINTFLIKE(4, 5);
  251. # ifdef __cplusplus
  252. }
  253. # endif
  254. #endif /* NETDATA_LOG_H */