logging: allow logging to file instead of syslog

This commit is contained in:
Miroslav Lichvar 2017-05-24 15:38:43 +02:00
parent 3a5566c6c3
commit 6cbeb107db
2 changed files with 25 additions and 3 deletions

View file

@ -40,6 +40,7 @@ int log_debug_enabled = 0;
/* Flag indicating we have initialised */
static int initialised = 0;
static FILE *file_log;
static int system_log = 0;
static int parent_fd = 0;
@ -69,6 +70,7 @@ void
LOG_Initialise(void)
{
initialised = 1;
file_log = stderr;
}
/* ================================================== */
@ -79,6 +81,8 @@ LOG_Finalise(void)
{
if (system_log) {
closelog();
} else {
fclose(file_log);
}
LOG_CycleLogFiles();
@ -113,7 +117,7 @@ static void log_message(int fatal, LOG_Severity severity, const char *message)
}
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
} else {
fprintf(stderr, fatal ? "Fatal error : %s\n" : "%s\n", message);
fprintf(file_log, fatal ? "Fatal error : %s\n" : "%s\n", message);
}
}
@ -135,10 +139,10 @@ void LOG_Message(LOG_Severity severity,
time(&t);
stm = *gmtime(&t);
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &stm);
fprintf(stderr, "%s ", buf);
fprintf(file_log, "%s ", buf);
#if DEBUG > 0
if (debug_level >= DEBUG_LEVEL_PRINT_FUNCTION)
fprintf(stderr, "%s:%d:(%s) ", filename, line_number, function_name);
fprintf(file_log, "%s:%d:(%s) ", filename, line_number, function_name);
#endif
}
@ -173,6 +177,21 @@ void LOG_Message(LOG_Severity severity,
}
}
/* ================================================== */
void
LOG_OpenFileLog(const char *log_file)
{
FILE *f;
f = fopen(log_file, "a");
if (!f)
LOG_FATAL("Could not open log file %s", log_file);
file_log = f;
}
/* ================================================== */
void

View file

@ -99,6 +99,9 @@ extern void LOG_Message(LOG_Severity severity, const char *format, ...);
*/
extern void LOG_SetDebugLevel(int level);
/* Log messages to a file instead of stderr */
extern void LOG_OpenFileLog(const char *log_file);
/* Log messages to syslog instead of stderr */
extern void LOG_OpenSystemLog(void);