]> source.dussan.org Git - rspamd.git/commitdiff
Simplify logfiles escaping.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 23 Dec 2013 14:48:10 +0000 (14:48 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 23 Dec 2013 14:48:10 +0000 (14:48 +0000)
src/logger.c
src/printf.c
src/printf.h

index 9dd999c61a202bac34226bf4e93012eb16bb4cec..c4592af331dbeb39a780c8a208327d32653f37e7 100644 (file)
@@ -144,6 +144,22 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, void *data, gint count, gboo
        }
 }
 
+static void
+rspamd_escape_log_string (gchar *str)
+{
+       guchar *p = (guchar *)str;
+
+       while (*p) {
+               if ((*p & 0x80) || !g_ascii_isprint (*p)) {
+                       *p = '?';
+               }
+               else if (*p == '\n' || *p == '\r') {
+                       *p = ' ';
+               }
+               p ++;
+       }
+}
+
 /* Logging utility functions */
 gint
 open_log_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
@@ -382,7 +398,7 @@ void
 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];
+       static gchar                    logbuf[BUFSIZ];
        va_list                         vp;
     u_char                         *end;
 
@@ -391,9 +407,9 @@ rspamd_common_log_function (rspamd_logger_t *rspamd_log, GLogLevelFlags log_leve
                va_start (vp, fmt);
                end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
                *end = '\0';
-               (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+               rspamd_escape_log_string (logbuf);
                va_end (vp);
-               rspamd_log->log_func (NULL, function, log_level, escaped_logbuf, FALSE, rspamd_log);
+               rspamd_log->log_func (NULL, function, log_level, logbuf, FALSE, rspamd_log);
                g_mutex_unlock (rspamd_log->mtx);
        }
 }
@@ -402,7 +418,7 @@ void
 rspamd_default_log_function (GLogLevelFlags log_level,
                const gchar *function, const gchar *fmt, ...)
 {
-       static gchar                   logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
+       static gchar                   logbuf[BUFSIZ];
        va_list                         vp;
        u_char                         *end;
 
@@ -412,9 +428,9 @@ rspamd_default_log_function (GLogLevelFlags log_level,
                        va_start (vp, fmt);
                        end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
                        *end = '\0';
-                       (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+                       rspamd_escape_log_string (logbuf);
                        va_end (vp);
-                       fprintf (stderr, "%s\n", escaped_logbuf);
+                       fprintf (stderr, "%s\n", logbuf);
                }
        }
        else if (log_level <= default_logger->cfg->log_level) {
@@ -422,9 +438,9 @@ rspamd_default_log_function (GLogLevelFlags log_level,
                va_start (vp, fmt);
                end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
                *end = '\0';
-               (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+               rspamd_escape_log_string (logbuf);
                va_end (vp);
-               default_logger->log_func (NULL, function, log_level, escaped_logbuf, FALSE, default_logger);
+               default_logger->log_func (NULL, function, log_level, logbuf, FALSE, default_logger);
                g_mutex_unlock (default_logger->mtx);
        }
 }
@@ -687,7 +703,7 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla
 void
 rspamd_conditional_debug (rspamd_logger_t *rspamd_log, guint32 addr, const gchar *function, const gchar *fmt, ...)
 {
-       static gchar                     logbuf[BUFSIZ], escaped_logbuf[BUFSIZ];
+       static gchar                     logbuf[BUFSIZ];
        va_list                         vp;
     u_char                         *end;
 
@@ -698,9 +714,9 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log, guint32 addr, const gchar
                va_start (vp, fmt);
                end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp);
                *end = '\0';
-               (void)rspamd_escape_string (escaped_logbuf, logbuf, sizeof (escaped_logbuf));
+               rspamd_escape_log_string (logbuf);
                va_end (vp);
-               rspamd_log->log_func (NULL, function, G_LOG_LEVEL_DEBUG, escaped_logbuf, TRUE, rspamd_log);
+               rspamd_log->log_func (NULL, function, G_LOG_LEVEL_DEBUG, logbuf, TRUE, rspamd_log);
                g_mutex_unlock (rspamd_log->mtx);
        }
 } 
@@ -710,13 +726,11 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log, guint32 addr, const gchar
 void
 rspamd_glib_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg)
 {
-       gchar                         escaped_logbuf[BUFSIZ];
        rspamd_logger_t              *rspamd_log = arg;
 
        if (rspamd_log->enabled) {
                g_mutex_lock (rspamd_log->mtx);
-               (void)rspamd_escape_string (escaped_logbuf, message, sizeof (escaped_logbuf));
-               rspamd_log->log_func (log_domain, NULL, log_level, escaped_logbuf, FALSE, rspamd_log);
+               rspamd_log->log_func (log_domain, NULL, log_level, message, FALSE, rspamd_log);
                g_mutex_unlock (rspamd_log->mtx);
        }
 }
index 5c425b5f997a737197fbf19f1a07ebdc0936263b..d72ec95c8dc9efa02910f370fe7d998553be3dc9 100644 (file)
@@ -280,60 +280,6 @@ rspamd_printf_gstring (GString *s, const gchar *fmt, ...)
        return r;
 }
 
-gchar *
-rspamd_escape_string (gchar *dst, const gchar *src, glong len)
-{
-       gchar              *buf = dst, *last = dst + len;
-       guint8              c;
-       const gchar        *p = src;
-       gunichar            uc;
-
-       if (len <= 0) {
-               return dst;
-       }
-
-       while (*p && buf < last) {
-               /* Detect utf8 */
-               uc = g_utf8_get_char_validated (p, last - buf);
-               if (uc > 0) {
-                       c = g_unichar_to_utf8 (uc, buf);
-                       buf += c;
-                       p += c;
-               }
-               else {
-                       c = *p ++;
-                       if (G_UNLIKELY ((c & 0x80))) {
-                               c &= 0x7F;
-                               if (last - buf >= 3) {
-                                       *buf++ = 'M';
-                                       *buf++ = '-';
-                               }
-                       }
-                       if (G_UNLIKELY ( g_ascii_iscntrl (c))) {
-                               if (c == '\n') {
-                                       *buf++ = ' ';
-                               }
-                               else if (c == '\t') {
-                                       *buf++ = '\t';
-                               }
-                               else {
-                                       *buf++ = '^';
-                                       if (buf != last) {
-                                               *buf++ = c ^ 0100;
-                                       }
-                               }
-                       }
-                       else {
-                               *buf++ = c;
-                       }
-               }
-       }
-
-       *buf = '\0';
-
-       return buf;
-}
-
 #define RSPAMD_PRINTF_APPEND(buf, len)                                                                                 \
     do {                                                                                                                                               \
     wr = func ((buf), (len), apd);                                                                                             \
index 105b085037d28e01841afdc4f455eedc69f75034..a4e03791d2fdb0cca8c57311ab3bd21f8cf5e67a 100644 (file)
@@ -72,14 +72,4 @@ glong rspamd_printf_gstring (GString *s, const gchar *fmt, ...);
 
 glong rspamd_vprintf_common (rspamd_printf_append_func func, gpointer apd, const gchar *fmt, va_list args);
 
-/**
- * Escape rspamd string to write it to log file or other 7 bit prefferable places
- *
- * @param dst destination string
- * @param src source string
- * @param len length of destination buffer
- * @return pointer to end of buffer
- */
-gchar * rspamd_escape_string (gchar *dst, const gchar *src, glong len);
-
 #endif /* PRINTF_H_ */