From a6bb8e8d31bc9ecdc01c454b36150fea84c37ca8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 11 Apr 2018 22:31:55 +0200 Subject: [SERVER] Option to disable timestamps on stdout/console (default: disabled) --- conf/server.conf | 3 +++ src/fuse/main.c | 1 + src/server/globals.c | 4 +++- src/shared/log.c | 33 +++++++++++++++++++++++++-------- src/shared/log.h | 2 ++ 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/conf/server.conf b/conf/server.conf index 332f8a3..a33a84e 100644 --- a/conf/server.conf +++ b/conf/server.conf @@ -49,4 +49,7 @@ consoleMask=ERROR WARNING MINOR INFO ; INFO Informational message ; DEBUG1 Debug information, used for medium verbosity ; DEBUG2 Used for debug messages that would show up a lot +; +; Whether timestamps should be output to console too (or just to file if false) +consoleTimestamps=false diff --git a/src/fuse/main.c b/src/fuse/main.c index e927541..adf7b23 100644 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -308,6 +308,7 @@ int main(int argc, char *argv[]) // TODO Make log mask configurable log_setConsoleMask( 65535 ); + log_setConsoleTimestamps( true ); log_setFileMask( 65535 ); newArgv = calloc( argc + 10, sizeof(char*) ); diff --git a/src/server/globals.c b/src/server/globals.c index 0502bbc..a59974f 100644 --- a/src/server/globals.c +++ b/src/server/globals.c @@ -32,8 +32,9 @@ int _maxImages = SERVER_MAX_IMAGES; int _maxPayload = 9000000; // 9MB uint64_t _maxReplicationSize = (uint64_t)100000000000LL; +#define IS_TRUE(value) (atoi(value) != 0 || strcmp(value, "true") == 0 || strcmp(value, "True") == 0 || strcmp(value, "TRUE") == 0) #define SAVE_TO_VAR_STR(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) { if (_ ## kk != NULL) free(_ ## kk); _ ## kk = strdup(value); } } while (0) -#define SAVE_TO_VAR_BOOL(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) _ ## kk = atoi(value) != 0 || strcmp(value, "true") == 0 || strcmp(value, "True") == 0 || strcmp(value, "TRUE") == 0; } while (0) +#define SAVE_TO_VAR_BOOL(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) _ ## kk = IS_TRUE(value); } while (0) #define SAVE_TO_VAR_INT(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) parse32(value, &_ ## kk, #ss); } while (0) #define SAVE_TO_VAR_UINT(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) parse32u(value, &_ ## kk, #ss); } while (0) #define SAVE_TO_VAR_UINT64(ss, kk) do { if (strcmp(section, #ss) == 0 && strcmp(key, #kk) == 0) parse64u(value, &_ ## kk, #ss); } while (0) @@ -69,6 +70,7 @@ static int ini_handler(void *custom UNUSED, const char* section, const char* key SAVE_TO_VAR_UINT64( limits, maxReplicationSize ); if ( strcmp( section, "logging" ) == 0 && strcmp( key, "fileMask" ) == 0 ) handleMaskString( value, &log_setFileMask ); if ( strcmp( section, "logging" ) == 0 && strcmp( key, "consoleMask" ) == 0 ) handleMaskString( value, &log_setConsoleMask ); + if ( strcmp( section, "logging" ) == 0 && strcmp( key, "consoleTimestamps" ) == 0 ) log_setConsoleTimestamps( IS_TRUE(value) ); if ( strcmp( section, "logging" ) == 0 && strcmp( key, "file" ) == 0 ) { if ( log_openLogFile( value ) ) { logadd( LOG_INFO, "Opened log file %s", value ); diff --git a/src/shared/log.c b/src/shared/log.c index a3ffe4a..8df19c1 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -37,6 +37,8 @@ static logmask_t maskCon = 15; static char *logFile = NULL; static int logFd = -1; +static bool consoleTimestamps = false; + static int writeLevel(char *buffer, logmask_t level); @@ -56,6 +58,11 @@ void log_setConsoleMask(logmask_t mask) maskCon = mask; } +void log_setConsoleTimestamps(bool on) +{ + consoleTimestamps = on; +} + bool log_openLogFile(const char *path) { pthread_mutex_lock( &logLock ); @@ -85,10 +92,18 @@ void logadd(const logmask_t mask, const char *fmt, ...) time_t rawtime; struct tm timeinfo; char buffer[LINE_LEN]; - - time( &rawtime ); - localtime_r( &rawtime, &timeinfo ); - size_t offset = strftime( buffer, LINE_LEN, "[%d.%m. %H:%M:%S] ", &timeinfo ); + bool toFile = maskFile & mask; + bool toStdout = maskCon & mask; + size_t offset; + + if ( toFile || ( toStdout && consoleTimestamps ) ) { + time( &rawtime ); + localtime_r( &rawtime, &timeinfo ); + offset = strftime( buffer, LINE_LEN, "[%d.%m. %H:%M:%S] ", &timeinfo ); + } else { + offset = 0; + } + const char *stdoutLine = buffer + offset; offset += writeLevel( buffer + offset, mask ); va_start( ap, fmt ); ret = vsnprintf( buffer + offset, LINE_LEN - offset, fmt, ap ); @@ -103,13 +118,14 @@ void logadd(const logmask_t mask, const char *fmt, ...) buffer[offset++] = '\n'; buffer[offset] = '\0'; } - if ( maskFile & mask ) { + if ( toFile ) { pthread_mutex_lock( &logLock ); if ( logFd >= 0 ) { size_t done = 0; while (done < offset ) { const ssize_t wr = write( logFd, buffer + done, offset - done ); if ( wr < 0 ) { + if ( errno == EINTR ) continue; printf( "Logging to file failed! (errno=%d)\n", errno ); break; } @@ -118,12 +134,13 @@ void logadd(const logmask_t mask, const char *fmt, ...) } pthread_mutex_unlock( &logLock ); } - if ( maskCon & mask ) { + if ( toStdout ) { + if ( consoleTimestamps ) stdoutLine = buffer; #ifdef AFL_MODE - fputs( buffer, stderr ); + fputs( stdoutLine, stderr ); fflush( stderr ); #else - fputs( buffer, stdout ); + fputs( stdoutLine, stdout ); fflush( stdout ); #endif } diff --git a/src/shared/log.h b/src/shared/log.h index da34fc3..5b1e8f7 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -42,6 +42,8 @@ void log_setFileMask(logmask_t mask); void log_setConsoleMask(logmask_t mask); +void log_setConsoleTimestamps(bool on); + /** * Open or reopen the log file. If path is NULL and the * function was called with a path before, the same path -- cgit v1.2.3-55-g7522