author | Tero Marttila <terom@fixme.fi> |
Fri, 08 May 2009 02:51:20 +0300 | |
changeset 195 | 42aedce3e2eb |
parent 194 | 808b1b047620 |
child 209 | d240da5dbeb5 |
permissions | -rw-r--r-- |
21 | 1 |
|
2 |
#include "log.h" |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
3 |
#include "str.h" |
21 | 4 |
|
5 |
#include <stdio.h> |
|
6 |
#include <stdarg.h> |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
7 |
#include <string.h> |
21 | 8 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
9 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
10 |
* The default log output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
11 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
12 |
void log_default_func (const char *line, void *arg) |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
13 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
14 |
(void) arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
15 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
16 |
// display |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
17 |
printf("%s\n", line); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
18 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
19 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
20 |
/** |
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
21 |
* The global log level |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
22 |
*/ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
23 |
static enum log_level _log_level = LOG_LEVEL_DEFAULT; |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
24 |
|
104
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
25 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
26 |
* The global log output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
27 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
28 |
static struct log_output_ctx { |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
29 |
/** The function itself */ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
30 |
log_output_func func; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
31 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
32 |
/** The arg */ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
33 |
void *arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
34 |
} _log_output_ctx = { log_default_func, NULL }; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
35 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
36 |
/** |
104
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
37 |
* List of log level names |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
38 |
*/ |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
39 |
const char *log_level_names[] = { |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
40 |
"DEBUG", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
41 |
"INFO", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
42 |
"WARN", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
43 |
"ERROR", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
44 |
"FATAL", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
45 |
NULL |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
46 |
}; |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
47 |
|
21 | 48 |
#define _LOG_LEVEL_NAME(ll) case LOG_ ## ll: return #ll; |
49 |
const char *log_level_name (enum log_level level) |
|
50 |
{ |
|
51 |
switch (level) { |
|
52 |
_LOG_LEVEL_NAME(DEBUG) |
|
53 |
_LOG_LEVEL_NAME(INFO) |
|
54 |
_LOG_LEVEL_NAME(WARN) |
|
55 |
_LOG_LEVEL_NAME(ERROR) |
|
56 |
_LOG_LEVEL_NAME(FATAL) |
|
57 |
default: return "???"; |
|
58 |
} |
|
59 |
} |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
60 |
#undef _LOG_LEVEL_NAME |
21 | 61 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
62 |
void set_log_level (enum log_level level) |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
63 |
{ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
64 |
// meep meep |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
65 |
_log_level = level; |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
66 |
} |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
67 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
68 |
void log_set_func (log_output_func func, void *arg) |
21 | 69 |
{ |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
70 |
// replace the current one |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
71 |
_log_output_ctx.func = func ? func : log_default_func; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
72 |
_log_output_ctx.arg = arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
73 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
74 |
|
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
75 |
void log_output_tag (enum log_level level, const char *tag, const char *func, const char *user_fmt, va_list user_fmtargs, const char *log_fmt, ...) |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
76 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
77 |
char buf[LOG_MSG_MAX], *buf_ptr = buf; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
78 |
size_t buf_size = sizeof(buf); |
21 | 79 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
80 |
// filter out? |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
81 |
if (level < _log_level) |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
82 |
return; |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
83 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
84 |
// output the header |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
85 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt(buf_ptr, buf_size, "[%5s] %20s : ", tag, func)); |
21 | 86 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
87 |
// output the user data |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
88 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt_va(buf_ptr, buf_size, user_fmt, user_fmtargs)); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
89 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
90 |
// output the suffix |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
91 |
if (log_fmt) { |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
92 |
va_list vargs; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
93 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
94 |
va_start(vargs, log_fmt); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
95 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt_va(buf_ptr, buf_size, log_fmt, vargs)); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
96 |
va_end(vargs); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
97 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
98 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
99 |
// send it to the output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
100 |
_log_output_ctx.func(buf, _log_output_ctx.arg); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
101 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
102 |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
103 |
void _log_msg (enum log_level level, const char *func, const char *format, ...) |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
104 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
105 |
va_list vargs; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
106 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
107 |
// formatted output: no suffix |
21 | 108 |
va_start(vargs, format); |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
109 |
log_output_tag(level, log_level_name(level), func, format, vargs, NULL); |
21 | 110 |
va_end(vargs); |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
111 |
} |
21 | 112 |
|
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
156
diff
changeset
|
113 |
void _log_err (enum log_level level, err_t err, const char *func, const char *format, ...) |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
114 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
115 |
va_list vargs; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
116 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
117 |
// formatted output: suffix error_name() |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
118 |
va_start(vargs, format); |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
119 |
log_output_tag(level, log_level_name(level), func, format, vargs, ": %s", error_name(err)); |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
120 |
va_end(vargs); |
21 | 121 |
} |
122 |
||
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
156
diff
changeset
|
123 |
void _log_error (enum log_level level, const error_t *err, const char *func, const char *format, ...) |
21 | 124 |
{ |
125 |
va_list vargs; |
|
126 |
||
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
127 |
// formatted output: suffix error_msg() |
21 | 128 |
va_start(vargs, format); |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
129 |
log_output_tag(level, log_level_name(level), func, format, vargs, ": %s", error_msg(err)); |
21 | 130 |
va_end(vargs); |
131 |
} |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
132 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
133 |
void _log_perr (enum log_level level, const char *func, const char *format, ...) |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
134 |
{ |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
135 |
va_list vargs; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
136 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
137 |
// formatted output: suffix strerror() |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
138 |
va_start(vargs, format); |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
139 |
log_output_tag(level, log_level_name(level), func, format, vargs, ": %s", strerror(errno)); |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
140 |
va_end(vargs); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
141 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
142 |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
143 |
void _log_exit (enum log_level level, int exit_code, const char *func, const char *format, ...) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
144 |
{ |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
145 |
va_list vargs; |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
146 |
|
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
147 |
// formatted output without any suffix |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
148 |
va_start(vargs, format); |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
149 |
log_output_tag(level, "EXIT", func, format, vargs, NULL); |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
150 |
va_end(vargs); |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
151 |
|
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
152 |
// exit |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
153 |
exit(exit_code); |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
154 |
} |