author | Tero Marttila <terom@fixme.fi> |
Thu, 28 May 2009 00:35:02 +0300 | |
branch | new-lib-errors |
changeset 218 | 5229a5d098b2 |
parent 217 | 7728d6ec3abf |
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> |
218
5229a5d098b2
some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents:
217
diff
changeset
|
8 |
#include <stdlib.h> |
21 | 9 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
10 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
11 |
* The default log output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
12 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
13 |
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
|
14 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
15 |
(void) arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
16 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
17 |
// display |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
18 |
printf("%s\n", line); |
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 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
21 |
/** |
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
22 |
* 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
|
23 |
*/ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
24 |
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
|
25 |
|
104
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
26 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
27 |
* The global log output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
28 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
29 |
static struct log_output_ctx { |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
30 |
/** The function itself */ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
31 |
log_output_func func; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
32 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
33 |
/** The arg */ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
34 |
void *arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
35 |
} _log_output_ctx = { log_default_func, NULL }; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
36 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
37 |
/** |
104
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
38 |
* List of log level names |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
39 |
*/ |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
40 |
const char *log_level_names[] = { |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
41 |
"DEBUG", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
42 |
"INFO", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
43 |
"WARN", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
44 |
"ERROR", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
45 |
"FATAL", |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
46 |
NULL |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
47 |
}; |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
48 |
|
21 | 49 |
#define _LOG_LEVEL_NAME(ll) case LOG_ ## ll: return #ll; |
50 |
const char *log_level_name (enum log_level level) |
|
51 |
{ |
|
52 |
switch (level) { |
|
53 |
_LOG_LEVEL_NAME(DEBUG) |
|
54 |
_LOG_LEVEL_NAME(INFO) |
|
55 |
_LOG_LEVEL_NAME(WARN) |
|
56 |
_LOG_LEVEL_NAME(ERROR) |
|
57 |
_LOG_LEVEL_NAME(FATAL) |
|
58 |
default: return "???"; |
|
59 |
} |
|
60 |
} |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
61 |
#undef _LOG_LEVEL_NAME |
21 | 62 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
63 |
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
|
64 |
{ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
65 |
// meep meep |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
66 |
_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
|
67 |
} |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
68 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
69 |
void log_set_func (log_output_func func, void *arg) |
21 | 70 |
{ |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
71 |
// replace the current one |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
72 |
_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
|
73 |
_log_output_ctx.arg = arg; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
74 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
75 |
|
216 | 76 |
void log_output_tag_va (enum log_level level, const char *tag, const char *func, const char *user_fmt, va_list user_fmtargs, const char *log_fmt, va_list log_fmtargs) |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
77 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
78 |
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
|
79 |
size_t buf_size = sizeof(buf); |
21 | 80 |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
81 |
// filter out? |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
82 |
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
|
83 |
return; |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
84 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
85 |
// 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
|
86 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt(buf_ptr, buf_size, "[%5s] %20s : ", tag, func)); |
21 | 87 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
88 |
// output the user data |
209
d240da5dbeb5
accept NULL for log function fmt arg
Tero Marttila <terom@fixme.fi>
parents:
195
diff
changeset
|
89 |
if (user_fmt) |
d240da5dbeb5
accept NULL for log function fmt arg
Tero Marttila <terom@fixme.fi>
parents:
195
diff
changeset
|
90 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt_va(buf_ptr, buf_size, user_fmt, user_fmtargs)); |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
91 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
92 |
// output the suffix |
216 | 93 |
if (log_fmt) |
94 |
buf_ptr += str_advance(NULL, &buf_size, str_append_fmt_va(buf_ptr, buf_size, log_fmt, log_fmtargs)); |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
95 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
96 |
// send it to the output func |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
97 |
_log_output_ctx.func(buf, _log_output_ctx.arg); |
216 | 98 |
|
99 |
} |
|
100 |
||
101 |
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, ...) |
|
102 |
{ |
|
103 |
va_list vargs; |
|
104 |
||
105 |
va_start(vargs, log_fmt); |
|
106 |
log_output_tag_va(level, tag, func, user_fmt, user_fmtargs, log_fmt, vargs); |
|
107 |
va_end(vargs); |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
108 |
} |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
109 |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
110 |
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
|
111 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
112 |
va_list vargs; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
113 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
114 |
// formatted output: no suffix |
21 | 115 |
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
|
116 |
log_output_tag(level, log_level_name(level), func, format, vargs, NULL); |
21 | 117 |
va_end(vargs); |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
118 |
} |
21 | 119 |
|
216 | 120 |
void _log_msg_va2 (enum log_level level, const char *func, const char *fmt1, va_list fmtargs1, const char *fmt2, va_list fmtargs2) |
121 |
{ |
|
122 |
log_output_tag_va(level, log_level_name(level), func, fmt1, fmtargs1, fmt2, fmtargs2); |
|
123 |
} |
|
124 |
||
217 | 125 |
void _log_err (enum log_level level, const struct error_list *list, err_t code, const char *func, const char *format, ...) |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
126 |
{ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
127 |
va_list vargs; |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
128 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
129 |
// formatted output: suffix error_name() |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
130 |
va_start(vargs, format); |
217 | 131 |
log_output_tag(level, log_level_name(level), func, format, vargs, ": %s", error_name(list, code)); |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
132 |
va_end(vargs); |
21 | 133 |
} |
134 |
||
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
156
diff
changeset
|
135 |
void _log_error (enum log_level level, const error_t *err, const char *func, const char *format, ...) |
21 | 136 |
{ |
137 |
va_list vargs; |
|
138 |
||
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
139 |
// formatted output: suffix error_msg() |
21 | 140 |
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
|
141 |
log_output_tag(level, log_level_name(level), func, format, vargs, ": %s", error_msg(err)); |
21 | 142 |
va_end(vargs); |
143 |
} |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
144 |
|
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
145 |
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
|
146 |
{ |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
147 |
va_list vargs; |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
148 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
149 |
// formatted output: suffix strerror() |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
va_end(vargs); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
153 |
} |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
154 |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
155 |
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
|
156 |
{ |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
157 |
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
|
158 |
|
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
159 |
// 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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
|
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
164 |
// exit |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
165 |
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
|
166 |
} |