Refactor logging
- merge LOG_Line_Function, LOG_Fatal_Function and LOG_Position - use C99 variadic macros for LOG and LOG_FATAL
This commit is contained in:
parent
ea418b2e18
commit
cd7bfa2510
2 changed files with 61 additions and 64 deletions
90
logging.c
90
logging.c
|
@ -98,78 +98,51 @@ LOG_Finalise(void)
|
|||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_Line_Function(LOG_Severity severity, LOG_Facility facility, const char *format, ...)
|
||||
static void log_message(int fatal, LOG_Severity severity, const char *message)
|
||||
{
|
||||
char buf[2048];
|
||||
va_list other_args;
|
||||
va_start(other_args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, other_args);
|
||||
va_end(other_args);
|
||||
#ifdef WINNT
|
||||
if (logfile) {
|
||||
fprintf(logfile, "%s\n", buf);
|
||||
fprintf(logfile, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||
}
|
||||
#else
|
||||
if (system_log) {
|
||||
int priority;
|
||||
switch (severity) {
|
||||
case LOGS_INFO:
|
||||
syslog(LOG_INFO, "%s", buf);
|
||||
priority = LOG_INFO;
|
||||
break;
|
||||
case LOGS_WARN:
|
||||
syslog(LOG_WARNING, "%s", buf);
|
||||
priority = LOG_WARNING;
|
||||
break;
|
||||
case LOGS_ERR:
|
||||
default:
|
||||
syslog(LOG_ERR, "%s", buf);
|
||||
priority = LOG_ERR;
|
||||
break;
|
||||
case LOGS_FATAL:
|
||||
priority = LOG_CRIT;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
|
||||
} else {
|
||||
fprintf(stderr, "%s\n", buf);
|
||||
fprintf(stderr, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_Fatal_Function(LOG_Facility facility, const char *format, ...)
|
||||
void LOG_Message(LOG_Severity severity, LOG_Facility facility,
|
||||
int line_number, const char *filename,
|
||||
const char *function_name, const char *format, ...)
|
||||
{
|
||||
char buf[2048];
|
||||
va_list other_args;
|
||||
va_start(other_args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, other_args);
|
||||
va_end(other_args);
|
||||
|
||||
#ifdef WINNT
|
||||
if (logfile) {
|
||||
fprintf(logfile, "Fatal error : %s\n", buf);
|
||||
}
|
||||
#else
|
||||
if (system_log) {
|
||||
syslog(LOG_CRIT, "Fatal error : %s", buf);
|
||||
} else {
|
||||
fprintf(stderr, "Fatal error : %s\n", buf);
|
||||
}
|
||||
if (parent_fd) {
|
||||
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
||||
; /* Not much we can do here */
|
||||
}
|
||||
#endif
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
void
|
||||
LOG_Position(const char *filename, int line_number, const char *function_name)
|
||||
{
|
||||
#ifdef WINNT
|
||||
#else
|
||||
time_t t;
|
||||
struct tm stm;
|
||||
char buf[64];
|
||||
|
||||
#ifdef WINNT
|
||||
#else
|
||||
if (!system_log) {
|
||||
/* Don't clutter up syslog with internal debugging info */
|
||||
time(&t);
|
||||
|
@ -178,6 +151,31 @@ LOG_Position(const char *filename, int line_number, const char *function_name)
|
|||
fprintf(stderr, "%s:%d:(%s)[%s] ", filename, line_number, function_name, buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
va_start(other_args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, other_args);
|
||||
va_end(other_args);
|
||||
|
||||
switch (severity) {
|
||||
case LOGS_INFO:
|
||||
case LOGS_WARN:
|
||||
case LOGS_ERR:
|
||||
log_message(0, severity, buf);
|
||||
break;
|
||||
case LOGS_FATAL:
|
||||
log_message(1, severity, buf);
|
||||
|
||||
if (parent_fd) {
|
||||
if (write(parent_fd, buf, strlen(buf) + 1) < 0)
|
||||
; /* Not much we can do here */
|
||||
}
|
||||
|
||||
exit(1);
|
||||
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
|
35
logging.h
35
logging.h
|
@ -28,11 +28,24 @@
|
|||
#ifndef GOT_LOGGING_H
|
||||
#define GOT_LOGGING_H
|
||||
|
||||
/* Line logging macros. If the compiler is GNU C, we take advantage of
|
||||
being able to get the function name also. */
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define FUNCTION_NAME __FUNCTION__
|
||||
#else
|
||||
#define FUNCTION_NAME ""
|
||||
#endif
|
||||
|
||||
#define LOG(severity, facility, ...) LOG_Message(severity, facility, __LINE__, __FILE__, FUNCTION_NAME, __VA_ARGS__)
|
||||
#define LOG_FATAL(facility, ...) LOG_Message(LOGS_FATAL, facility, __LINE__, __FILE__, FUNCTION_NAME, __VA_ARGS__)
|
||||
|
||||
/* Definition of severity */
|
||||
typedef enum {
|
||||
LOGS_INFO,
|
||||
LOGS_WARN,
|
||||
LOGS_ERR
|
||||
LOGS_ERR,
|
||||
LOGS_FATAL
|
||||
} LOG_Severity;
|
||||
|
||||
/* Definition of facility. Each message is tagged with who generated
|
||||
|
@ -75,13 +88,9 @@ extern void LOG_Initialise(void);
|
|||
extern void LOG_Finalise(void);
|
||||
|
||||
/* Line logging function */
|
||||
extern void LOG_Line_Function(LOG_Severity severity, LOG_Facility facility, const char *format, ...);
|
||||
|
||||
/* Logging function for fatal errors */
|
||||
extern void LOG_Fatal_Function(LOG_Facility facility, const char *format, ...);
|
||||
|
||||
/* Position in code reporting function */
|
||||
extern void LOG_Position(const char *filename, int line_number, const char *function_name);
|
||||
extern void LOG_Message(LOG_Severity severity, LOG_Facility facility,
|
||||
int line_number, const char *filename,
|
||||
const char *function_name, const char *format, ...);
|
||||
|
||||
/* Log messages to syslog instead of stderr */
|
||||
extern void LOG_OpenSystemLog(void);
|
||||
|
@ -95,16 +104,6 @@ extern void LOG_CloseParentFd(void);
|
|||
/* Return zero once per 10 seconds */
|
||||
extern int LOG_RateLimited(void);
|
||||
|
||||
/* Line logging macro. If the compiler is GNU C, we take advantage of
|
||||
being able to get the function name also. */
|
||||
#if defined(__GNUC__)
|
||||
#define LOG LOG_Position(__FILE__, __LINE__, __FUNCTION__); LOG_Line_Function
|
||||
#define LOG_FATAL LOG_Position(__FILE__, __LINE__, __FUNCTION__); LOG_Fatal_Function
|
||||
#else
|
||||
#define LOG LOG_Position(__FILE__, __LINE__, ""); LOG_Line_Function
|
||||
#define LOG_FATAL LOG_Position(__FILE__, __LINE__, ""); LOG_Fatal_Function
|
||||
#endif /* defined (__GNUC__) */
|
||||
|
||||
/* File logging functions */
|
||||
|
||||
typedef int LOG_FileID;
|
||||
|
|
Loading…
Reference in a new issue