1 /*** 2 This file is part of eudev, forked from systemd. 3 4 Copyright 2010 Lennart Poettering 5 6 systemd is free software; you can redistribute it and/or modify it 7 under the terms of the GNU Lesser General Public License as published by 8 the Free Software Foundation; either version 2.1 of the License, or 9 (at your option) any later version. 10 11 systemd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with systemd; If not, see <http://www.gnu.org/licenses/>. 18 ***/ 19 20 #pragma once 21 22 #include <stdbool.h> 23 #include <stdarg.h> 24 #include <syslog.h> 25 #include <errno.h> 26 27 #include "macro.h" 28 29 typedef enum LogTarget{ 30 LOG_TARGET_CONSOLE, 31 LOG_TARGET_CONSOLE_PREFIXED, 32 LOG_TARGET_KMSG, 33 LOG_TARGET_JOURNAL, 34 LOG_TARGET_JOURNAL_OR_KMSG, 35 LOG_TARGET_SYSLOG, 36 LOG_TARGET_SYSLOG_OR_KMSG, 37 LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */ 38 LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */ 39 LOG_TARGET_NULL, 40 _LOG_TARGET_MAX, 41 _LOG_TARGET_INVALID = -1 42 } LogTarget; 43 44 void log_set_target(LogTarget target); 45 void log_set_max_level(int level); 46 int log_get_max_level(void) _pure_; 47 48 int log_open(void); 49 void log_close(void); 50 51 void log_close_syslog(void); 52 void log_close_journal(void); 53 void log_close_kmsg(void); 54 void log_close_console(void); 55 56 int log_internal( 57 int level, 58 int error, 59 const char *file, 60 int line, 61 const char *func, 62 const char *format, ...) _printf_(6,7); 63 64 int log_internalv( 65 int level, 66 int error, 67 const char *file, 68 int line, 69 const char *func, 70 const char *format, 71 va_list ap) _printf_(6,0); 72 73 int log_oom_internal( 74 const char *file, 75 int line, 76 const char *func); 77 78 /* Logging for various assertions */ 79 noreturn void log_assert_failed( 80 const char *text, 81 const char *file, 82 int line, 83 const char *func); 84 85 noreturn void log_assert_failed_unreachable( 86 const char *text, 87 const char *file, 88 int line, 89 const char *func); 90 91 92 /* Logging with level */ 93 #define log_full_errno(level, error, ...) \ 94 ({ \ 95 int _l = (level), _e = (error); \ 96 (log_get_max_level() >= LOG_PRI(_l)) \ 97 ? log_internal(_l, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \ 98 : -abs(_e); \ 99 }) 100 101 #define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__) 102 103 /* Normal logging */ 104 #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) 105 #define log_info(...) log_full(LOG_INFO, __VA_ARGS__) 106 #define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__) 107 #define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__) 108 #define log_error(...) log_full(LOG_ERR, __VA_ARGS__) 109 #define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__) 110 111 /* Logging triggered by an errno-like error */ 112 #define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__) 113 #define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__) 114 #define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__) 115 #define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__) 116 #define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__) 117 #define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, error, __VA_ARGS__) 118 119 #ifdef LOG_TRACE 120 # define log_trace(...) log_debug(__VA_ARGS__) 121 #else 122 # define log_trace(...) do {} while(0) 123 #endif 124 125 /* This modifies the buffer passed! */ 126 127 #define log_oom() log_oom_internal(__FILE__, __LINE__, __func__) 128 129 const char *log_target_to_string(LogTarget target) _const_; 130 LogTarget log_target_from_string(const char *s) _pure_; 131