static const gchar lf_chr = '\n';
+static rspamd_logger_t *default_logger = NULL;
+
static void
syslog_log_function (const gchar * log_domain, const gchar *function,
rspamd_log->enabled = TRUE;
return 0;
case RSPAMD_LOG_FILE:
- rspamd_log->fd = open (rspamd_log->cfg->log_file, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
+ rspamd_log->fd = open (rspamd_log->cfg->log_file, O_CREAT | O_WRONLY | O_APPEND,
+ S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (rspamd_log->fd == -1) {
- fprintf (stderr, "open_log: cannot open desired log file: %s, %s", rspamd_log->cfg->log_file, strerror (errno));
+ fprintf (stderr, "open_log: cannot open desired log file: %s, %s",
+ rspamd_log->cfg->log_file, strerror (errno));
return -1;
}
if (fchown (rspamd_log->fd, uid, gid) == -1) {
- fprintf (stderr, "open_log: cannot chown desired log file: %s, %s", rspamd_log->cfg->log_file, strerror (errno));
+ fprintf (stderr, "open_log: cannot chown desired log file: %s, %s",
+ rspamd_log->cfg->log_file, strerror (errno));
+ close (rspamd_log->fd);
return -1;
}
rspamd_log->enabled = TRUE;
radix_tree_free (rspamd->logger->debug_ip);
rspamd->logger->debug_ip = NULL;
}
+
+ default_logger = rspamd->logger;
}
/**
* This log functions select real logger and write message if level is less or equal to configured log level
*/
void
-rspamd_common_log_function (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...)
+rspamd_common_log_function (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level,
+ const gchar *function, const gchar *fmt, ...)
{
static gchar logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
va_list vp;
}
}
+void
+rspamd_default_log_function (GLogLevelFlags log_level,
+ const gchar *function, const gchar *fmt, ...)
+{
+ static gchar logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
+ va_list vp;
+ u_char *end;
+
+ if (default_logger == NULL) {
+ /* Just fprintf message */
+ if (log_level >= G_LOG_LEVEL_INFO) {
+ va_start (vp, fmt);
+ end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
+ *end = '\0';
+ (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+ va_end (vp);
+ fprintf (stderr, "%s\n", escaped_logbuf);
+ }
+ }
+ else if (log_level <= default_logger->cfg->log_level) {
+ g_mutex_lock (default_logger->mtx);
+ va_start (vp, fmt);
+ end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
+ *end = '\0';
+ (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+ va_end (vp);
+ default_logger->log_func (NULL, function, log_level, escaped_logbuf, FALSE, default_logger);
+ g_mutex_unlock (default_logger->mtx);
+ }
+}
+
/**
* Fill buffer with message (limits must be checked BEFORE this call)
got_time = TRUE;
}
else {
- /* Do not try to write to file too often while throtling */
+ /* Do not try to write to file too often while throttling */
return;
}
}
g_mutex_unlock (rspamd_log->mtx);
}
}
-
/**
* Wrapper for glib logger
*/
/**
* Log function that is compatible for glib messages
*/
-void rspamd_glib_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg);
+void rspamd_glib_log_function (const gchar *log_domain,
+ GLogLevelFlags log_level, const gchar *message, gpointer arg);
/**
- * Function with variable number of arguments support
+ * Function with variable number of arguments support
*/
-void rspamd_common_log_function (rspamd_logger_t *logger, GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...);
+void rspamd_common_log_function (rspamd_logger_t *logger,
+ GLogLevelFlags log_level, const gchar *function, const gchar *fmt, ...);
/**
* Conditional debug function
*/
-void rspamd_conditional_debug (rspamd_logger_t *logger, guint32 addr, const gchar *function, const gchar *fmt, ...) ;
+void rspamd_conditional_debug (rspamd_logger_t *logger,
+ guint32 addr, const gchar *function, const gchar *fmt, ...) ;
+
+/**
+ * Function with variable number of arguments support that uses static default logger
+ */
+void rspamd_default_log_function (GLogLevelFlags log_level, const gchar *function,
+ const gchar *fmt, ...);
/**
* Temporary turn on debug
# define debug_task(...) rspamd_conditional_debug(rspamd_main->logger, task->from_addr.s_addr, __FUNCTION__, __VA_ARGS__)
#endif
#else
-#define msg_err(...) rspamd_log_fprintf(stderr, __VA_ARGS__)
-#define msg_warn(...) rspamd_log_fprintf(stderr, __VA_ARGS__)
-#define msg_info(...) rspamd_log_fprintf(stderr, __VA_ARGS__)
-#define msg_debug(...) rspamd_log_fprintf(stderr, __VA_ARGS__)
-#define debug_task(...) rspamd_log_fprintf(stderr, __VA_ARGS__)
+#define msg_err(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, __FUNCTION__, __VA_ARGS__)
+#define msg_warn(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, __FUNCTION__, __VA_ARGS__)
+#define msg_info(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, __FUNCTION__, __VA_ARGS__)
+#define msg_debug(...) do {} while(0)
+#define debug_task(...) do {} while(0)
#endif
#endif