]>
Commit | Line | Data |
---|---|---|
03467c88 | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5899f3b7 | 2 | |
c2f1db8f | 3 | #pragma once |
5899f3b7 | 4 | |
a7334b09 LP |
5 | /*** |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2010 Lennart Poettering | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
11 | under the terms of the GNU Lesser General Public License as published by |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
13 | (at your option) any later version. |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 18 | Lesser General Public License for more details. |
a7334b09 | 19 | |
5430f7f2 | 20 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
22 | ***/ | |
23 | ||
07630cea | 24 | #include <errno.h> |
17a94911 | 25 | #include <stdarg.h> |
07630cea | 26 | #include <stdbool.h> |
6410074b | 27 | #include <stdlib.h> |
4daf54a8 | 28 | #include <sys/signalfd.h> |
07630cea | 29 | #include <syslog.h> |
5899f3b7 | 30 | |
20ad4cfd | 31 | #include "sd-id128.h" |
07630cea | 32 | |
158350e8 | 33 | #include "macro.h" |
5899f3b7 | 34 | |
16801e90 LP |
35 | typedef enum LogTarget{ |
36 | LOG_TARGET_CONSOLE, | |
aca83a53 | 37 | LOG_TARGET_CONSOLE_PREFIXED, |
16801e90 | 38 | LOG_TARGET_KMSG, |
5ba081b0 LP |
39 | LOG_TARGET_JOURNAL, |
40 | LOG_TARGET_JOURNAL_OR_KMSG, | |
843d2643 LP |
41 | LOG_TARGET_SYSLOG, |
42 | LOG_TARGET_SYSLOG_OR_KMSG, | |
5ba081b0 | 43 | LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */ |
a6903061 | 44 | LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */ |
9fae33d2 | 45 | LOG_TARGET_NULL, |
16801e90 LP |
46 | _LOG_TARGET_MAX, |
47 | _LOG_TARGET_INVALID = -1 | |
48 | } LogTarget; | |
49 | ||
50 | void log_set_target(LogTarget target); | |
51 | void log_set_max_level(int level); | |
3eff4208 | 52 | void log_set_facility(int facility); |
16801e90 | 53 | |
34f0e866 LP |
54 | int log_set_target_from_string(const char *e); |
55 | int log_set_max_level_from_string(const char *e); | |
56 | ||
bbe63281 | 57 | void log_show_color(bool b); |
b1e90ec5 | 58 | bool log_get_show_color(void) _pure_; |
bbe63281 | 59 | void log_show_location(bool b); |
b1e90ec5 | 60 | bool log_get_show_location(void) _pure_; |
bbe63281 LP |
61 | |
62 | int log_show_color_from_string(const char *e); | |
63 | int log_show_location_from_string(const char *e); | |
64 | ||
44a6b1b6 ZJS |
65 | LogTarget log_get_target(void) _pure_; |
66 | int log_get_max_level(void) _pure_; | |
1adf1049 | 67 | |
843d2643 | 68 | int log_open(void); |
871e5809 | 69 | void log_close(void); |
4d8a7798 | 70 | void log_forget_fds(void); |
843d2643 | 71 | |
16801e90 | 72 | void log_close_syslog(void); |
5ba081b0 | 73 | void log_close_journal(void); |
843d2643 LP |
74 | void log_close_kmsg(void); |
75 | void log_close_console(void); | |
16801e90 | 76 | |
34f0e866 LP |
77 | void log_parse_environment(void); |
78 | ||
79008bdd | 79 | int log_internal( |
877d54e9 | 80 | int level, |
086891e5 | 81 | int error, |
79008bdd | 82 | const char *file, |
877d54e9 LP |
83 | int line, |
84 | const char *func, | |
086891e5 | 85 | const char *format, ...) _printf_(6,7); |
5899f3b7 | 86 | |
79008bdd | 87 | int log_internalv( |
877d54e9 | 88 | int level, |
086891e5 | 89 | int error, |
79008bdd | 90 | const char *file, |
877d54e9 LP |
91 | int line, |
92 | const char *func, | |
93 | const char *format, | |
086891e5 | 94 | va_list ap) _printf_(6,0); |
877d54e9 | 95 | |
79008bdd | 96 | int log_object_internal( |
fdf9f9bb | 97 | int level, |
086891e5 | 98 | int error, |
79008bdd | 99 | const char *file, |
fdf9f9bb ZJS |
100 | int line, |
101 | const char *func, | |
79008bdd | 102 | const char *object_field, |
fdf9f9bb | 103 | const char *object, |
086891e5 | 104 | const char *format, ...) _printf_(8,9); |
fdf9f9bb | 105 | |
79008bdd | 106 | int log_object_internalv( |
fdf9f9bb | 107 | int level, |
086891e5 | 108 | int error, |
fdf9f9bb ZJS |
109 | const char*file, |
110 | int line, | |
111 | const char *func, | |
79008bdd | 112 | const char *object_field, |
fdf9f9bb ZJS |
113 | const char *object, |
114 | const char *format, | |
086891e5 | 115 | va_list ap) _printf_(8,0); |
fdf9f9bb | 116 | |
877d54e9 LP |
117 | int log_struct_internal( |
118 | int level, | |
086891e5 | 119 | int error, |
877d54e9 LP |
120 | const char *file, |
121 | int line, | |
122 | const char *func, | |
086891e5 | 123 | const char *format, ...) _printf_(6,0) _sentinel_; |
877d54e9 LP |
124 | |
125 | int log_oom_internal( | |
126 | const char *file, | |
127 | int line, | |
128 | const char *func); | |
185986c6 | 129 | |
2149e37c LP |
130 | /* This modifies the buffer passed! */ |
131 | int log_dump_internal( | |
877d54e9 | 132 | int level, |
086891e5 | 133 | int error, |
79008bdd | 134 | const char *file, |
877d54e9 LP |
135 | int line, |
136 | const char *func, | |
137 | char *buffer); | |
138 | ||
086891e5 | 139 | /* Logging for various assertions */ |
919ce0b7 | 140 | noreturn void log_assert_failed( |
877d54e9 LP |
141 | const char *text, |
142 | const char *file, | |
143 | int line, | |
144 | const char *func); | |
145 | ||
919ce0b7 | 146 | noreturn void log_assert_failed_unreachable( |
877d54e9 LP |
147 | const char *text, |
148 | const char *file, | |
149 | int line, | |
150 | const char *func); | |
80514f9c LP |
151 | |
152 | void log_assert_failed_return( | |
153 | const char *text, | |
154 | const char *file, | |
155 | int line, | |
156 | const char *func); | |
2149e37c | 157 | |
086891e5 | 158 | /* Logging with level */ |
1cfa9a4c LP |
159 | #define log_full_errno(level, error, ...) \ |
160 | ({ \ | |
161 | int _level = (level), _e = (error); \ | |
162 | (log_get_max_level() >= LOG_PRI(_level)) \ | |
163 | ? log_internal(_level, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \ | |
164 | : -abs(_e); \ | |
bf371116 | 165 | }) |
87ff6b1c | 166 | |
086891e5 LP |
167 | #define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__) |
168 | ||
169 | /* Normal logging */ | |
4104970e ZJS |
170 | #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) |
171 | #define log_info(...) log_full(LOG_INFO, __VA_ARGS__) | |
172 | #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__) | |
173 | #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__) | |
174 | #define log_error(...) log_full(LOG_ERR, __VA_ARGS__) | |
175 | #define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__) | |
5899f3b7 | 176 | |
086891e5 LP |
177 | /* Logging triggered by an errno-like error */ |
178 | #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__) | |
179 | #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__) | |
180 | #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__) | |
181 | #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__) | |
182 | #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__) | |
b4d23205 | 183 | #define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__) |
086891e5 | 184 | |
cb41ff29 ZJS |
185 | #ifdef LOG_TRACE |
186 | # define log_trace(...) log_debug(__VA_ARGS__) | |
187 | #else | |
188 | # define log_trace(...) do {} while(0) | |
189 | #endif | |
190 | ||
086891e5 LP |
191 | /* Structured logging */ |
192 | #define log_struct(level, ...) log_struct_internal(level, 0, __FILE__, __LINE__, __func__, __VA_ARGS__) | |
193 | #define log_struct_errno(level, error, ...) log_struct_internal(level, error, __FILE__, __LINE__, __func__, __VA_ARGS__) | |
0d0f0c50 | 194 | |
2149e37c | 195 | /* This modifies the buffer passed! */ |
086891e5 LP |
196 | #define log_dump(level, buffer) log_dump_internal(level, 0, __FILE__, __LINE__, __func__, buffer) |
197 | ||
198 | #define log_oom() log_oom_internal(__FILE__, __LINE__, __func__) | |
2149e37c | 199 | |
44a6b1b6 | 200 | bool log_on_console(void) _pure_; |
81270860 | 201 | |
44a6b1b6 ZJS |
202 | const char *log_target_to_string(LogTarget target) _const_; |
203 | LogTarget log_target_from_string(const char *s) _pure_; | |
706911fb | 204 | |
e2cc6eca LP |
205 | /* Helpers to prepare various fields for structured logging */ |
206 | #define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__ | |
207 | #define LOG_MESSAGE_ID(x) "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(x) | |
4daf54a8 ZJS |
208 | |
209 | void log_received_signal(int level, const struct signalfd_siginfo *si); | |
c1dc6153 LP |
210 | |
211 | void log_set_upgrade_syslog_to_journal(bool b); | |
158350e8 LP |
212 | |
213 | int log_syntax_internal( | |
214 | const char *unit, | |
215 | int level, | |
216 | const char *config_file, | |
217 | unsigned config_line, | |
218 | int error, | |
219 | const char *file, | |
220 | int line, | |
221 | const char *func, | |
222 | const char *format, ...) _printf_(9, 10); | |
223 | ||
224 | #define log_syntax(unit, level, config_file, config_line, error, ...) \ | |
225 | ({ \ | |
226 | int _level = (level), _e = (error); \ | |
227 | (log_get_max_level() >= LOG_PRI(_level)) \ | |
228 | ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \ | |
229 | : -abs(_e); \ | |
230 | }) | |
0e05ee04 LP |
231 | |
232 | #define log_syntax_invalid_utf8(unit, level, config_file, config_line, rvalue) \ | |
233 | ({ \ | |
234 | int _level = (level); \ | |
235 | if (log_get_max_level() >= LOG_PRI(_level)) { \ | |
236 | _cleanup_free_ char *_p = NULL; \ | |
237 | _p = utf8_escape_invalid(rvalue); \ | |
238 | log_syntax_internal(unit, _level, config_file, config_line, 0, __FILE__, __LINE__, __func__, \ | |
239 | "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \ | |
240 | } \ | |
241 | -EINVAL; \ | |
242 | }) |