author | Tero Marttila <terom@fixme.fi> |
Thu, 21 May 2009 16:57:56 +0300 | |
changeset 213 | f0e52e026197 |
parent 195 | 42aedce3e2eb |
child 216 | a10ba529ae39 |
permissions | -rw-r--r-- |
21 | 1 |
#ifndef LOG_H |
2 |
#define LOG_H |
|
3 |
||
4 |
/** @file log.h |
|
5 |
* |
|
6 |
* Local logging functions |
|
7 |
*/ |
|
8 |
#include "error.h" |
|
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
9 |
#include <stdarg.h> |
21 | 10 |
|
11 |
/** |
|
12 |
* Log level definitions |
|
13 |
* |
|
14 |
* XXX: these names conflict with <syslog.h> |
|
15 |
*/ |
|
16 |
enum log_level { |
|
17 |
LOG_DEBUG, |
|
18 |
LOG_INFO, |
|
19 |
LOG_WARN, |
|
20 |
LOG_ERROR, |
|
21 |
LOG_FATAL, |
|
22 |
}; |
|
23 |
||
24 |
/** |
|
104
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
25 |
* List of log_level values as strings, NULL-terminated |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
26 |
*/ |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
27 |
extern const char *log_level_names[]; |
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
28 |
|
fc196bb4bcc2
implement lua_log and lua_log_level
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
29 |
/** |
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
30 |
* The default log level |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
31 |
*/ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
32 |
#define LOG_LEVEL_DEFAULT LOG_INFO |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
33 |
|
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
34 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
35 |
* Maximum length of a log message, including terminating NUL |
21 | 36 |
*/ |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
37 |
#define LOG_MSG_MAX 1024 |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
38 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
39 |
/** |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
40 |
* Log output function, for displaying a line of text, without any trailing newline. |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
41 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
42 |
typedef void (*log_output_func) (const char *line, void *arg); |
21 | 43 |
|
44 |
/** |
|
73
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
45 |
* Set the current log level to filter out messages below the given level |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
46 |
*/ |
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
47 |
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
|
48 |
|
2780a73c71f3
add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents:
22
diff
changeset
|
49 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
50 |
* Set the current log output function, replacing any default or previous value. |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
51 |
* |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
52 |
* Pass in func as NULL to revert to the default handler. |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
53 |
*/ |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
54 |
void log_set_func (log_output_func func, void *arg); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
55 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
56 |
/** |
194
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
57 |
* Internal logging func, meant for using custom level tags. This performs the filtering of log levels. |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
58 |
* |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
59 |
* Format the full output line, and pass it to the log_output_func. The line will be of the form: |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
60 |
* "[<tag>] <func> : <user_fmt> [ <log_fmt> ]" |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
61 |
*/ |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
62 |
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, ...) |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
63 |
__attribute__ ((format (printf, 6, 7))); |
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
64 |
|
808b1b047620
add a new test/fail module, and rewrite test/assert to use it
Tero Marttila <terom@fixme.fi>
parents:
172
diff
changeset
|
65 |
/** |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
66 |
* Log a message with the given level |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
67 |
*/ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
68 |
#define log_msg(level, ...) _log_msg(level, __func__, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
69 |
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
|
70 |
__attribute__ ((format (printf, 3, 4))); |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
71 |
|
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
72 |
/** |
21 | 73 |
* Shorthand for log_msg |
74 |
*/ |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
75 |
#define log_debug(...) log_msg(LOG_DEBUG, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
76 |
#define log_info(...) log_msg(LOG_INFO, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
77 |
#define log_warn(...) log_msg(LOG_WARN, __VA_ARGS__) |
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
162
diff
changeset
|
78 |
/* #define log_error(...) log_msg(LOG_ERROR, __func__, __VA_ARGS__) */ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
79 |
#define log_fatal(...) log_msg(LOG_FATAL, __VA_ARGS__) |
21 | 80 |
|
81 |
/** |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
82 |
* Log a message with the given level, appending the formatted error code name |
21 | 83 |
*/ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
84 |
#define log_err(err_code, ...) _log_err(LOG_ERROR, err_code, __func__, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
85 |
#define log_warn_err(err_code, ...) _log_err(LOG_WARN, err_code, __func__, __VA_ARGS__) |
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
162
diff
changeset
|
86 |
void _log_err (enum log_level level, err_t err, const char *func, const char *format, ...) |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
87 |
__attribute__ ((format (printf, 4, 5))); |
21 | 88 |
|
137
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 |
* Log a message with the given level, appending the formatted error message |
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
91 |
*/ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
92 |
#define log_error(err_info, ...) _log_error(LOG_ERROR, err_info, __func__, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
93 |
#define log_warn_error(err_info, ...) _log_error(LOG_WARN, err_info, __func__, __VA_ARGS__) |
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
162
diff
changeset
|
94 |
void _log_error (enum log_level level, const error_t *err, const char *func, const char *format, ...) |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
95 |
__attribute__ ((format (printf, 4, 5))); |
21 | 96 |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
97 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
98 |
* Log using errno. |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
99 |
*/ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
100 |
#define log_perr(...) _log_perr(LOG_ERROR, __func__, __VA_ARGS__) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
101 |
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
|
102 |
__attribute__ ((format (printf, 3, 4))); |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
103 |
|
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
104 |
/** |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
105 |
* Log with an [EXIT] tag at given level, and then exit with given exit code |
137
c607c357c486
implement console_print and log_set_func
Tero Marttila <terom@fixme.fi>
parents:
118
diff
changeset
|
106 |
*/ |
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
107 |
#define log_exit(level, exit_code, ...) _log_exit(level, exit_code, __func__, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
108 |
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
|
109 |
__attribute__ ((format (printf, 4, 5))) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
110 |
__attribute__ ((noreturn)); |
21 | 111 |
|
139
55b9dcc2b73a
implement sock_ssl_connect_async (the old sock_ssl_connect exists no more)
Tero Marttila <terom@fixme.fi>
parents:
137
diff
changeset
|
112 |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
113 |
/** |
21 | 114 |
* log_fatal + exit failure |
115 |
*/ |
|
162
fa8dc83739e2
replace exit(EXIT_FAILURE) with abort() in FATAL_*
Tero Marttila <terom@fixme.fi>
parents:
156
diff
changeset
|
116 |
#define FATAL(...) do { log_fatal(__VA_ARGS__); abort(); } while (0) |
21 | 117 |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
118 |
/** |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
119 |
* log_err + exit failure |
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
120 |
*/ |
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
162
diff
changeset
|
121 |
#define FATAL_ERR(err_code, ...) do { _log_err(LOG_FATAL, err_code, __func__, __VA_ARGS__); abort(); } while (0) |
22
c339c020fd33
add missing irc_cmd.h, and modify line_proto/irc_conn to use log
Tero Marttila <terom@fixme.fi>
parents:
21
diff
changeset
|
122 |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
123 |
/** |
21 | 124 |
* log_err_info + exit failure |
125 |
*/ |
|
172
ea4972e51fa3
fix log_warn_*/log_err/log_error naming
Tero Marttila <terom@fixme.fi>
parents:
162
diff
changeset
|
126 |
#define FATAL_ERROR(err_info, ...) do { _log_error(LOG_FATAL, err_info, __func__, __VA_ARGS__); abort(); } while (0) |
21 | 127 |
|
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
128 |
/** |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
129 |
* log_perr + exit failure |
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
130 |
*/ |
162
fa8dc83739e2
replace exit(EXIT_FAILURE) with abort() in FATAL_*
Tero Marttila <terom@fixme.fi>
parents:
156
diff
changeset
|
131 |
#define FATAL_PERROR(...) do { _log_perr(LOG_FATAL, __func__, __VA_ARGS__); abort(); } while (0) |
118
05b8d5150313
implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents:
104
diff
changeset
|
132 |
|
195
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
133 |
/** |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
134 |
* Exit with given code, also logging a message at LOG_INFO with anĀ [EXIT] tag |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
135 |
*/ |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
136 |
#define EXIT_INFO(exit_code, ...) log_exit(LOG_INFO, exit_code, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
137 |
#define EXIT_WARN(exit_code, ...) log_exit(LOG_WARN, exit_code, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
138 |
#define EXIT_ERROR(exit_code, ...) log_exit(LOG_ERROR, exit_code, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
139 |
#define EXIT_FATAL(exit_code, ...) log_exit(LOG_FATAL, exit_code, __VA_ARGS__) |
42aedce3e2eb
rework test to implement flags, test_results, test_stats, TEST_WILL_FAIL
Tero Marttila <terom@fixme.fi>
parents:
194
diff
changeset
|
140 |
|
21 | 141 |
#endif /* LOG_H */ |