+#define DEFAULT_LOG_LEVEL L_NOTICE
+
+static int log_level = DEFAULT_LOG_LEVEL;
+static const int log_class[] = {
+ [L_CRIT] = LOG_CRIT,
+ [L_WARNING] = LOG_WARNING,
+ [L_NOTICE] = LOG_NOTICE,
+ [L_INFO] = LOG_INFO,
+ [L_DEBUG] = LOG_DEBUG
+};
+
+#ifdef DUMMY_MODE
+#define use_syslog false
+#else
+static bool use_syslog = true;
+#endif
+
+
+static void
+netifd_delete_process(struct netifd_process *proc)
+{
+ list_del(&proc->list);
+ ustream_free(&proc->log.stream);
+ close(proc->log.fd.fd);
+}
+
+void
+netifd_log_message(int priority, const char *format, ...)
+{
+ va_list vl;
+
+ if (priority > log_level)
+ return;
+
+ va_start(vl, format);
+ if (use_syslog)
+ vsyslog(log_class[priority], format, vl);
+ else
+ vfprintf(stderr, format, vl);
+ va_end(vl);
+}
+
+static void
+netifd_process_log_read_cb(struct ustream *s, int bytes)
+{
+ struct netifd_process *proc;
+ const char *log_prefix;
+ char *data;
+ int len = 0;
+
+ proc = container_of(s, struct netifd_process, log.stream);
+ log_prefix = proc->log_prefix;
+ if (!log_prefix)
+ log_prefix = "process";
+
+ do {
+ char *newline;
+
+ data = ustream_get_read_buf(s, &len);
+ if (!len)
+ break;
+
+ newline = strchr(data, '\n');
+
+ if (proc->log_overflow) {
+ if (newline) {
+ len = newline + 1 - data;
+ proc->log_overflow = false;
+ }
+ } else if (newline) {
+ *newline = 0;
+ len = newline + 1 - data;
+ netifd_log_message(L_NOTICE, "%s (%d): %s\n",
+ log_prefix, proc->uloop.pid, data);
+ } else if (len == s->r.buffer_len) {
+ netifd_log_message(L_NOTICE, "%s (%d): %s [...]\n",
+ log_prefix, proc->uloop.pid, data);
+ proc->log_overflow = true;
+ } else
+ break;
+
+ ustream_consume(s, len);
+ } while (1);
+}
+