logging: set debug level instead of on/off

Prefix messages written to terminal with filename, line and function
name only with debug level 2 and higher.
This commit is contained in:
Miroslav Lichvar 2014-04-08 18:44:17 +02:00
parent 7c45b1d2a3
commit 788e7fcd89
3 changed files with 21 additions and 13 deletions

View file

@ -42,7 +42,9 @@ static int system_log = 0;
static int parent_fd = 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; static time_t last_limited = 0;
@ -146,18 +148,20 @@ void LOG_Message(LOG_Severity severity, LOG_Facility facility,
time_t t; time_t t;
struct tm stm; struct tm stm;
/* Don't write debug messages if not enabled */ /* Don't write debug messages if debug level is too low */
if (!log_debug && severity == LOGS_DEBUG) if (debug_level < DEBUG_LEVEL_PRINT_DEBUG && severity == LOGS_DEBUG)
return; return;
#ifdef WINNT #ifdef WINNT
#else #else
if (!system_log) { 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); time(&t);
stm = *gmtime(&t); stm = *gmtime(&t);
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm); 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 #endif
@ -209,9 +213,9 @@ LOG_OpenSystemLog(void)
/* ================================================== */ /* ================================================== */
void LOG_EnableDebug(void) void LOG_SetDebugLevel(int level)
{ {
log_debug = 1; debug_level = level;
} }
/* ================================================== */ /* ================================================== */

View file

@ -102,8 +102,12 @@ extern void LOG_Message(LOG_Severity severity, LOG_Facility facility,
int line_number, const char *filename, int line_number, const char *filename,
const char *function_name, const char *format, ...); const char *function_name, const char *format, ...);
/* Enable logging of debug messages */ /* Set debug level:
extern void LOG_EnableDebug(void); 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 */ /* Log messages to syslog instead of stderr */
extern void LOG_OpenSystemLog(void); extern void LOG_OpenSystemLog(void);

8
main.c
View file

@ -323,6 +323,7 @@ int main
int do_init_rtc = 0, restarted = 0; int do_init_rtc = 0, restarted = 0;
int other_pid; int other_pid;
int lock_memory = 0, sched_priority = 0; int lock_memory = 0, sched_priority = 0;
int system_log = 1;
LOG_Initialise(); LOG_Initialise();
@ -361,6 +362,7 @@ int main
} else if (!strcmp("-d", *argv)) { } else if (!strcmp("-d", *argv)) {
debug++; debug++;
nofork = 1; nofork = 1;
system_log = 0;
} else if (!strcmp("-4", *argv)) { } else if (!strcmp("-4", *argv)) {
address_family = IPADDR_INET4; address_family = IPADDR_INET4;
} else if (!strcmp("-6", *argv)) { } else if (!strcmp("-6", *argv)) {
@ -381,13 +383,11 @@ int main
go_daemon(); go_daemon();
} }
if (!debug) { if (system_log) {
LOG_OpenSystemLog(); LOG_OpenSystemLog();
} }
if (debug > 1) { LOG_SetDebugLevel(debug);
LOG_EnableDebug();
}
LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION); LOG(LOGS_INFO, LOGF_Main, "chronyd version %s starting", CHRONY_VERSION);