]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.h
Merge pull request #1654 from poettering/util-lib
[thirdparty/systemd.git] / src / basic / log.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
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
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
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
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <sys/signalfd.h>
29 #include <syslog.h>
30
31 #include "sd-id128.h"
32
33 #include "macro.h"
34
35 typedef enum LogTarget{
36 LOG_TARGET_CONSOLE,
37 LOG_TARGET_CONSOLE_PREFIXED,
38 LOG_TARGET_KMSG,
39 LOG_TARGET_JOURNAL,
40 LOG_TARGET_JOURNAL_OR_KMSG,
41 LOG_TARGET_SYSLOG,
42 LOG_TARGET_SYSLOG_OR_KMSG,
43 LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
44 LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
45 LOG_TARGET_NULL,
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);
52 void log_set_facility(int facility);
53
54 int log_set_target_from_string(const char *e);
55 int log_set_max_level_from_string(const char *e);
56
57 void log_show_color(bool b);
58 bool log_get_show_color(void) _pure_;
59 void log_show_location(bool b);
60 bool log_get_show_location(void) _pure_;
61
62 int log_show_color_from_string(const char *e);
63 int log_show_location_from_string(const char *e);
64
65 LogTarget log_get_target(void) _pure_;
66 int log_get_max_level(void) _pure_;
67
68 int log_open(void);
69 void log_close(void);
70 void log_forget_fds(void);
71
72 void log_close_syslog(void);
73 void log_close_journal(void);
74 void log_close_kmsg(void);
75 void log_close_console(void);
76
77 void log_parse_environment(void);
78
79 int log_internal(
80 int level,
81 int error,
82 const char *file,
83 int line,
84 const char *func,
85 const char *format, ...) _printf_(6,7);
86
87 int log_internalv(
88 int level,
89 int error,
90 const char *file,
91 int line,
92 const char *func,
93 const char *format,
94 va_list ap) _printf_(6,0);
95
96 int log_object_internal(
97 int level,
98 int error,
99 const char *file,
100 int line,
101 const char *func,
102 const char *object_field,
103 const char *object,
104 const char *format, ...) _printf_(8,9);
105
106 int log_object_internalv(
107 int level,
108 int error,
109 const char*file,
110 int line,
111 const char *func,
112 const char *object_field,
113 const char *object,
114 const char *format,
115 va_list ap) _printf_(8,0);
116
117 int log_struct_internal(
118 int level,
119 int error,
120 const char *file,
121 int line,
122 const char *func,
123 const char *format, ...) _printf_(6,0) _sentinel_;
124
125 int log_oom_internal(
126 const char *file,
127 int line,
128 const char *func);
129
130 /* This modifies the buffer passed! */
131 int log_dump_internal(
132 int level,
133 int error,
134 const char *file,
135 int line,
136 const char *func,
137 char *buffer);
138
139 /* Logging for various assertions */
140 noreturn void log_assert_failed(
141 const char *text,
142 const char *file,
143 int line,
144 const char *func);
145
146 noreturn void log_assert_failed_unreachable(
147 const char *text,
148 const char *file,
149 int line,
150 const char *func);
151
152 void log_assert_failed_return(
153 const char *text,
154 const char *file,
155 int line,
156 const char *func);
157
158 /* Logging with level */
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); \
165 })
166
167 #define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__)
168
169 /* Normal logging */
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__)
176
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__)
183 #define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
184
185 #ifdef LOG_TRACE
186 # define log_trace(...) log_debug(__VA_ARGS__)
187 #else
188 # define log_trace(...) do {} while(0)
189 #endif
190
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__)
194
195 /* This modifies the buffer passed! */
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__)
199
200 bool log_on_console(void) _pure_;
201
202 const char *log_target_to_string(LogTarget target) _const_;
203 LogTarget log_target_from_string(const char *s) _pure_;
204
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)
208
209 void log_received_signal(int level, const struct signalfd_siginfo *si);
210
211 void log_set_upgrade_syslog_to_journal(bool b);
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 })
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 })