]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/log.h
journalctl: unlink without checking with access first
[thirdparty/systemd.git] / src / shared / log.h
CommitLineData
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
bbe63281 24#include <stdbool.h>
17a94911 25#include <stdarg.h>
4daf54a8
ZJS
26#include <syslog.h>
27#include <sys/signalfd.h>
0d0f0c50 28#include <errno.h>
5899f3b7
LP
29
30#include "macro.h"
20ad4cfd 31#include "sd-id128.h"
5899f3b7 32
16801e90
LP
33typedef enum LogTarget{
34 LOG_TARGET_CONSOLE,
aca83a53 35 LOG_TARGET_CONSOLE_PREFIXED,
16801e90 36 LOG_TARGET_KMSG,
5ba081b0
LP
37 LOG_TARGET_JOURNAL,
38 LOG_TARGET_JOURNAL_OR_KMSG,
843d2643
LP
39 LOG_TARGET_SYSLOG,
40 LOG_TARGET_SYSLOG_OR_KMSG,
5ba081b0 41 LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
a6903061 42 LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
9fae33d2 43 LOG_TARGET_NULL,
16801e90
LP
44 _LOG_TARGET_MAX,
45 _LOG_TARGET_INVALID = -1
46} LogTarget;
47
48void log_set_target(LogTarget target);
49void log_set_max_level(int level);
3eff4208 50void log_set_facility(int facility);
16801e90 51
34f0e866
LP
52int log_set_target_from_string(const char *e);
53int log_set_max_level_from_string(const char *e);
54
bbe63281 55void log_show_color(bool b);
b1e90ec5 56bool log_get_show_color(void) _pure_;
bbe63281 57void log_show_location(bool b);
b1e90ec5 58bool log_get_show_location(void) _pure_;
bbe63281
LP
59
60int log_show_color_from_string(const char *e);
61int log_show_location_from_string(const char *e);
62
44a6b1b6
ZJS
63LogTarget log_get_target(void) _pure_;
64int log_get_max_level(void) _pure_;
1adf1049 65
843d2643 66int log_open(void);
871e5809 67void log_close(void);
4d8a7798 68void log_forget_fds(void);
843d2643 69
16801e90 70void log_close_syslog(void);
5ba081b0 71void log_close_journal(void);
843d2643
LP
72void log_close_kmsg(void);
73void log_close_console(void);
16801e90 74
34f0e866
LP
75void log_parse_environment(void);
76
79008bdd 77int log_internal(
877d54e9 78 int level,
086891e5 79 int error,
79008bdd 80 const char *file,
877d54e9
LP
81 int line,
82 const char *func,
086891e5 83 const char *format, ...) _printf_(6,7);
5899f3b7 84
79008bdd 85int log_internalv(
877d54e9 86 int level,
086891e5 87 int error,
79008bdd 88 const char *file,
877d54e9
LP
89 int line,
90 const char *func,
91 const char *format,
086891e5 92 va_list ap) _printf_(6,0);
877d54e9 93
79008bdd 94int log_object_internal(
fdf9f9bb 95 int level,
086891e5 96 int error,
79008bdd 97 const char *file,
fdf9f9bb
ZJS
98 int line,
99 const char *func,
79008bdd 100 const char *object_field,
fdf9f9bb 101 const char *object,
086891e5 102 const char *format, ...) _printf_(8,9);
fdf9f9bb 103
79008bdd 104int log_object_internalv(
fdf9f9bb 105 int level,
086891e5 106 int error,
fdf9f9bb
ZJS
107 const char*file,
108 int line,
109 const char *func,
79008bdd 110 const char *object_field,
fdf9f9bb
ZJS
111 const char *object,
112 const char *format,
086891e5 113 va_list ap) _printf_(8,0);
fdf9f9bb 114
877d54e9
LP
115int log_struct_internal(
116 int level,
086891e5 117 int error,
877d54e9
LP
118 const char *file,
119 int line,
120 const char *func,
086891e5 121 const char *format, ...) _printf_(6,0) _sentinel_;
877d54e9
LP
122
123int log_oom_internal(
124 const char *file,
125 int line,
126 const char *func);
185986c6 127
2149e37c
LP
128/* This modifies the buffer passed! */
129int log_dump_internal(
877d54e9 130 int level,
086891e5 131 int error,
79008bdd 132 const char *file,
877d54e9
LP
133 int line,
134 const char *func,
135 char *buffer);
136
086891e5 137/* Logging for various assertions */
919ce0b7 138noreturn void log_assert_failed(
877d54e9
LP
139 const char *text,
140 const char *file,
141 int line,
142 const char *func);
143
919ce0b7 144noreturn void log_assert_failed_unreachable(
877d54e9
LP
145 const char *text,
146 const char *file,
147 int line,
148 const char *func);
80514f9c
LP
149
150void log_assert_failed_return(
151 const char *text,
152 const char *file,
153 int line,
154 const char *func);
2149e37c 155
086891e5 156/* Logging with level */
1cfa9a4c
LP
157#define log_full_errno(level, error, ...) \
158 ({ \
159 int _level = (level), _e = (error); \
160 (log_get_max_level() >= LOG_PRI(_level)) \
161 ? log_internal(_level, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \
162 : -abs(_e); \
bf371116 163 })
87ff6b1c 164
086891e5
LP
165#define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__)
166
167/* Normal logging */
4104970e
ZJS
168#define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
169#define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
170#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
171#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
172#define log_error(...) log_full(LOG_ERR, __VA_ARGS__)
173#define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__)
5899f3b7 174
086891e5
LP
175/* Logging triggered by an errno-like error */
176#define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__)
177#define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__)
178#define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__)
179#define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__)
180#define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__)
b4d23205 181#define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__)
086891e5 182
cb41ff29
ZJS
183#ifdef LOG_TRACE
184# define log_trace(...) log_debug(__VA_ARGS__)
185#else
186# define log_trace(...) do {} while(0)
187#endif
188
086891e5
LP
189/* Structured logging */
190#define log_struct(level, ...) log_struct_internal(level, 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
191#define log_struct_errno(level, error, ...) log_struct_internal(level, error, __FILE__, __LINE__, __func__, __VA_ARGS__)
0d0f0c50 192
2149e37c 193/* This modifies the buffer passed! */
086891e5
LP
194#define log_dump(level, buffer) log_dump_internal(level, 0, __FILE__, __LINE__, __func__, buffer)
195
196#define log_oom() log_oom_internal(__FILE__, __LINE__, __func__)
2149e37c 197
44a6b1b6 198bool log_on_console(void) _pure_;
81270860 199
44a6b1b6
ZJS
200const char *log_target_to_string(LogTarget target) _const_;
201LogTarget log_target_from_string(const char *s) _pure_;
706911fb 202
e2cc6eca
LP
203/* Helpers to prepare various fields for structured logging */
204#define LOG_MESSAGE(fmt, ...) "MESSAGE=" fmt, ##__VA_ARGS__
205#define LOG_MESSAGE_ID(x) "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(x)
8f16f51d 206#define LOG_ERRNO(error) "ERRNO=%i", abs(error)
4daf54a8
ZJS
207
208void log_received_signal(int level, const struct signalfd_siginfo *si);
c1dc6153
LP
209
210void log_set_upgrade_syslog_to_journal(bool b);