chiark / gitweb /
9918381d39df19dd5cceb63d887579613fedbbb5
[elogind.git] / src / shared / 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 <stdbool.h>
25 #include <stdarg.h>
26 #include <syslog.h>
27 #include <sys/signalfd.h>
28 #include <errno.h>
29
30 #include "macro.h"
31 #include "sd-id128.h"
32
33 typedef enum LogTarget{
34         LOG_TARGET_CONSOLE,
35         LOG_TARGET_KMSG,
36         LOG_TARGET_JOURNAL,
37         LOG_TARGET_JOURNAL_OR_KMSG,
38         LOG_TARGET_SYSLOG,
39         LOG_TARGET_SYSLOG_OR_KMSG,
40         LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
41         LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
42         LOG_TARGET_NULL,
43         _LOG_TARGET_MAX,
44         _LOG_TARGET_INVALID = -1
45 }  LogTarget;
46
47 void log_set_target(LogTarget target);
48 void log_set_max_level(int level);
49 void log_set_facility(int facility);
50
51 int log_set_target_from_string(const char *e);
52 int log_set_max_level_from_string(const char *e);
53
54 void log_show_color(bool b);
55 bool log_get_show_color(void) _pure_;
56 void log_show_location(bool b);
57 bool log_get_show_location(void) _pure_;
58
59 int log_show_color_from_string(const char *e);
60 int log_show_location_from_string(const char *e);
61
62 LogTarget log_get_target(void) _pure_;
63 int log_get_max_level(void) _pure_;
64
65 int log_open(void);
66 void log_close(void);
67 void log_forget_fds(void);
68
69 void log_close_syslog(void);
70 void log_close_journal(void);
71 void log_close_kmsg(void);
72 void log_close_console(void);
73
74 void log_parse_environment(void);
75
76 int log_meta(
77                 int level,
78                 const char*file,
79                 int line,
80                 const char *func,
81                 const char *format, ...) _printf_(5,6);
82
83 int log_metav(
84                 int level,
85                 const char*file,
86                 int line,
87                 const char *func,
88                 const char *format,
89                 va_list ap) _printf_(5,0);
90
91 int log_meta_object(
92                 int level,
93                 const char*file,
94                 int line,
95                 const char *func,
96                 const char *object_name,
97                 const char *object,
98                 const char *format, ...) _printf_(7,8);
99
100 int log_metav_object(
101                 int level,
102                 const char*file,
103                 int line,
104                 const char *func,
105                 const char *object_name,
106                 const char *object,
107                 const char *format,
108                 va_list ap) _printf_(7,0);
109
110 int log_struct_internal(
111                 int level,
112                 const char *file,
113                 int line,
114                 const char *func,
115                 const char *format, ...) _printf_(5,0) _sentinel_;
116
117 int log_oom_internal(
118                 const char *file,
119                 int line,
120                 const char *func);
121
122 /* This modifies the buffer passed! */
123 int log_dump_internal(
124                 int level,
125                 const char*file,
126                 int line,
127                 const char *func,
128                 char *buffer);
129
130 noreturn void log_assert_failed(
131                 const char *text,
132                 const char *file,
133                 int line,
134                 const char *func);
135
136 noreturn void log_assert_failed_unreachable(
137                 const char *text,
138                 const char *file,
139                 int line,
140                 const char *func);
141
142 void log_assert_failed_return(
143                 const char *text,
144                 const char *file,
145                 int line,
146                 const char *func);
147
148 #define log_full(level, ...) \
149 do { \
150         if (log_get_max_level() >= (level)) \
151                 log_meta((level), __FILE__, __LINE__, __func__, __VA_ARGS__); \
152 } while (0)
153
154 #define log_debug(...)   log_full(LOG_DEBUG,   __VA_ARGS__)
155 #define log_info(...)    log_full(LOG_INFO,    __VA_ARGS__)
156 #define log_notice(...)  log_full(LOG_NOTICE,  __VA_ARGS__)
157 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
158 #define log_error(...)   log_full(LOG_ERR,     __VA_ARGS__)
159
160 #define log_struct(level, ...) log_struct_internal(level, __FILE__, __LINE__, __func__, __VA_ARGS__)
161
162 #define log_oom() log_oom_internal(__FILE__, __LINE__, __func__)
163
164 /* This modifies the buffer passed! */
165 #define log_dump(level, buffer) log_dump_internal(level, __FILE__, __LINE__, __func__, buffer)
166
167 bool log_on_console(void) _pure_;
168
169 const char *log_target_to_string(LogTarget target) _const_;
170 LogTarget log_target_from_string(const char *s) _pure_;
171
172 #define MESSAGE_ID(x) "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(x)
173
174 void log_received_signal(int level, const struct signalfd_siginfo *si);
175
176 void log_set_upgrade_syslog_to_journal(bool b);