You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger_private.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2023 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_LOGGER_PRIVATE_H
  17. #define RSPAMD_LOGGER_PRIVATE_H
  18. #include "logger.h"
  19. /* How much message should be repeated before it is count to be repeated one */
  20. #define REPEATS_MIN 3
  21. #define REPEATS_MAX 300
  22. #define LOGBUF_LEN 8192
  23. struct rspamd_log_module {
  24. gchar *mname;
  25. guint id;
  26. };
  27. struct rspamd_log_modules {
  28. guchar *bitset;
  29. guint bitset_len; /* Number of BITS used in bitset */
  30. guint bitset_allocated; /* Size of bitset allocated in BYTES */
  31. GHashTable *modules;
  32. };
  33. struct rspamd_logger_error_elt {
  34. gint completed;
  35. GQuark ptype;
  36. pid_t pid;
  37. gdouble ts;
  38. gchar id[RSPAMD_LOG_ID_LEN + 1];
  39. gchar module[9];
  40. gchar message[];
  41. };
  42. struct rspamd_logger_error_log {
  43. struct rspamd_logger_error_elt *elts;
  44. rspamd_mempool_t *pool;
  45. uint32_t max_elts;
  46. uint32_t elt_len;
  47. /* Avoid false cache sharing */
  48. guchar __padding[64 - sizeof(gpointer) * 2 - sizeof(uint64_t)];
  49. guint cur_row;
  50. };
  51. /**
  52. * Static structure that store logging parameters
  53. * It is NOT shared between processes and is created by main process
  54. */
  55. struct rspamd_logger_s {
  56. struct rspamd_logger_funcs ops;
  57. gint log_level;
  58. struct rspamd_logger_error_log *errlog;
  59. struct rspamd_cryptobox_pubkey *pk;
  60. struct rspamd_cryptobox_keypair *keypair;
  61. guint flags;
  62. gboolean closed;
  63. gboolean enabled;
  64. gboolean is_debug;
  65. gboolean no_lock;
  66. pid_t pid;
  67. const gchar *process_type;
  68. struct rspamd_radix_map_helper *debug_ip;
  69. rspamd_mempool_mutex_t *mtx;
  70. rspamd_mempool_t *pool;
  71. uint64_t log_cnt[4];
  72. };
  73. /*
  74. * Common logging prototypes
  75. */
  76. /*
  77. * File logging
  78. */
  79. void *rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
  80. uid_t uid, gid_t gid, GError **err);
  81. void *rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
  82. gpointer arg, uid_t uid, gid_t gid, GError **err);
  83. void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg);
  84. bool rspamd_log_file_log(const gchar *module, const gchar *id,
  85. const gchar *function,
  86. gint level_flags,
  87. const gchar *message,
  88. gsize mlen,
  89. rspamd_logger_t *rspamd_log,
  90. gpointer arg);
  91. bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg,
  92. gpointer arg, GError **err);
  93. struct rspamd_logger_iov_thrash_stack {
  94. struct rspamd_logger_iov_thrash_stack *prev;
  95. char data[0];
  96. };
  97. #define RSPAMD_LOGGER_MAX_IOV 8
  98. struct rspamd_logger_iov_ctx {
  99. struct iovec iov[RSPAMD_LOGGER_MAX_IOV];
  100. int niov;
  101. struct rspamd_logger_iov_thrash_stack *thrash_stack;
  102. };
  103. /**
  104. * Fills IOV of logger (usable for file/console logging)
  105. * Warning: this function is NOT reentrant, do not call it twice from a single moment of execution
  106. * @param iov filled by this function
  107. * @param module
  108. * @param id
  109. * @param function
  110. * @param level_flags
  111. * @param message
  112. * @param mlen
  113. * @param rspamd_log
  114. * @return number of iov elements being filled
  115. */
  116. void rspamd_log_fill_iov(struct rspamd_logger_iov_ctx *iov_ctx,
  117. double ts,
  118. const gchar *module, const gchar *id,
  119. const gchar *function,
  120. gint level_flags,
  121. const gchar *message,
  122. gsize mlen,
  123. rspamd_logger_t *rspamd_log);
  124. /**
  125. * Frees IOV context
  126. * @param iov_ctx
  127. */
  128. void rspamd_log_iov_free(struct rspamd_logger_iov_ctx *iov_ctx);
  129. /**
  130. * Escape log line by replacing unprintable characters to hex escapes like \xNN
  131. * @param src
  132. * @param srclen
  133. * @param dst
  134. * @param dstlen
  135. * @return end of the escaped buffer
  136. */
  137. gchar *rspamd_log_line_hex_escape(const guchar *src, gsize srclen,
  138. gchar *dst, gsize dstlen);
  139. /**
  140. * Returns number of characters to be escaped, e.g. a caller can allocate a new buffer
  141. * the desired number of characters
  142. * @param src
  143. * @param srclen
  144. * @return number of characters to be escaped
  145. */
  146. gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen);
  147. static const struct rspamd_logger_funcs file_log_funcs = {
  148. .init = rspamd_log_file_init,
  149. .dtor = rspamd_log_file_dtor,
  150. .reload = rspamd_log_file_reload,
  151. .log = rspamd_log_file_log,
  152. .on_fork = rspamd_log_file_on_fork,
  153. };
  154. /*
  155. * Syslog logging
  156. */
  157. void *rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
  158. uid_t uid, gid_t gid, GError **err);
  159. void *rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
  160. gpointer arg, uid_t uid, gid_t gid, GError **err);
  161. void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg);
  162. bool rspamd_log_syslog_log(const gchar *module, const gchar *id,
  163. const gchar *function,
  164. gint level_flags,
  165. const gchar *message,
  166. gsize mlen,
  167. rspamd_logger_t *rspamd_log,
  168. gpointer arg);
  169. static const struct rspamd_logger_funcs syslog_log_funcs = {
  170. .init = rspamd_log_syslog_init,
  171. .dtor = rspamd_log_syslog_dtor,
  172. .reload = rspamd_log_syslog_reload,
  173. .log = rspamd_log_syslog_log,
  174. .on_fork = NULL,
  175. };
  176. /*
  177. * Console logging
  178. */
  179. void *rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg,
  180. uid_t uid, gid_t gid, GError **err);
  181. void *rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg,
  182. gpointer arg, uid_t uid, gid_t gid, GError **err);
  183. void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg);
  184. bool rspamd_log_console_log(const gchar *module, const gchar *id,
  185. const gchar *function,
  186. gint level_flags,
  187. const gchar *message,
  188. gsize mlen,
  189. rspamd_logger_t *rspamd_log,
  190. gpointer arg);
  191. static const struct rspamd_logger_funcs console_log_funcs = {
  192. .init = rspamd_log_console_init,
  193. .dtor = rspamd_log_console_dtor,
  194. .reload = rspamd_log_console_reload,
  195. .log = rspamd_log_console_log,
  196. .on_fork = NULL,
  197. };
  198. #endif