10 #include "dinit-log.h"
13 extern EventLoop_t eventLoop;
15 LogLevel log_level = LogLevel::WARN;
16 LogLevel cons_log_level = LogLevel::WARN;
18 static bool log_current_line; // Whether the current line is being logged
20 static ServiceSet *service_set = nullptr; // Reference to service set
23 class BufferedLogStream : public PosixFdWatcher<NullMutex>
28 bool partway = false; // if we are partway throught output of a log message
29 bool discarded = false; // if we have discarded a message
30 bool release = true; // if we should inhibit output and release console
32 // A "special message" is not stored in the circular buffer; instead
33 // it is delivered from an external buffer not managed by BufferedLogger.
34 bool special = false; // currently outputting special message?
35 char *special_buf; // buffer containing special message
36 int msg_index; // index into special message
38 CPBuffer<4096> log_buffer;
43 int current_index = 0; // current/next incoming message index
53 Rearm gotEvent(EventLoop_t *loop, int fd, int flags) noexcept override;
55 // Check whether the console can be released.
56 void flushForRelease();
57 void release_console();
58 bool is_release_set() { return release; }
60 // Commit a log message
63 bool was_first = current_index == 0;
64 current_index = log_buffer.get_length();
65 if (was_first && ! release) {
66 setEnabled(&eventLoop, true);
72 log_buffer.trim_to(current_index);
77 return log_buffer.get_free();
80 void append(const char *s, size_t len)
82 log_buffer.append(s, len);
88 // (One for main log, one for console)
89 static BufferedLogStream log_stream[2];
91 constexpr static int DLOG_MAIN = 0; // main log facility
92 constexpr static int DLOG_CONS = 1; // console
95 void BufferedLogStream::release_console()
98 int flags = fcntl(1, F_GETFL, 0);
99 fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
100 service_set->pullConsoleQueue();
104 void BufferedLogStream::flushForRelease()
108 // Try to flush any messages that are currently buffered. (Console is non-blocking
109 // so it will fail gracefully).
110 if (gotEvent(&eventLoop, fd, out_events) == Rearm::DISARM) {
111 // Console has already been released at this point.
112 setEnabled(&eventLoop, false);
114 // gotEvent didn't want to disarm, so must be partway through a message; will
115 // release when it's finished.
118 Rearm BufferedLogStream::gotEvent(EventLoop_t *loop, int fd, int flags) noexcept
120 // TODO correct for the case that this is *not* the console log stream.
122 auto &log_stream = *this;
124 if ((! partway) && log_stream.special) {
125 char * start = log_stream.special_buf + log_stream.msg_index;
126 char * end = std::find(log_stream.special_buf + log_stream.msg_index, (char *)nullptr, '\n');
127 int r = write(fd, start, end - start + 1);
129 if (start + r > end) {
130 // All written: go on to next message in queue
131 log_stream.special = false;
132 log_stream.partway = false;
133 log_stream.msg_index = 0;
137 return Rearm::DISARM;
141 log_stream.msg_index += r;
146 // spurious readiness - EAGAIN or EWOULDBLOCK?
153 // Writing from the regular circular buffer
155 // TODO issue special message if we have discarded a log message
157 if (log_stream.current_index == 0) {
159 return Rearm::DISARM;
162 char *ptr = log_stream.log_buffer.get_ptr(0);
163 int len = log_stream.log_buffer.get_contiguous_length(ptr);
164 char *creptr = ptr + len; // contiguous region end
165 char *eptr = std::find(ptr, creptr, '\n');
167 bool will_complete = false; // will complete this message?
168 if (eptr != creptr) {
169 eptr++; // include '\n'
170 will_complete = true;
175 int r = write(fd, ptr, len);
178 bool complete = (r == len) && will_complete;
179 log_stream.log_buffer.consume(len);
180 log_stream.partway = ! complete;
182 log_stream.current_index -= len;
183 if (log_stream.current_index == 0 || release) {
184 // No more messages buffered / stop logging to console:
186 return Rearm::DISARM;
192 // EAGAIN / EWOULDBLOCK?
198 // We've written something by the time we get here. We could fall through to below, but
199 // let's give other events a chance to be processed by returning now.
203 void init_log(ServiceSet *sset) noexcept
206 log_stream[DLOG_CONS].registerWith(&eventLoop, STDOUT_FILENO, out_events); // TODO register in disabled state
207 enable_console_log(true);
210 bool is_log_flushed() noexcept
212 return log_stream[DLOG_CONS].current_index == 0;
215 // Enable or disable console logging. If disabled, console logging will be disabled on the
216 // completion of output of the current message (if any), at which point the first service record
217 // queued in the service set will acquire the console.
218 void enable_console_log(bool enable) noexcept
220 bool log_to_console = ! log_stream[DLOG_CONS].is_release_set();
221 if (enable && ! log_to_console) {
222 // Console is fd 1 - stdout
223 // Set non-blocking IO:
224 int flags = fcntl(1, F_GETFL, 0);
225 fcntl(1, F_SETFL, flags | O_NONBLOCK);
227 log_stream[DLOG_CONS].init(STDOUT_FILENO);
228 log_stream[DLOG_CONS].setEnabled(&eventLoop, true);
230 else if (! enable && log_to_console) {
231 log_stream[DLOG_CONS].flushForRelease();
236 // Variadic method to calculate the sum of string lengths:
237 static int sum_length(const char *arg) noexcept
239 return std::strlen(arg);
242 template <typename U, typename ... T> static int sum_length(U first, T ... args) noexcept
244 return sum_length(first) + sum_length(args...);
247 // Variadic method to append strings to a buffer:
248 static void append(BufferedLogStream &buf, const char *s)
250 buf.append(s, std::strlen(s));
253 template <typename U, typename ... T> static void append(BufferedLogStream &buf, U u, T ... t)
259 // Variadic method to log a sequence of strings as a single message:
260 template <typename ... T> static void do_log_cons(T ... args) noexcept
262 int amount = sum_length(args...);
263 if (log_stream[DLOG_CONS].get_free() >= amount) {
264 append(log_stream[DLOG_CONS], args...);
265 log_stream[DLOG_CONS].commit_msg();
268 // TODO mark a discarded message
272 // Variadic method to potentially log a sequence of strings as a single message with the given log level:
273 template <typename ... T> static void do_log(LogLevel lvl, T ... args) noexcept
275 if (lvl >= cons_log_level) {
276 do_log_cons(args...);
280 // Log a message. A newline will be appended.
281 void log(LogLevel lvl, const char *msg) noexcept
283 do_log(lvl, "dinit: ", msg, "\n");
286 // Log part of a message. A series of calls to do_log_part must be followed by a call to do_log_commit.
287 template <typename T> static void do_log_part(T arg) noexcept
289 int amount = sum_length(arg);
290 if (log_stream[DLOG_CONS].get_free() >= amount) {
291 append(log_stream[DLOG_CONS], arg);
294 log_stream[DLOG_CONS].rollback_msg();
295 log_current_line = false;
296 // TODO mark discarded message
300 // Commit a message that was issued as a series of parts (via do_log_part).
301 static void do_log_commit() noexcept
303 if (log_current_line) {
304 log_stream[DLOG_CONS].commit_msg();
308 // Log a multi-part message beginning
309 void logMsgBegin(LogLevel lvl, const char *msg) noexcept
312 log_current_line = lvl >= log_level;
313 if (log_current_line) {
314 do_log_part("dinit: ");
319 // Continue a multi-part log message
320 void logMsgPart(const char *msg) noexcept
323 if (log_current_line) {
328 // Complete a multi-part log message
329 void logMsgEnd(const char *msg) noexcept
332 if (log_current_line) {
339 void logServiceStarted(const char *service_name) noexcept
341 do_log_cons("[ OK ] ", service_name, "\n");
344 void logServiceFailed(const char *service_name) noexcept
346 do_log_cons("[FAILED] ", service_name, "\n");
349 void logServiceStopped(const char *service_name) noexcept
351 do_log_cons("[STOPPD] ", service_name, "\n");