10 #include "dinit-log.h"
13 extern EventLoop_t eventLoop;
15 static bool log_current_line[2]; // Whether the current line is being logged (for console, main log)
16 LogLevel log_level[2] = { LogLevel::WARN, LogLevel::WARN };
18 static ServiceSet *service_set = nullptr; // Reference to service set
21 class BufferedLogStream : public PosixFdWatcher<NullMutex>
26 bool partway = false; // if we are partway throught output of a log message
27 bool discarded = false; // if we have discarded a message
28 bool release = true; // if we should inhibit output and release console
30 // A "special message" is not stored in the circular buffer; instead
31 // it is delivered from an external buffer not managed by BufferedLogger.
32 bool special = false; // currently outputting special message?
33 char *special_buf; // buffer containing special message
34 int msg_index; // index into special message
36 CPBuffer<4096> log_buffer;
41 int current_index = 0; // current/next incoming message index
51 Rearm gotEvent(EventLoop_t *loop, int fd, int flags) noexcept override;
53 // Check whether the console can be released.
54 void flushForRelease();
55 void release_console();
56 bool is_release_set() { return release; }
58 // Commit a log message
61 bool was_first = current_index == 0;
62 current_index = log_buffer.get_length();
63 if (was_first && ! release) {
64 setEnabled(&eventLoop, true);
70 log_buffer.trim_to(current_index);
75 return log_buffer.get_free();
78 void append(const char *s, size_t len)
80 log_buffer.append(s, len);
86 // (One for main log, one for console)
87 static BufferedLogStream log_stream[2];
89 constexpr static int DLOG_MAIN = 0; // main log facility
90 constexpr static int DLOG_CONS = 1; // console
92 void BufferedLogStream::release_console()
95 int flags = fcntl(1, F_GETFL, 0);
96 fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
97 service_set->pullConsoleQueue();
101 void BufferedLogStream::flushForRelease()
105 // Try to flush any messages that are currently buffered. (Console is non-blocking
106 // so it will fail gracefully).
107 if (gotEvent(&eventLoop, fd, out_events) == Rearm::DISARM) {
108 // Console has already been released at this point.
109 setEnabled(&eventLoop, false);
111 // gotEvent didn't want to disarm, so must be partway through a message; will
112 // release when it's finished.
115 Rearm BufferedLogStream::gotEvent(EventLoop_t *loop, int fd, int flags) noexcept
117 auto &log_stream = *this;
119 if ((! partway) && log_stream.special) {
120 char * start = log_stream.special_buf + log_stream.msg_index;
121 char * end = std::find(log_stream.special_buf + log_stream.msg_index, (char *)nullptr, '\n');
122 int r = write(fd, start, end - start + 1);
124 if (start + r > end) {
125 // All written: go on to next message in queue
126 log_stream.special = false;
127 log_stream.partway = false;
128 log_stream.msg_index = 0;
132 return Rearm::DISARM;
136 log_stream.msg_index += r;
141 // spurious readiness, or EAGAIN/EWOULDBLOCK/EINTR
142 // There's not much we can do for other errors anyway.
147 // Writing from the regular circular buffer
149 // TODO issue special message if we have discarded a log message
151 if (log_stream.current_index == 0) {
153 return Rearm::DISARM;
156 char *ptr = log_stream.log_buffer.get_ptr(0);
157 int len = log_stream.log_buffer.get_contiguous_length(ptr);
158 char *creptr = ptr + len; // contiguous region end
159 char *eptr = std::find(ptr, creptr, '\n');
161 bool will_complete = false; // will complete this message?
162 if (eptr != creptr) {
163 eptr++; // include '\n'
164 will_complete = true;
169 int r = write(fd, ptr, len);
172 bool complete = (r == len) && will_complete;
173 log_stream.log_buffer.consume(len);
174 log_stream.partway = ! complete;
176 log_stream.current_index -= len;
177 if (log_stream.current_index == 0 || release) {
178 // No more messages buffered / stop logging to console:
180 return Rearm::DISARM;
186 // We've written something by the time we get here. We could fall through to below, but
187 // let's give other events a chance to be processed by returning now.
191 // Initialise the logging subsystem
192 // Potentially throws std::bad_alloc or std::system_error
193 void init_log(ServiceSet *sset)
196 log_stream[DLOG_CONS].registerWith(&eventLoop, STDOUT_FILENO, out_events); // TODO register in disabled state
197 enable_console_log(true);
200 // Set up the main log to output to the given file descriptor.
201 // Potentially throws std::bad_alloc or std::system_error
202 void setup_main_log(int fd)
204 log_stream[DLOG_MAIN].init(STDERR_FILENO);
205 log_stream[DLOG_MAIN].registerWith(&eventLoop, STDERR_FILENO, out_events);
208 bool is_log_flushed() noexcept
210 return log_stream[DLOG_CONS].current_index == 0;
213 // Enable or disable console logging. If disabled, console logging will be disabled on the
214 // completion of output of the current message (if any), at which point the first service record
215 // queued in the service set will acquire the console.
216 void enable_console_log(bool enable) noexcept
218 bool log_to_console = ! log_stream[DLOG_CONS].is_release_set();
219 if (enable && ! log_to_console) {
220 // Console is fd 1 - stdout
221 // Set non-blocking IO:
222 int flags = fcntl(1, F_GETFL, 0);
223 fcntl(1, F_SETFL, flags | O_NONBLOCK);
225 log_stream[DLOG_CONS].init(STDOUT_FILENO);
226 log_stream[DLOG_CONS].setEnabled(&eventLoop, true);
228 else if (! enable && log_to_console) {
229 log_stream[DLOG_CONS].flushForRelease();
234 // Variadic method to calculate the sum of string lengths:
235 static int sum_length(const char *arg) noexcept
237 return std::strlen(arg);
240 template <typename U, typename ... T> static int sum_length(U first, T ... args) noexcept
242 return sum_length(first) + sum_length(args...);
245 // Variadic method to append strings to a buffer:
246 static void append(BufferedLogStream &buf, const char *s)
248 buf.append(s, std::strlen(s));
251 template <typename U, typename ... T> static void append(BufferedLogStream &buf, U u, T ... t)
257 // Variadic method to log a sequence of strings as a single message:
258 template <typename ... T> static void push_to_log(T ... args) noexcept
260 int amount = sum_length(args...);
261 for (int i = 0; i < 2; i++) {
262 if (! log_current_line[i]) continue;
263 if (log_stream[i].get_free() >= amount) {
264 append(log_stream[i], args...);
265 log_stream[i].commit_msg();
268 // TODO mark a discarded message
273 // Variadic method to potentially log a sequence of strings as a single message with the given log level:
274 template <typename ... T> static void do_log(LogLevel lvl, T ... args) noexcept
276 log_current_line[DLOG_CONS] = lvl >= log_level[DLOG_CONS];
277 log_current_line[DLOG_MAIN] = lvl >= log_level[DLOG_MAIN];
278 push_to_log(args...);
281 template <typename ... T> static void do_log_cons(T ... args) noexcept
283 log_current_line[DLOG_CONS] = true;
284 log_current_line[DLOG_MAIN] = false;
285 push_to_log(args...);
288 template <typename ... T> static void do_log_main(T ... args) noexcept
290 log_current_line[DLOG_CONS] = false;
291 log_current_line[DLOG_MAIN] = true;
292 push_to_log(args...);
295 // Log a message. A newline will be appended.
296 void log(LogLevel lvl, const char *msg) noexcept
298 do_log(lvl, "dinit: ", msg, "\n");
301 // Log part of a message. A series of calls to do_log_part must be followed by a call to do_log_commit.
302 template <typename T> static void do_log_part(int idx, T arg) noexcept
304 if (log_current_line[idx]) {
305 int amount = sum_length(arg);
306 if (log_stream[idx].get_free() >= amount) {
307 append(log_stream[idx], arg);
310 log_stream[idx].rollback_msg();
311 log_current_line[idx] = false;
312 // TODO mark discarded message
317 // Log part of a message. A series of calls to do_log_part must be followed by a call to do_log_commit.
318 template <typename T> static void do_log_part(T arg) noexcept
320 do_log_part(DLOG_CONS, arg);
323 // Commit a message that was issued as a series of parts (via do_log_part).
324 static void do_log_commit(int idx) noexcept
326 if (log_current_line[idx]) {
327 log_stream[idx].commit_msg();
331 // Log a multi-part message beginning
332 void logMsgBegin(LogLevel lvl, const char *msg) noexcept
334 log_current_line[DLOG_CONS] = lvl >= log_level[DLOG_CONS];
335 log_current_line[DLOG_MAIN] = lvl >= log_level[DLOG_MAIN];
336 for (int i = 0; i < 2; i++) {
337 do_log_part(i, "dinit: ");
342 // Continue a multi-part log message
343 void logMsgPart(const char *msg) noexcept
345 do_log_part(DLOG_CONS, msg);
346 do_log_part(DLOG_MAIN, msg);
349 // Complete a multi-part log message
350 void logMsgEnd(const char *msg) noexcept
352 for (int i = 0; i < 2; i++) {
354 do_log_part(i, "\n");
359 void logServiceStarted(const char *service_name) noexcept
361 do_log_cons("[ OK ] ", service_name, "\n");
362 do_log_main("dinit: service ", service_name, " started.\n");
365 void logServiceFailed(const char *service_name) noexcept
367 do_log_cons("[FAILED] ", service_name, "\n");
368 do_log_main("dinit: service ", service_name, " failed to start.\n");
371 void logServiceStopped(const char *service_name) noexcept
373 do_log_cons("[STOPPD] ", service_name, "\n");
374 do_log_main("dinit: service ", service_name, " stopped.\n");