|
cwist
Modern C web framework with libttak runtime
|
Opt-in background writer for log lines (cwist_log_async_start()). More...
#include "log_async.h"#include <cwist/sys/app/logger.h>#include <cwist/core/mem/alloc.h>#include <errno.h>#include <pthread.h>#include <stdbool.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <cwist/sys/platform.h>
Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
| #define | LOG_ASYNC_DEFAULT_BYTES ((size_t)256 * 1024) |
| #define | LOG_ASYNC_STREAMS 2 /* [0] = stdout, [1] = stderr */ |
Functions | |
| bool | cwist_log_async_start (size_t buffer_bytes) |
| Start writing log lines from a background thread. | |
| void | cwist_log_async_flush (void) |
| Block until every line queued so far has been written. No-op when inactive. | |
| void | cwist_log_async_stop (void) |
| Write out every queued line, stop the writer and go back to synchronous logging. Safe to call when async logging is not active, and more than once. | |
| bool | cwist_log_async_active (void) |
| Report whether async logging is active. | |
| bool | cwist_log_async_submit (int fd, const char *line, size_t len) |
Queue one complete line for fd (STDOUT_FILENO or STDERR_FILENO). | |
Opt-in background writer for log lines (cwist_log_async_start()).
Each output fd (stdout, stderr) has one byte ring. A caller formats its line on its own stack, copies it into the ring under one mutex and returns; a single writer thread write()s the rings to their fds. Callers never make a syscall on the request path, and lines stay whole and in submission order per fd.
A full ring makes the caller wait for space, so no line is dropped. A line larger than the ring, or any line while the writer is not running, is refused and the caller writes it synchronously as before.
fork(): the child keeps the rings' memory but not the writer thread. Its copy of the parent's queued bytes is discarded (the parent writes them) and a writer is started on the child's first line.
Definition in file log_async.c.
Definition at line 18 of file log_async.c.
Definition at line 51 of file log_async.c.
Definition at line 52 of file log_async.c.
Report whether async logging is active.
Whether the background log writer is active.
Definition at line 256 of file log_async.c.
References cap.
Block until every line queued so far has been written. No-op when inactive.
Block until every line queued so far has been written.
Definition at line 221 of file log_async.c.
References cap.
Start writing log lines from a background thread.
Start the background log writer.
| buffer_bytes | Ring size per stream (stdout and stderr); 0 selects 256 KiB. |
Definition at line 176 of file log_async.c.
References cap, cwist_alloc(), cwist_free(), LOG_ASYNC_DEFAULT_BYTES, and LOG_ASYNC_STREAMS.
Write out every queued line, stop the writer and go back to synchronous logging. Safe to call when async logging is not active, and more than once.
Write out queued lines, stop the writer and return to synchronous output.
Definition at line 229 of file log_async.c.
References cap, cwist_free(), and LOG_ASYNC_STREAMS.
Queue one complete line for fd (STDOUT_FILENO or STDERR_FILENO).
fd is not stdout/stderr, or the line is larger than the ring. The caller then writes the line synchronously itself. Definition at line 263 of file log_async.c.
References cap.