From 68d26dcec7ae462c4be724385ea3bb4fc744b4b2 Mon Sep 17 00:00:00 2001 From: Ken Steele Date: Tue, 20 Aug 2013 12:03:52 -0400 Subject: [PATCH] Merge multiple copies of CreateTimeString() to one copy. There were 8 identical copies of CreateTimeString() in 8 files. Most used SCLocalTime, to replace localtime_r(), but some did not. Created one copy in util-time.c. --- src/alert-debuglog.c | 13 ++----------- src/alert-fastlog.c | 13 ++----------- src/log-dnslog.c | 12 +----------- src/log-droplog.c | 14 ++------------ src/log-file.c | 13 ++----------- src/log-filestore.c | 13 ++----------- src/log-httplog.c | 14 ++------------ src/log-tlslog.c | 14 ++------------ src/util-time.c | 11 +++++++++++ src/util-time.h | 3 ++- 10 files changed, 28 insertions(+), 92 deletions(-) diff --git a/src/alert-debuglog.c b/src/alert-debuglog.c index 86a4692d0e..e6152b7c2e 100644 --- a/src/alert-debuglog.c +++ b/src/alert-debuglog.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -56,6 +56,7 @@ #include "util-var-name.h" #include "util-optimize.h" #include "util-logopenfile.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "alert-debug.log" @@ -86,16 +87,6 @@ typedef struct AlertDebugLogThread_ { MemBuffer *buffer; } AlertDebugLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) { - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm*)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - /** * \brief Function to log the FlowVars in to alert-debug.log * diff --git a/src/alert-fastlog.c b/src/alert-fastlog.c index 987a4583b8..fe9a42c237 100644 --- a/src/alert-fastlog.c +++ b/src/alert-fastlog.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -56,6 +56,7 @@ #include "util-proto-name.h" #include "util-optimize.h" #include "util-logopenfile.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "fast.log" @@ -105,16 +106,6 @@ typedef struct AlertFastLogThread_ { LogFileCtx* file_ctx; } AlertFastLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) { - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - TmEcode AlertFastLogIPv4(ThreadVars *tv, Packet *p, void *data, PacketQueue *pq, PacketQueue *postpq) { AlertFastLogThread *aft = (AlertFastLogThread *)data; diff --git a/src/log-dnslog.c b/src/log-dnslog.c index 768c6753c2..5ef4b1fab7 100644 --- a/src/log-dnslog.c +++ b/src/log-dnslog.c @@ -46,6 +46,7 @@ #include "util-buffer.h" #include "util-logopenfile.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "dns.log" @@ -95,17 +96,6 @@ typedef struct LogDnsLogThread_ { MemBuffer *buffer; } LogDnsLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) -{ - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - static void CreateTypeString(uint16_t type, char *str, size_t str_size) { if (type == DNS_RECORD_TYPE_A) { snprintf(str, str_size, "A"); diff --git a/src/log-droplog.c b/src/log-droplog.c index f3172ddc6b..ba5540f997 100644 --- a/src/log-droplog.c +++ b/src/log-droplog.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2011 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -52,6 +52,7 @@ #include "util-print.h" #include "util-proto-name.h" #include "util-logopenfile.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "drop.log" @@ -182,17 +183,6 @@ static void LogDropLogDeInitCtx(OutputCtx *output_ctx) } } -/** \brief Function to create the time string from the packet timestamp */ -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) { - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - /** * \brief Log the dropped packets in netfilter format when engine is running * in inline mode diff --git a/src/log-file.c b/src/log-file.c index 5d13f35d07..90acac047c 100644 --- a/src/log-file.c +++ b/src/log-file.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2012 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -47,6 +47,7 @@ #include "util-debug.h" #include "util-atomic.h" #include "util-file.h" +#include "util-time.h" #include "output.h" @@ -91,16 +92,6 @@ typedef struct LogFileLogThread_ { uint32_t file_cnt; } LogFileLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) { - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - static void LogFileMetaGetUri(FILE *fp, Packet *p, File *ff) { HtpState *htp_state = (HtpState *)p->flow->alstate; if (htp_state != NULL) { diff --git a/src/log-filestore.c b/src/log-filestore.c index ba6e88932f..933101e1a7 100644 --- a/src/log-filestore.c +++ b/src/log-filestore.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2012 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -45,6 +45,7 @@ #include "util-debug.h" #include "util-atomic.h" #include "util-file.h" +#include "util-time.h" #include "output.h" @@ -94,16 +95,6 @@ typedef struct LogFilestoreLogThread_ { uint32_t file_cnt; } LogFilestoreLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) { - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - static void LogFilestoreMetaGetUri(FILE *fp, Packet *p, File *ff) { HtpState *htp_state = (HtpState *)p->flow->alstate; if (htp_state != NULL) { diff --git a/src/log-httplog.c b/src/log-httplog.c index 1993ad1db2..57176a3426 100644 --- a/src/log-httplog.c +++ b/src/log-httplog.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2011 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -47,6 +47,7 @@ #include "util-buffer.h" #include "util-logopenfile.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "http.log" @@ -142,17 +143,6 @@ typedef struct LogHttpLogThread_ { MemBuffer *buffer; } LogHttpLogThread; -static void CreateTimeString (const struct timeval *ts, char *str, size_t size) -{ - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *)SCLocalTime(time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - /* Custom format logging */ static void LogHttpLogCustom(LogHttpLogThread *aft, htp_tx_t *tx, const struct timeval *ts, char *srcip, Port sp, char *dstip, Port dp) diff --git a/src/log-tlslog.c b/src/log-tlslog.c index 9b546df260..6b3b31d1f3 100644 --- a/src/log-tlslog.c +++ b/src/log-tlslog.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2012 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -48,6 +48,7 @@ #include "util-logopenfile.h" #include "util-crypt.h" +#include "util-time.h" #define DEFAULT_LOG_FILENAME "tls.log" @@ -125,17 +126,6 @@ typedef struct LogTlsLogThread_ { size_t enc_buf_len; } LogTlsLogThread; -static void CreateTimeString(const struct timeval *ts, char *str, size_t size) -{ - time_t time = ts->tv_sec; - struct tm local_tm; - struct tm *t = (struct tm *) localtime_r(&time, &local_tm); - - snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", - t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, - t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); -} - static void LogTlsLogExtended(LogTlsLogThread *aft, SSLState * state) { if (state->server_connp.cert0_fingerprint != NULL) { diff --git a/src/util-time.c b/src/util-time.c index a744607d78..48dfb4f809 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -116,3 +116,14 @@ struct tm *SCLocalTime(time_t timep, struct tm *result) { return localtime_r(&timep, result); } + +void CreateTimeString (const struct timeval *ts, char *str, size_t size) +{ + time_t time = ts->tv_sec; + struct tm local_tm; + struct tm *t = (struct tm*)SCLocalTime(time, &local_tm); + + snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", + t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, + t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); +} diff --git a/src/util-time.h b/src/util-time.h index fc18db8d51..fe930ff105 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2013 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -47,6 +47,7 @@ void TimeModeSetLive(void); void TimeModeSetOffline (void); struct tm *SCLocalTime(time_t timep, struct tm *result); +void CreateTimeString (const struct timeval *ts, char *str, size_t size); #endif /* __UTIL_TIME_H__ */