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
|
static void log_message(int fatal, LOG_Severity severity, const char *message)
|
||||||
LOG_Line_Function(LOG_Severity severity, LOG_Facility facility, 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
|
#ifdef WINNT
|
||||||
if (logfile) {
|
if (logfile) {
|
||||||
fprintf(logfile, "%s\n", buf);
|
fprintf(logfile, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (system_log) {
|
if (system_log) {
|
||||||
|
int priority;
|
||||||
switch (severity) {
|
switch (severity) {
|
||||||
case LOGS_INFO:
|
case LOGS_INFO:
|
||||||
syslog(LOG_INFO, "%s", buf);
|
priority = LOG_INFO;
|
||||||
break;
|
break;
|
||||||
case LOGS_WARN:
|
case LOGS_WARN:
|
||||||
syslog(LOG_WARNING, "%s", buf);
|
priority = LOG_WARNING;
|
||||||
break;
|
break;
|
||||||
case LOGS_ERR:
|
case LOGS_ERR:
|
||||||
default:
|
priority = LOG_ERR;
|
||||||
syslog(LOG_ERR, "%s", buf);
|
|
||||||
break;
|
break;
|
||||||
|
case LOGS_FATAL:
|
||||||
|
priority = LOG_CRIT;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
}
|
}
|
||||||
|
syslog(priority, fatal ? "Fatal error : %s" : "%s", message);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%s\n", buf);
|
fprintf(stderr, fatal ? "Fatal error : %s\n" : "%s\n", message);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void LOG_Message(LOG_Severity severity, LOG_Facility facility,
|
||||||
LOG_Fatal_Function(LOG_Facility facility, const char *format, ...)
|
int line_number, const char *filename,
|
||||||
|
const char *function_name, const char *format, ...)
|
||||||
{
|
{
|
||||||
char buf[2048];
|
char buf[2048];
|
||||||
va_list other_args;
|
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;
|
time_t t;
|
||||||
struct tm stm;
|
struct tm stm;
|
||||||
char buf[64];
|
|
||||||
|
#ifdef WINNT
|
||||||
|
#else
|
||||||
if (!system_log) {
|
if (!system_log) {
|
||||||
/* Don't clutter up syslog with internal debugging info */
|
/* Don't clutter up syslog with internal debugging info */
|
||||||
time(&t);
|
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);
|
fprintf(stderr, "%s:%d:(%s)[%s] ", filename, line_number, function_name, buf);
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
#ifndef GOT_LOGGING_H
|
||||||
#define 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 */
|
/* Definition of severity */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LOGS_INFO,
|
LOGS_INFO,
|
||||||
LOGS_WARN,
|
LOGS_WARN,
|
||||||
LOGS_ERR
|
LOGS_ERR,
|
||||||
|
LOGS_FATAL
|
||||||
} LOG_Severity;
|
} LOG_Severity;
|
||||||
|
|
||||||
/* Definition of facility. Each message is tagged with who generated
|
/* Definition of facility. Each message is tagged with who generated
|
||||||
|
@ -75,13 +88,9 @@ extern void LOG_Initialise(void);
|
||||||
extern void LOG_Finalise(void);
|
extern void LOG_Finalise(void);
|
||||||
|
|
||||||
/* Line logging function */
|
/* Line logging function */
|
||||||
extern void LOG_Line_Function(LOG_Severity severity, LOG_Facility facility, const char *format, ...);
|
extern void LOG_Message(LOG_Severity severity, LOG_Facility facility,
|
||||||
|
int line_number, const char *filename,
|
||||||
/* Logging function for fatal errors */
|
const char *function_name, const char *format, ...);
|
||||||
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);
|
|
||||||
|
|
||||||
/* Log messages to syslog instead of stderr */
|
/* Log messages to syslog instead of stderr */
|
||||||
extern void LOG_OpenSystemLog(void);
|
extern void LOG_OpenSystemLog(void);
|
||||||
|
@ -95,16 +104,6 @@ extern void LOG_CloseParentFd(void);
|
||||||
/* Return zero once per 10 seconds */
|
/* Return zero once per 10 seconds */
|
||||||
extern int LOG_RateLimited(void);
|
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 */
|
/* File logging functions */
|
||||||
|
|
||||||
typedef int LOG_FileID;
|
typedef int LOG_FileID;
|
||||||
|
|
Loading…
Reference in a new issue