log.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 D_WEB_BUFFER 0x0000000000000001
  9. #define D_WEB_CLIENT 0x0000000000000002
  10. #define D_LISTENER 0x0000000000000004
  11. #define D_WEB_DATA 0x0000000000000008
  12. #define D_OPTIONS 0x0000000000000010
  13. #define D_PROCNETDEV_LOOP 0x0000000000000020
  14. #define D_RRD_STATS 0x0000000000000040
  15. #define D_WEB_CLIENT_ACCESS 0x0000000000000080
  16. #define D_TC_LOOP 0x0000000000000100
  17. #define D_DEFLATE 0x0000000000000200
  18. #define D_CONFIG 0x0000000000000400
  19. #define D_PLUGINSD 0x0000000000000800
  20. #define D_CHILDS 0x0000000000001000
  21. #define D_EXIT 0x0000000000002000
  22. #define D_CHECKS 0x0000000000004000
  23. #define D_NFACCT_LOOP 0x0000000000008000
  24. #define D_PROCFILE 0x0000000000010000
  25. #define D_RRD_CALLS 0x0000000000020000
  26. #define D_DICTIONARY 0x0000000000040000
  27. #define D_MEMORY 0x0000000000080000
  28. #define D_CGROUP 0x0000000000100000
  29. #define D_REGISTRY 0x0000000000200000
  30. #define D_VARIABLES 0x0000000000400000
  31. #define D_HEALTH 0x0000000000800000
  32. #define D_CONNECT_TO 0x0000000001000000
  33. #define D_RRDHOST 0x0000000002000000
  34. #define D_LOCKS 0x0000000004000000
  35. #define D_EXPORTING 0x0000000008000000
  36. #define D_STATSD 0x0000000010000000
  37. #define D_POLLFD 0x0000000020000000
  38. #define D_STREAM 0x0000000040000000
  39. #define D_ANALYTICS 0x0000000080000000
  40. #define D_RRDENGINE 0x0000000100000000
  41. #define D_ACLK 0x0000000200000000
  42. #define D_REPLICATION 0x0000002000000000
  43. #define D_SYSTEM 0x8000000000000000
  44. extern int web_server_is_multithreaded;
  45. extern uint64_t debug_flags;
  46. extern const char *program_name;
  47. extern int stdaccess_fd;
  48. extern FILE *stdaccess;
  49. extern int stdhealth_fd;
  50. extern FILE *stdhealth;
  51. extern int stdcollector_fd;
  52. extern FILE *stderror;
  53. extern const char *stdaccess_filename;
  54. extern const char *stderr_filename;
  55. extern const char *stdout_filename;
  56. extern const char *stdhealth_filename;
  57. extern const char *stdcollector_filename;
  58. extern const char *facility_log;
  59. #ifdef ENABLE_ACLK
  60. extern const char *aclklog_filename;
  61. extern int aclklog_fd;
  62. extern FILE *aclklog;
  63. extern int aclklog_enabled;
  64. #endif
  65. extern int access_log_syslog;
  66. extern int error_log_syslog;
  67. extern int output_log_syslog;
  68. extern int health_log_syslog;
  69. extern time_t error_log_throttle_period;
  70. extern unsigned long error_log_errors_per_period, error_log_errors_per_period_backup;
  71. int error_log_limit(int reset);
  72. void open_all_log_files();
  73. void reopen_all_log_files();
  74. #define LOG_DATE_LENGTH 26
  75. void log_date(char *buffer, size_t len, time_t now);
  76. static inline void debug_dummy(void) {}
  77. void error_log_limit_reset(void);
  78. void error_log_limit_unlimited(void);
  79. typedef struct error_with_limit {
  80. time_t log_every;
  81. size_t count;
  82. time_t last_logged;
  83. usec_t sleep_ut;
  84. } ERROR_LIMIT;
  85. typedef enum netdata_log_level {
  86. NETDATA_LOG_LEVEL_ERROR,
  87. NETDATA_LOG_LEVEL_INFO,
  88. NETDATA_LOG_LEVEL_END
  89. } netdata_log_level_t;
  90. #define NETDATA_LOG_LEVEL_INFO_STR "info"
  91. #define NETDATA_LOG_LEVEL_ERROR_STR "error"
  92. #define NETDATA_LOG_LEVEL_ERROR_SHORT_STR "err"
  93. extern netdata_log_level_t global_log_severity_level;
  94. netdata_log_level_t log_severity_string_to_severity_level(char *level);
  95. char *log_severity_level_to_severity_string(netdata_log_level_t level);
  96. void log_set_global_severity_level(netdata_log_level_t value);
  97. void log_set_global_severity_for_external_plugins();
  98. #define error_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) }
  99. #define error_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) }
  100. #ifdef NETDATA_INTERNAL_CHECKS
  101. #define netdata_log_debug(type, args...) do { if(unlikely(debug_flags & type)) debug_int(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  102. #define internal_error(condition, args...) do { if(unlikely(condition)) error_int(0, "IERR", __FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  103. #define internal_fatal(condition, args...) do { if(unlikely(condition)) fatal_int(__FILE__, __FUNCTION__, __LINE__, ##args); } while(0)
  104. #else
  105. #define netdata_log_debug(type, args...) debug_dummy()
  106. #define internal_error(args...) debug_dummy()
  107. #define internal_fatal(args...) debug_dummy()
  108. #endif
  109. #define netdata_log_info(args...) info_int(0, __FILE__, __FUNCTION__, __LINE__, ##args)
  110. #define collector_info(args...) info_int(1, __FILE__, __FUNCTION__, __LINE__, ##args)
  111. #define infoerr(args...) error_int(0, "INFO", __FILE__, __FUNCTION__, __LINE__, ##args)
  112. #define netdata_log_error(args...) error_int(0, "ERROR", __FILE__, __FUNCTION__, __LINE__, ##args)
  113. #define collector_infoerr(args...) error_int(1, "INFO", __FILE__, __FUNCTION__, __LINE__, ##args)
  114. #define collector_error(args...) error_int(1, "ERROR", __FILE__, __FUNCTION__, __LINE__, ##args)
  115. #define error_limit(erl, args...) error_limit_int(erl, "ERROR", __FILE__, __FUNCTION__, __LINE__, ##args)
  116. #define fatal(args...) fatal_int(__FILE__, __FUNCTION__, __LINE__, ##args)
  117. #define fatal_assert(expr) ((expr) ? (void)(0) : fatal_int(__FILE__, __FUNCTION__, __LINE__, "Assertion `%s' failed", #expr))
  118. void send_statistics(const char *action, const char *action_result, const char *action_data);
  119. void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) PRINTFLIKE(4, 5);
  120. void info_int( int is_collector, const char *file, const char *function, const unsigned long line, const char *fmt, ... ) PRINTFLIKE(5, 6);
  121. void error_int( int is_collector, const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... ) PRINTFLIKE(6, 7);
  122. void error_limit_int(ERROR_LIMIT *erl, const char *prefix, const char *file __maybe_unused, const char *function __maybe_unused, unsigned long line __maybe_unused, const char *fmt, ... ) PRINTFLIKE(6, 7);;
  123. void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) NORETURN PRINTFLIKE(4, 5);
  124. void netdata_log_access( const char *fmt, ... ) PRINTFLIKE(1, 2);
  125. void netdata_log_health( const char *fmt, ... ) PRINTFLIKE(1, 2);
  126. #ifdef ENABLE_ACLK
  127. void log_aclk_message_bin( const char *data, const size_t data_len, int tx, const char *mqtt_topic, const char *message_name);
  128. #endif
  129. # ifdef __cplusplus
  130. }
  131. # endif
  132. #endif /* NETDATA_LOG_H */