diff --git a/logging.c b/logging.c index 077a60c..8719a46 100644 --- a/logging.c +++ b/logging.c @@ -42,7 +42,9 @@ static int system_log = 0; static int parent_fd = 0; -static int log_debug = 0; +#define DEBUG_LEVEL_PRINT_FUNCTION 2 +#define DEBUG_LEVEL_PRINT_DEBUG 2 +static int debug_level = 0; static time_t last_limited = 0; @@ -146,18 +148,20 @@ void LOG_Message(LOG_Severity severity, LOG_Facility facility, time_t t; struct tm stm; - /* Don't write debug messages if not enabled */ - if (!log_debug && severity == LOGS_DEBUG) + /* Don't write debug messages if debug level is too low */ + if (debug_level < DEBUG_LEVEL_PRINT_DEBUG && severity == LOGS_DEBUG) return; #ifdef WINNT #else if (!system_log) { - /* Don't clutter up syslog with internal debugging info */ + /* Don't clutter up syslog with timestamps and internal debugging info */ time(&t); stm = *gmtime(&t); strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm); - fprintf(stderr, "%s %s:%d:(%s) ", buf, filename, line_number, function_name); + fprintf(stderr, "%s ", buf); + if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION) + fprintf(stderr, "%s:%d:(%s) ", filename, line_number, function_name); } #endif @@ -209,9 +213,9 @@ LOG_OpenSystemLog(void) /* ================================================== */ -void LOG_EnableDebug(void) +void LOG_SetDebugLevel(int level) { - log_debug = 1; + debug_level = level; } /* ================================================== */ diff --git a/logging.h b/logging.h index 2b247b4..fcadb7b 100644 --- a/logging.h +++ b/logging.h @@ -102,8 +102,12 @@ extern void LOG_Message(LOG_Severity severity, LOG_Facility facility, int line_number, const char *filename, const char *function_name, const char *format, ...); -/* Enable logging of debug messages */ -extern void LOG_EnableDebug(void); +/* Set debug level: + 0, 1 - only non-debug messages are logged + 2 - debug messages are logged too, all messages are prefixed with + filename, line, and function name + */ +extern void LOG_SetDebugLevel(int level); /* Log messages to syslog instead of stderr */ extern void LOG_OpenSystemLog(void); diff --git a/main.c b/main.c index 5ed3add..825c18b 100644 --- a/main.c +++ b/main.c @@ -323,6 +323,7 @@ int main int do_init_rtc = 0, restarted = 0; int other_pid; int lock_memory = 0, sched_priority = 0; + int system_log = 1; LOG_Initialise(); @@ -361,6 +362,7 @@ int main } else if (!strcmp("-d", *argv)) { debug++; nofork = 1; + system_log = 0; } else if (!strcmp("-4", *argv)) { address_family = IPADDR_INET4; } else if (!strcmp("-6", *argv)) { @@ -381,13 +383,11 @@ int main go_daemon(); } - if (!debug) { + if (system_log) { LOG_OpenSystemLog(); } - if (debug > 1) { - LOG_EnableDebug(); - } + LOG_SetDebugLevel(debug); LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION);