Browse Source

Refactored logging. Increased default log buffer size.

djm34
troky 11 years ago
parent
commit
9f6bf16196
  1. 143
      logging.c
  2. 81
      logging.h

143
logging.c

@ -47,79 +47,92 @@ static void my_log_curses(int prio, const char *datetime, const char *str, bool
} }
} }
void applog(int prio, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vapplogsiz(prio, LOGBUFSIZ, fmt, args);
va_end(args);
}
void applogsiz(int prio, int size, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vapplogsiz(prio, size, fmt, args);
va_end(args);
}
/* high-level logging function, based on global opt_log_level */ /* high-level logging function, based on global opt_log_level */
void applog(int prio, const char* fmt, ...) { void vapplogsiz(int prio, int size, const char* fmt, va_list args)
va_list args; {
if (opt_debug || prio != LOG_DEBUG) {
if (opt_debug || prio != LOG_DEBUG) { if (use_syslog || opt_log_output || prio <= opt_log_level) {
if (use_syslog || opt_log_output || prio <= opt_log_level) { char *tmp42 = (char *)calloc(size, 1);
char tmp42[LOGBUFSIZ]; vsnprintf(tmp42, size, fmt, args);
va_start(args, fmt); _applog(prio, tmp42, false);
vsnprintf(tmp42, sizeof(tmp42), fmt, args); free(tmp42);
va_end(args); }
_applog(prio, tmp42, false); }
}
}
} }
/* /*
* log function * log function
*/ */
void _applog(int prio, const char *str, bool force) void _applog(int prio, const char *str, bool force)
{ {
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H
if (use_syslog) { if (use_syslog) {
syslog(prio, "%s", str); syslog(prio, "%s", str);
} }
#else #else
if (0) {} if (0) {}
#endif #endif
else { else {
char datetime[64]; char datetime[64];
struct timeval tv = {0, 0}; struct timeval tv = {0, 0};
struct tm *tm; struct tm *tm;
cgtime(&tv); cgtime(&tv);
const time_t tmp_time = tv.tv_sec; const time_t tmp_time = tv.tv_sec;
tm = localtime(&tmp_time); tm = localtime(&tmp_time);
/* Day changed. */ /* Day changed. */
if (opt_log_show_date && (last_date_output_day != tm->tm_mday)) if (opt_log_show_date && (last_date_output_day != tm->tm_mday)) {
{ last_date_output_day = tm->tm_mday;
last_date_output_day = tm->tm_mday; char date_output_str[64];
char date_output_str[64]; snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d", tm->tm_year + 1900,
tm->tm_year + 1900, tm->tm_mon + 1,
tm->tm_mon + 1, tm->tm_mday);
tm->tm_mday); _applog(prio, date_output_str, force);
_applog(prio, date_output_str, force); }
} if (opt_log_show_date) {
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
if (opt_log_show_date) tm->tm_year + 1900,
{ tm->tm_mon + 1,
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ", tm->tm_mday,
tm->tm_year + 1900, tm->tm_hour,
tm->tm_mon + 1, tm->tm_min,
tm->tm_mday, tm->tm_sec);
tm->tm_hour, }
tm->tm_min, else {
tm->tm_sec); snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
} tm->tm_hour,
else tm->tm_min,
{ tm->tm_sec);
snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ", }
tm->tm_hour,
tm->tm_min, /* Only output to stderr if it's not going to the screen as well */
tm->tm_sec); if (!isatty(fileno((FILE *)stderr))) {
} fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
fflush(stderr);
/* Only output to stderr if it's not going to the screen as well */ }
if (!isatty(fileno((FILE *)stderr))) {
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */ my_log_curses(prio, datetime, str, force);
fflush(stderr); }
}
my_log_curses(prio, datetime, str, force);
}
} }

81
logging.h

@ -28,74 +28,67 @@ extern int opt_log_level;
extern int opt_log_show_date; extern int opt_log_show_date;
#define LOGBUFSIZ 256 #define LOGBUFSIZ 512
void applog(int prio, const char* fmt, ...); void applog(int prio, const char* fmt, ...);
void applogsiz(int prio, int size, const char* fmt, ...);
void vapplogsiz(int prio, int size, const char* fmt, va_list args);
extern void _applog(int prio, const char *str, bool force); extern void _applog(int prio, const char *str, bool force);
#define IN_FMT_FFL " in %s %s():%d" #define IN_FMT_FFL " in %s %s():%d"
#define applogsiz(prio, _SIZ, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
char tmp42[_SIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(prio, tmp42, false); \
} \
} \
} while (0)
#define forcelog(prio, fmt, ...) do { \ #define forcelog(prio, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \ if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \ if (use_syslog || opt_log_output || prio <= opt_log_level) { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(prio, tmp42, true); \ _applog(prio, tmp42, true); \
} \ } \
} \ } \
} while (0) } while (0)
#define quit(status, fmt, ...) do { \ #define quit(status, fmt, ...) do { \
if (fmt) { \ if (fmt) { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(LOG_ERR, tmp42, true); \ _applog(LOG_ERR, tmp42, true); \
} \ } \
_quit(status); \ _quit(status); \
} while (0) } while (0)
#define quithere(status, fmt, ...) do { \ #define quithere(status, fmt, ...) do { \
if (fmt) { \ if (fmt) { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \ snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, __FILE__, __func__, __LINE__); \ ##__VA_ARGS__, __FILE__, __func__, __LINE__); \
_applog(LOG_ERR, tmp42, true); \ _applog(LOG_ERR, tmp42, true); \
} \ } \
_quit(status); \ _quit(status); \
} while (0) } while (0)
#define quitfrom(status, _file, _func, _line, fmt, ...) do { \ #define quitfrom(status, _file, _func, _line, fmt, ...) do { \
if (fmt) { \ if (fmt) { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \ snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, _file, _func, _line); \ ##__VA_ARGS__, _file, _func, _line); \
_applog(LOG_ERR, tmp42, true); \ _applog(LOG_ERR, tmp42, true); \
} \ } \
_quit(status); \ _quit(status); \
} while (0) } while (0)
#ifdef HAVE_CURSES #ifdef HAVE_CURSES
#define wlog(fmt, ...) do { \ #define wlog(fmt, ...) do { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlog(tmp42); \ _wlog(tmp42); \
} while (0) } while (0)
#define wlogprint(fmt, ...) do { \ #define wlogprint(fmt, ...) do { \
char tmp42[LOGBUFSIZ]; \ char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \ snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlogprint(tmp42); \ _wlogprint(tmp42); \
} while (0) } while (0)
#endif #endif

Loading…
Cancel
Save