skip to content
==> include/mesh/log.h <==
1/* @ajax/mesh-log: INFO/ERROR lines with a UTC wall-clock timestamp on stderr
2 * through a log thread (mesh-host log.rs): emitting never blocks and never
3 * fails.  A line goes into a bounded queue (MESH_LOG_QUEUE lines); when it is
4 * full -- stderr is a pipe nobody reads, a slow terminal -- the line is
5 * dropped from stderr and counted (S_LOG_DROPPED).  The log thread writes
6 * with write(2) and ignores its errors (EPIPE counts as dropped too).  The
7 * last MESH_LOG_KEEP lines are kept in memory (meshd's `log` request). */
8#ifndef MESH_LOG_H
9#define MESH_LOG_H
10
11#include <stddef.h>
12#include <stdint.h>
13
14#define MESH_LOG_KEEP 1000
15#define MESH_LOG_QUEUE 4096
16
17/* "HH:MM:SS.mmm" (UTC) into buf (at least 13 bytes). */
18void mesh_log_stamp(char buf[16]);
19/* Queues one line for stderr (dropped and counted if the queue is full) and keeps it. Takes a copy. */
20void mesh_log_emit(const char *line);
21void mesh_info(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
22void mesh_error(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
23/* Lines not written to stderr. */
24uint64_t mesh_log_dropped(void);
25/* Waits (at most limit_ms) for the queued lines to be written. */
26void mesh_log_flush(unsigned limit_ms);
27/* Exits once the queued lines are written (at most 500 ms). */
28_Noreturn void mesh_log_exit(int code);
29/* The last n lines, oldest first: *lines is a malloc'd array of malloc'd strings. */
30size_t mesh_log_recent(size_t n, char ***lines);
31/* Where lines go (default fd 2); for tests. */
32void mesh_log_set_fd(int fd);
33
34#endif