chiark / gitweb /
journald: log provenience of signals
[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 void log_show_location(bool b);
56
57 int log_show_color_from_string(const char *e);
58 int log_show_location_from_string(const char *e);
59
60 LogTarget log_get_target(void) _pure_;
61 int log_get_max_level(void) _pure_;
62
63 int log_open(void);
64 void log_close(void);
65 void log_forget_fds(void);
66
67 void log_close_syslog(void);
68 void log_close_journal(void);
69 void log_close_kmsg(void);
70 void log_close_console(void);
71
72 void log_parse_environment(void);
73
74 int log_meta(
75                 int level,
76                 const char*file,
77                 int line,
78                 const char *func,
79                 const char *format, ...) _printf_(5,6);
80
81 int log_metav(
82                 int level,
83                 const char*file,
84                 int line,
85                 const char *func,
86                 const char *format,
87                 va_list ap) _printf_(5,0);
88
89 int log_meta_object(
90                 int level,
91                 const char*file,
92                 int line,
93                 const char *func,
94                 const char *object_name,
95                 const char *object,
96                 const char *format, ...) _printf_(7,8);
97
98 int log_metav_object(
99                 int level,
100                 const char*file,
101                 int line,
102                 const char *func,
103                 const char *object_name,
104                 const char *object,
105                 const char *format,
106                 va_list ap) _printf_(7,0);
107
108 int log_struct_internal(
109                 int level,
110                 const char *file,
111                 int line,
112                 const char *func,
113                 const char *format, ...) _printf_(5,0) _sentinel_;
114
115 int log_oom_internal(
116                 const char *file,
117                 int line,
118                 const char *func);
119
120 /* This modifies the buffer passed! */
121 int log_dump_internal(
122                 int level,
123                 const char*file,
124                 int line,
125                 const char *func,
126                 char *buffer);
127
128 noreturn void log_assert_failed(
129                 const char *text,
130                 const char *file,
131                 int line,
132                 const char *func);
133
134 noreturn void log_assert_failed_unreachable(
135                 const char *text,
136                 const char *file,
137                 int line,
138                 const char *func);
139
140 void log_assert_failed_return(
141                 const char *text,
142                 const char *file,
143                 int line,
144                 const char *func);
145
146 #define log_full(level, ...) \
147 do { \
148         if (log_get_max_level() >= (level)) \
149                 log_meta((level), __FILE__, __LINE__, __func__, __VA_ARGS__); \
150 } while (0)
151
152 #define log_debug(...)   log_full(LOG_DEBUG,   __VA_ARGS__)
153 #define log_info(...)    log_full(LOG_INFO,    __VA_ARGS__)
154 #define log_notice(...)  log_full(LOG_NOTICE,  __VA_ARGS__)
155 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
156 #define log_error(...)   log_full(LOG_ERR,     __VA_ARGS__)
157
158 #define log_struct(level, ...) log_struct_internal(level, __FILE__, __LINE__, __func__, __VA_ARGS__)
159
160 #define log_oom() log_oom_internal(__FILE__, __LINE__, __func__)
161
162 /* This modifies the buffer passed! */
163 #define log_dump(level, buffer) log_dump_internal(level, __FILE__, __LINE__, __func__, buffer)
164
165 bool log_on_console(void) _pure_;
166
167 const char *log_target_to_string(LogTarget target) _const_;
168 LogTarget log_target_from_string(const char *s) _pure_;
169
170 #define MESSAGE_ID(x) "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(x)
171
172 void log_received_signal(int level, const struct signalfd_siginfo *si);