Add a method to discard the console log buffer contents.
[oweals/dinit.git] / src / dinit-log.h
1 #ifndef DINIT_LOG_H
2 #define DINIT_LOG_H
3
4 // Logging for Dinit
5
6 #include <string>
7 #include <cstdio>
8 #include <climits>
9
10 class ServiceSet;
11
12 enum class LogLevel {
13     DEBUG,
14     INFO,
15     WARN,
16     ERROR,
17     ZERO    // log absolutely nothing
18 };
19
20 extern LogLevel log_level[2];
21 void enable_console_log(bool do_enable) noexcept;
22 void init_log(ServiceSet *sset);
23 void setup_main_log(int fd);
24 bool is_log_flushed() noexcept;
25 void discard_console_log_buffer() noexcept;
26
27 void log(LogLevel lvl, const char *msg) noexcept;
28 void logMsgBegin(LogLevel lvl, const char *msg) noexcept;
29 void logMsgPart(const char *msg) noexcept;
30 void logMsgEnd(const char *msg) noexcept;
31 void logServiceStarted(const char *service_name) noexcept;
32 void logServiceFailed(const char *service_name) noexcept;
33 void logServiceStopped(const char *service_name) noexcept;
34
35 // Convenience methods which perform type conversion of the argument.
36 // There is some duplication here that could possibly be avoided, but
37 // it doesn't seem like a big deal.
38 static inline void log(LogLevel lvl, const std::string &str) noexcept
39 {
40     log(lvl, str.c_str());
41 }
42
43 static inline void logMsgBegin(LogLevel lvl, const std::string &str) noexcept
44 {
45     logMsgBegin(lvl, str.c_str());
46 }
47
48 static inline void logMsgBegin(LogLevel lvl, int a) noexcept
49 {
50     constexpr int bufsz = (CHAR_BIT * sizeof(int) - 1) / 3 + 2;
51     char nbuf[bufsz];
52     snprintf(nbuf, bufsz, "%d", a);
53     logMsgBegin(lvl, nbuf);
54 }
55
56 static inline void logMsgPart(const std::string &str) noexcept
57 {
58     logMsgPart(str.c_str());
59 }
60
61 static inline void logMsgPart(int a) noexcept
62 {
63     constexpr int bufsz = (CHAR_BIT * sizeof(int) - 1) / 3 + 2;
64     char nbuf[bufsz];
65     snprintf(nbuf, bufsz, "%d", a);
66     logMsgPart(nbuf);
67 }
68
69 static inline void logMsgEnd(const std::string &str) noexcept
70 {
71     logMsgEnd(str.c_str());
72 }
73
74 static inline void logMsgEnd(int a) noexcept
75 {
76     constexpr int bufsz = (CHAR_BIT * sizeof(int) - 1) / 3 + 2;
77     char nbuf[bufsz];
78     snprintf(nbuf, bufsz, "%d", a);
79     logMsgEnd(nbuf);
80 }
81
82 static inline void logServiceStarted(const std::string &str) noexcept
83 {
84     logServiceStarted(str.c_str());
85 }
86
87 static inline void logServiceFailed(const std::string &str) noexcept
88 {
89     logServiceFailed(str.c_str());
90 }
91
92 static inline void logServiceStopped(const std::string &str) noexcept
93 {
94     logServiceStopped(str.c_str());
95 }
96
97 // It's not intended that methods in this namespace be called directly:
98 namespace dinit_log {
99     template <typename A> static inline void logParts(A a) noexcept
100     {
101         logMsgEnd(a);
102     }
103
104     template <typename A, typename ...B> static inline void logParts(A a, B... b) noexcept
105     {
106         logMsgPart(a);
107         logParts(b...);
108     }
109 }
110
111 // Variadic 'log' method.
112 template <typename A, typename ...B> static inline void log(LogLevel lvl, A a, B ...b) noexcept
113 {
114     logMsgBegin(lvl, a);
115     dinit_log::logParts(b...);
116 }
117
118 #endif