aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2013-12-23 14:48:10 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2013-12-23 14:48:10 +0000
commita2ae3d2678e461bccdae848a1f31607d8ed8d6f1 (patch)
treeee28339830ef7f233e461ad60f6bff772175354e
parent4ed70df2fbc849b080aea7977321daeec81db6f0 (diff)
downloadrspamd-a2ae3d2678e461bccdae848a1f31607d8ed8d6f1.tar.gz
rspamd-a2ae3d2678e461bccdae848a1f31607d8ed8d6f1.zip
Simplify logfiles escaping.
-rw-r--r--src/logger.c42
-rw-r--r--src/printf.c54
-rw-r--r--src/printf.h10
3 files changed, 28 insertions, 78 deletions
diff --git a/src/logger.c b/src/logger.c
index 9dd999c61..c4592af33 100644
--- a/src/logger.c
+++ b/src/logger.c
@@ -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);
}
}
diff --git a/src/printf.c b/src/printf.c
index 5c425b5f9..d72ec95c8 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -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); \
diff --git a/src/printf.h b/src/printf.h
index 105b08503..a4e03791d 100644
--- a/src/printf.h
+++ b/src/printf.h
@@ -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_ */