Skip to content

Commit 9f6bf16

Browse files
committed
Refactored logging. Increased default log buffer size.
1 parent d8d8ba8 commit 9f6bf16

File tree

2 files changed

+115
-109
lines changed

2 files changed

+115
-109
lines changed

logging.c

+78-65
Original file line numberDiff line numberDiff line change
@@ -47,79 +47,92 @@ static void my_log_curses(int prio, const char *datetime, const char *str, bool
4747
}
4848
}
4949

50+
void applog(int prio, const char* fmt, ...)
51+
{
52+
va_list args;
53+
54+
va_start(args, fmt);
55+
vapplogsiz(prio, LOGBUFSIZ, fmt, args);
56+
va_end(args);
57+
}
58+
59+
void applogsiz(int prio, int size, const char* fmt, ...)
60+
{
61+
va_list args;
62+
63+
va_start(args, fmt);
64+
vapplogsiz(prio, size, fmt, args);
65+
va_end(args);
66+
}
67+
5068
/* high-level logging function, based on global opt_log_level */
51-
void applog(int prio, const char* fmt, ...) {
52-
va_list args;
53-
54-
if (opt_debug || prio != LOG_DEBUG) {
55-
if (use_syslog || opt_log_output || prio <= opt_log_level) {
56-
char tmp42[LOGBUFSIZ];
57-
va_start(args, fmt);
58-
vsnprintf(tmp42, sizeof(tmp42), fmt, args);
59-
va_end(args);
60-
_applog(prio, tmp42, false);
61-
}
62-
}
69+
void vapplogsiz(int prio, int size, const char* fmt, va_list args)
70+
{
71+
if (opt_debug || prio != LOG_DEBUG) {
72+
if (use_syslog || opt_log_output || prio <= opt_log_level) {
73+
char *tmp42 = (char *)calloc(size, 1);
74+
vsnprintf(tmp42, size, fmt, args);
75+
_applog(prio, tmp42, false);
76+
free(tmp42);
77+
}
78+
}
6379
}
80+
6481
/*
6582
* log function
6683
*/
6784
void _applog(int prio, const char *str, bool force)
6885
{
6986
#ifdef HAVE_SYSLOG_H
70-
if (use_syslog) {
71-
syslog(prio, "%s", str);
72-
}
87+
if (use_syslog) {
88+
syslog(prio, "%s", str);
89+
}
7390
#else
74-
if (0) {}
91+
if (0) {}
7592
#endif
76-
else {
77-
char datetime[64];
78-
struct timeval tv = {0, 0};
79-
struct tm *tm;
80-
81-
cgtime(&tv);
82-
83-
const time_t tmp_time = tv.tv_sec;
84-
tm = localtime(&tmp_time);
85-
86-
/* Day changed. */
87-
if (opt_log_show_date && (last_date_output_day != tm->tm_mday))
88-
{
89-
last_date_output_day = tm->tm_mday;
90-
char date_output_str[64];
91-
snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
92-
tm->tm_year + 1900,
93-
tm->tm_mon + 1,
94-
tm->tm_mday);
95-
_applog(prio, date_output_str, force);
96-
97-
}
98-
99-
if (opt_log_show_date)
100-
{
101-
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
102-
tm->tm_year + 1900,
103-
tm->tm_mon + 1,
104-
tm->tm_mday,
105-
tm->tm_hour,
106-
tm->tm_min,
107-
tm->tm_sec);
108-
}
109-
else
110-
{
111-
snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
112-
tm->tm_hour,
113-
tm->tm_min,
114-
tm->tm_sec);
115-
}
116-
117-
/* Only output to stderr if it's not going to the screen as well */
118-
if (!isatty(fileno((FILE *)stderr))) {
119-
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
120-
fflush(stderr);
121-
}
122-
123-
my_log_curses(prio, datetime, str, force);
124-
}
93+
else {
94+
char datetime[64];
95+
struct timeval tv = {0, 0};
96+
struct tm *tm;
97+
98+
cgtime(&tv);
99+
100+
const time_t tmp_time = tv.tv_sec;
101+
tm = localtime(&tmp_time);
102+
103+
/* Day changed. */
104+
if (opt_log_show_date && (last_date_output_day != tm->tm_mday)) {
105+
last_date_output_day = tm->tm_mday;
106+
char date_output_str[64];
107+
snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
108+
tm->tm_year + 1900,
109+
tm->tm_mon + 1,
110+
tm->tm_mday);
111+
_applog(prio, date_output_str, force);
112+
}
113+
114+
if (opt_log_show_date) {
115+
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
116+
tm->tm_year + 1900,
117+
tm->tm_mon + 1,
118+
tm->tm_mday,
119+
tm->tm_hour,
120+
tm->tm_min,
121+
tm->tm_sec);
122+
}
123+
else {
124+
snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
125+
tm->tm_hour,
126+
tm->tm_min,
127+
tm->tm_sec);
128+
}
129+
130+
/* Only output to stderr if it's not going to the screen as well */
131+
if (!isatty(fileno((FILE *)stderr))) {
132+
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
133+
fflush(stderr);
134+
}
135+
136+
my_log_curses(prio, datetime, str, force);
137+
}
125138
}

logging.h

+37-44
Original file line numberDiff line numberDiff line change
@@ -28,74 +28,67 @@ extern int opt_log_level;
2828

2929
extern int opt_log_show_date;
3030

31-
#define LOGBUFSIZ 256
31+
#define LOGBUFSIZ 512
3232

3333
void applog(int prio, const char* fmt, ...);
34+
void applogsiz(int prio, int size, const char* fmt, ...);
35+
void vapplogsiz(int prio, int size, const char* fmt, va_list args);
36+
3437
extern void _applog(int prio, const char *str, bool force);
3538

3639
#define IN_FMT_FFL " in %s %s():%d"
3740

38-
#define applogsiz(prio, _SIZ, fmt, ...) do { \
39-
if (opt_debug || prio != LOG_DEBUG) { \
40-
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
41-
char tmp42[_SIZ]; \
42-
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
43-
_applog(prio, tmp42, false); \
44-
} \
45-
} \
46-
} while (0)
47-
4841
#define forcelog(prio, fmt, ...) do { \
49-
if (opt_debug || prio != LOG_DEBUG) { \
50-
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
51-
char tmp42[LOGBUFSIZ]; \
52-
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
53-
_applog(prio, tmp42, true); \
54-
} \
55-
} \
42+
if (opt_debug || prio != LOG_DEBUG) { \
43+
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
44+
char tmp42[LOGBUFSIZ]; \
45+
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
46+
_applog(prio, tmp42, true); \
47+
} \
48+
} \
5649
} while (0)
5750

5851
#define quit(status, fmt, ...) do { \
59-
if (fmt) { \
60-
char tmp42[LOGBUFSIZ]; \
61-
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
62-
_applog(LOG_ERR, tmp42, true); \
63-
} \
64-
_quit(status); \
52+
if (fmt) { \
53+
char tmp42[LOGBUFSIZ]; \
54+
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
55+
_applog(LOG_ERR, tmp42, true); \
56+
} \
57+
_quit(status); \
6558
} while (0)
6659

6760
#define quithere(status, fmt, ...) do { \
68-
if (fmt) { \
69-
char tmp42[LOGBUFSIZ]; \
70-
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
71-
##__VA_ARGS__, __FILE__, __func__, __LINE__); \
72-
_applog(LOG_ERR, tmp42, true); \
73-
} \
74-
_quit(status); \
61+
if (fmt) { \
62+
char tmp42[LOGBUFSIZ]; \
63+
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
64+
##__VA_ARGS__, __FILE__, __func__, __LINE__); \
65+
_applog(LOG_ERR, tmp42, true); \
66+
} \
67+
_quit(status); \
7568
} while (0)
7669

7770
#define quitfrom(status, _file, _func, _line, fmt, ...) do { \
78-
if (fmt) { \
79-
char tmp42[LOGBUFSIZ]; \
80-
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
81-
##__VA_ARGS__, _file, _func, _line); \
82-
_applog(LOG_ERR, tmp42, true); \
83-
} \
84-
_quit(status); \
71+
if (fmt) { \
72+
char tmp42[LOGBUFSIZ]; \
73+
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
74+
##__VA_ARGS__, _file, _func, _line); \
75+
_applog(LOG_ERR, tmp42, true); \
76+
} \
77+
_quit(status); \
8578
} while (0)
8679

8780
#ifdef HAVE_CURSES
8881

8982
#define wlog(fmt, ...) do { \
90-
char tmp42[LOGBUFSIZ]; \
91-
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
92-
_wlog(tmp42); \
83+
char tmp42[LOGBUFSIZ]; \
84+
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
85+
_wlog(tmp42); \
9386
} while (0)
9487

9588
#define wlogprint(fmt, ...) do { \
96-
char tmp42[LOGBUFSIZ]; \
97-
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
98-
_wlogprint(tmp42); \
89+
char tmp42[LOGBUFSIZ]; \
90+
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
91+
_wlogprint(tmp42); \
9992
} while (0)
10093

10194
#endif

0 commit comments

Comments
 (0)