5 #include <sys/syslog.h>
11 #include "dinit-log.h"
14 // Dinit logging subsystem.
16 // Note that most actual functions for logging messages are found in the header, dinit-log.h.
18 // We have two separate log "streams": one for the console/stdout, one for the syslog facility (or log
19 // file). Both have a circular buffer. Log messages are appended to the circular buffer (for a syslog
20 // stream, the messages are prepended with a syslog priority indicator). Both streams start out inactive
21 // (release = true in buffered_log_stream), which means they will buffer messages but not write them.
23 // The console log stream needs to be able to release the console, if a service is waiting to acquire it.
24 // This is accomplished by calling flush_for_release() which then completes the output of the current
25 // message (if any) and then assigns the console to a waiting service.
27 extern eventloop_t event_loop;
28 extern bool external_log_open;
30 static bool log_current_line[2]; // Whether the current line is being logged (for console, main log)
31 static bool log_format_syslog[2] = { false, true };
33 static service_set *services = nullptr; // Reference to service set
35 loglevel_t log_level[2] = { loglevel_t::INFO, loglevel_t::WARN };
36 bool console_service_status = true; // show service status messages to console?
38 dasynq::time_val release_time; // time the log was released
40 using rearm = dasynq::rearm;
43 class buffered_log_stream : public eventloop_t::fd_watcher_impl<buffered_log_stream>
48 bool partway = false; // if we are partway throught output of a log message
49 bool discarded = false; // if we have discarded a message
50 bool release = true; // if we should inhibit output and release console when possible
52 // A "special message" is not stored in the circular buffer; instead
53 // it is delivered from an external buffer not managed by BufferedLogger.
54 bool special = false; // currently outputting special message?
55 const char *special_buf; // buffer containing special message
56 int msg_index; // index into special message
58 cpbuffer<4096> log_buffer;
63 int current_index = 0; // current/next incoming message index
73 rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept;
75 // Check whether the console can be released.
76 void flush_for_release();
77 bool is_release_set() { return release; }
79 // Commit a log message
82 bool was_first = current_index == 0;
83 current_index = log_buffer.get_length();
84 if (was_first && ! release) {
85 set_enabled(event_loop, true);
91 log_buffer.trim_to(current_index);
96 return log_buffer.get_free();
99 void append(const char *s, size_t len)
101 log_buffer.append(s, len);
104 // Discard buffer; call only when the stream isn't active.
108 log_buffer.trim_to(0);
111 // Mark that a message was discarded due to full buffer
112 void mark_discarded()
117 void watch_removed() noexcept override;
120 void release_console();
124 // (One for main log, one for console)
125 buffered_log_stream log_stream[2];
127 void buffered_log_stream::release_console()
130 int flags = fcntl(1, F_GETFL, 0);
131 fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
132 services->pull_console_queue();
134 // release still set, we didn't immediately get the console back; record the
135 // time at which we released:
136 event_loop.get_time(release_time, clock_type::MONOTONIC);
141 void buffered_log_stream::flush_for_release()
145 // Try to flush any messages that are currently buffered. (Console is non-blocking
146 // so it will fail gracefully).
147 rearm rearm_val = fd_event(event_loop, fd, dasynq::OUT_EVENTS);
148 if (rearm_val == rearm::DISARM) {
149 // Console has already been released at this point.
150 set_enabled(event_loop, false);
152 if (rearm_val == rearm::REMOVE) {
153 deregister(event_loop);
155 // fd_event didn't want to disarm, so must be partway through a message; will
156 // release when it's finished.
159 rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexcept
161 if ((! partway) && (! special) && discarded) {
162 special_buf = "dinit: *** log message discarded due to full buffer ***\n";
168 if ((! partway) && special) {
169 const char * start = special_buf + msg_index;
170 const char * end = start;
171 while (*end != '\n') end++;
172 int r = bp_sys::write(fd, start, end - start + 1);
174 if (start + r > end) {
175 // All written: go on to next message in queue
182 return rearm::DISARM;
190 else if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
191 return rearm::REMOVE;
196 // Writing from the regular circular buffer
198 if (current_index == 0) {
200 return rearm::DISARM;
203 // We try to find a complete line (terminated by '\n') in the buffer, and write it
204 // out. Since it may span the circular buffer end, it may consist of two distinct spans,
205 // and so we use writev to write them atomically.
207 struct iovec logiov[2];
209 char *ptr = log_buffer.get_ptr(0);
210 int len = log_buffer.get_contiguous_length(ptr);
211 char *creptr = ptr + len; // contiguous region end
212 char *eptr = std::find(ptr, creptr, '\n');
214 bool will_complete = false; // will complete this message?
215 if (eptr != creptr) {
216 eptr++; // include '\n'
217 will_complete = true;
222 logiov[0].iov_base = ptr;
223 logiov[0].iov_len = len;
224 int iovs_to_write = 1;
226 // Do we need the second span?
227 if (! will_complete && len != log_buffer.get_length()) {
228 ptr = log_buffer.get_buf_base();
229 creptr = ptr + log_buffer.get_length() - len;
230 eptr = std::find(ptr, creptr, '\n');
231 if (eptr != creptr) {
232 eptr++; // include '\n'
233 // It should not ever be the case that we do not now have a complete message
234 will_complete = true;
236 logiov[1].iov_base = ptr;
237 logiov[1].iov_len = eptr - ptr;
238 len += logiov[1].iov_len;
242 ssize_t r = bp_sys::writev(fd, logiov, iovs_to_write);
245 bool complete = (r == len) && will_complete;
246 log_buffer.consume(len);
247 partway = ! complete;
249 current_index -= len;
250 if (current_index == 0 || release) {
251 // No more messages buffered / stop logging to console:
253 return rearm::DISARM;
257 else if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
258 return rearm::REMOVE;
262 // We've written something by the time we get here. We could fall through to below, but
263 // let's give other events a chance to be processed by returning now.
267 void buffered_log_stream::watch_removed() noexcept
269 if (fd > STDERR_FILENO) {
273 // Here we rely on there only being two logs, console and "main"; we can check if we are the
274 // main log via identity:
275 if (&log_stream[DLOG_MAIN] == this) {
276 external_log_open = false;
282 // Initialise the logging subsystem
283 // Potentially throws std::bad_alloc or std::system_error
284 void init_log(service_set *sset, bool syslog_format)
287 log_stream[DLOG_CONS].add_watch(event_loop, STDOUT_FILENO, dasynq::OUT_EVENTS, false);
288 enable_console_log(true);
290 // The main (non-console) log won't be active yet, but we set the format here so that we
291 // buffer messages in the correct format:
292 log_format_syslog[DLOG_MAIN] = syslog_format;
295 // Close logging subsystem
298 if (log_stream[DLOG_CONS].fd != -1) log_stream[DLOG_CONS].deregister(event_loop);
299 if (log_stream[DLOG_MAIN].fd != -1) log_stream[DLOG_MAIN].deregister(event_loop);
302 // Set up the main log to output to the given file descriptor.
303 // Potentially throws std::bad_alloc or std::system_error
304 void setup_main_log(int fd)
306 log_stream[DLOG_MAIN].init(fd);
307 log_stream[DLOG_MAIN].add_watch(event_loop, fd, dasynq::OUT_EVENTS);
310 bool is_log_flushed() noexcept
312 return log_stream[DLOG_CONS].current_index == 0 &&
313 (log_stream[DLOG_MAIN].fd == -1 || log_stream[DLOG_MAIN].current_index == 0);
316 // Enable or disable console logging. If disabled, console logging will be disabled on the
317 // completion of output of the current message (if any), at which point the first service record
318 // queued in the service set will acquire the console.
319 void enable_console_log(bool enable) noexcept
321 bool log_to_console = ! log_stream[DLOG_CONS].is_release_set();
322 if (enable && ! log_to_console) {
323 // Set non-blocking IO:
324 int flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
325 fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);
327 log_stream[DLOG_CONS].init(STDOUT_FILENO);
328 log_stream[DLOG_CONS].set_enabled(event_loop, true);
330 else if (! enable && log_to_console) {
331 log_stream[DLOG_CONS].flush_for_release();
335 void discard_console_log_buffer() noexcept
337 // Only discard if more than a second has passed since we released the console.
338 dasynq::time_val current_time;
339 event_loop.get_time(current_time, clock_type::MONOTONIC);
340 if (current_time - release_time >= dasynq::time_val(1, 0)) {
341 log_stream[DLOG_CONS].discard();
345 // Variadic method to calculate the sum of string lengths:
346 static int sum_length(const char *arg) noexcept
348 return std::strlen(arg);
351 template <typename ... T> static int sum_length(const char * first, T ... args) noexcept
353 return sum_length(first) + sum_length(args...);
356 // Variadic method to append strings to a buffer:
357 static void append(buffered_log_stream &buf, const char *s)
359 buf.append(s, std::strlen(s));
362 template <typename ... T> static void append(buffered_log_stream &buf, const char *u, T ... t)
368 static int log_level_to_syslog_level(loglevel_t l)
371 case loglevel_t::DEBUG:
373 case loglevel_t::INFO:
375 case loglevel_t::WARN:
377 case loglevel_t::ERROR:
385 // Variadic method to log a sequence of strings as a single message to a particular facility:
386 template <typename ... T> static void push_to_log(int idx, T ... args) noexcept
388 if (! log_current_line[idx]) return;
389 int amount = sum_length(args...);
390 if (log_stream[idx].get_free() >= amount) {
391 append(log_stream[idx], args...);
392 log_stream[idx].commit_msg();
395 log_stream[idx].mark_discarded();
399 // Variadic method to potentially log a sequence of strings as a single message with the given log level:
400 template <typename ... T> static void do_log(loglevel_t lvl, bool to_cons, T ... args) noexcept
402 log_current_line[DLOG_CONS] = (lvl >= log_level[DLOG_CONS]) && to_cons;
403 log_current_line[DLOG_MAIN] = (lvl >= log_level[DLOG_MAIN]);
404 push_to_log(DLOG_CONS, args...);
406 if (log_current_line[DLOG_MAIN]) {
407 if (log_format_syslog[DLOG_MAIN]) {
409 snprintf(svcbuf, 10, "<%d>", LOG_DAEMON | log_level_to_syslog_level(lvl));
410 push_to_log(DLOG_MAIN, svcbuf, args...);
413 push_to_log(DLOG_MAIN, args...);
418 template <typename ... T> static void do_log_cons(T ... args) noexcept
420 if (console_service_status) {
421 log_current_line[DLOG_CONS] = true;
422 log_current_line[DLOG_MAIN] = false;
423 push_to_log(DLOG_CONS, args...);
427 // Log to the main facility at NOTICE level
428 template <typename ... T> static void do_log_main(T ... args) noexcept
430 log_current_line[DLOG_CONS] = false;
431 log_current_line[DLOG_MAIN] = true;
433 if (log_format_syslog[DLOG_MAIN]) {
435 snprintf(svcbuf, 10, "<%d>", LOG_DAEMON | LOG_NOTICE);
436 push_to_log(DLOG_MAIN, svcbuf, args...);
439 push_to_log(DLOG_MAIN, args...);
443 // Log a message. A newline will be appended.
444 void log(loglevel_t lvl, const char *msg) noexcept
446 do_log(lvl, true, "dinit: ", msg, "\n");
449 void log(loglevel_t lvl, bool to_cons, const char *msg) noexcept
451 do_log(lvl, to_cons, "dinit: ", msg, "\n");
454 // Log part of a message. A series of calls to do_log_part must be followed by a call to do_log_commit.
455 static void do_log_part(int idx, const char *arg) noexcept
457 if (log_current_line[idx]) {
458 int amount = sum_length(arg);
459 if (log_stream[idx].get_free() >= amount) {
460 append(log_stream[idx], arg);
463 log_stream[idx].rollback_msg();
464 log_current_line[idx] = false;
465 log_stream[idx].mark_discarded();
470 // Commit a message that was issued as a series of parts (via do_log_part).
471 static void do_log_commit(int idx) noexcept
473 if (log_current_line[idx]) {
474 log_stream[idx].commit_msg();
478 // Log a multi-part message beginning
479 void log_msg_begin(loglevel_t lvl, const char *msg) noexcept
481 log_current_line[DLOG_CONS] = lvl >= log_level[DLOG_CONS];
482 log_current_line[DLOG_MAIN] = lvl >= log_level[DLOG_MAIN];
484 // Prepend the syslog priority level string ("<N>") for the main log:
485 if (log_current_line[DLOG_MAIN]) {
486 if (log_format_syslog[DLOG_MAIN]) {
488 snprintf(svcbuf, 10, "<%d>", LOG_DAEMON | log_level_to_syslog_level(lvl));
489 do_log_part(DLOG_MAIN, svcbuf);
493 for (int i = 0; i < 2; i++) {
494 do_log_part(i, "dinit: ");
499 // Continue a multi-part log message
500 void log_msg_part(const char *msg) noexcept
502 do_log_part(DLOG_CONS, msg);
503 do_log_part(DLOG_MAIN, msg);
506 // Complete a multi-part log message
507 void log_msg_end(const char *msg) noexcept
509 for (int i = 0; i < 2; i++) {
511 do_log_part(i, "\n");
516 void log_service_started(const char *service_name) noexcept
518 do_log_cons("[ OK ] ", service_name, "\n");
519 do_log_main("dinit: service ", service_name, " started.\n");
522 void log_service_failed(const char *service_name) noexcept
524 do_log_cons("[FAILED] ", service_name, "\n");
525 do_log_main("dinit: service ", service_name, " failed to start.\n");
528 void log_service_stopped(const char *service_name) noexcept
530 do_log_cons("[STOPPD] ", service_name, "\n");
531 do_log_main("dinit: service ", service_name, " stopped.\n");