//
// Note that most actual functions for logging messages are found in the header, dinit-log.h.
//
-// We have two separate log "streams": one for the console/stdout, one for the syslog facility. Both have a
-// circular buffer. Log messages are appended to the circular buffer (for the syslog stream, the messages
-// are prepended with a syslog priority indicator). Both streams start out inactive (release = true in
-// buffered_log_stream), which means they will buffer messages but not write them.
+// We have two separate log "streams": one for the console/stdout, one for the syslog facility (or log
+// file). Both have a circular buffer. Log messages are appended to the circular buffer (for a syslog
+// stream, the messages are prepended with a syslog priority indicator). Both streams start out inactive
+// (release = true in buffered_log_stream), which means they will buffer messages but not write them.
//
// The console log stream needs to be able to release the console, if a service is waiting to acquire it.
// This is accomplished by calling flush_for_release() which then completes the output of the current
extern eventloop_t event_loop;
static bool log_current_line[2]; // Whether the current line is being logged (for console, main log)
-loglevel_t log_level[2] = { loglevel_t::WARN, loglevel_t::INFO };
+loglevel_t log_level[2] = { loglevel_t::INFO, loglevel_t::WARN };
static bool log_format_syslog[2] = { false, true };
static service_set *services = nullptr; // Reference to service set
+dasynq::time_val release_time; // time the log was released
+
using rearm = dasynq::rearm;
namespace {
int flags = fcntl(1, F_GETFL, 0);
fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
services->pull_console_queue();
+ if (release) {
+ // release still set, we didn't immediately get the console back; record the
+ // time at which we released:
+ event_loop.get_time(release_time, clock_type::MONOTONIC);
+ }
}
}
{
if ((! partway) && (! special) && discarded) {
special_buf = "dinit: *** log message discarded due to full buffer ***\n";
+ special = true;
+ discarded = false;
msg_index = 0;
}
if ((! partway) && special) {
const char * start = special_buf + msg_index;
- const char * end = std::find(special_buf + msg_index, (const char *)nullptr, '\n');
+ const char * end = start;
+ while (*end != '\n') end++;
int r = write(fd, start, end - start + 1);
if (r >= 0) {
if (start + r > end) {
// All written: go on to next message in queue
special = false;
- partway = false;
+ discarded = false;
msg_index = 0;
if (release) {
// Initialise the logging subsystem
// Potentially throws std::bad_alloc or std::system_error
-void init_log(service_set *sset)
+void init_log(service_set *sset, bool syslog_format)
{
services = sset;
log_stream[DLOG_CONS].add_watch(event_loop, STDOUT_FILENO, dasynq::OUT_EVENTS, false);
enable_console_log(true);
+
+ // The main (non-console) log won't be active yet, but we set the format here so that we
+ // buffer messages in the correct format:
+ log_format_syslog[DLOG_MAIN] = syslog_format;
}
// Set up the main log to output to the given file descriptor.
// Potentially throws std::bad_alloc or std::system_error
-void setup_main_log(int fd, bool syslog_format)
+void setup_main_log(int fd)
{
log_stream[DLOG_MAIN].init(fd);
log_stream[DLOG_MAIN].add_watch(event_loop, fd, dasynq::OUT_EVENTS);
- log_format_syslog[DLOG_MAIN] = syslog_format;
}
bool is_log_flushed() noexcept
void discard_console_log_buffer() noexcept
{
- log_stream[DLOG_CONS].discard();
+ // Only discard if more than a second has passed since we released the console.
+ dasynq::time_val current_time;
+ event_loop.get_time(current_time, clock_type::MONOTONIC);
+ if (current_time - release_time >= dasynq::time_val(1, 0)) {
+ log_stream[DLOG_CONS].discard();
+ }
}
// Variadic method to calculate the sum of string lengths: