clogger · Section 3
clog_close
NAME
clog_close - flush, close, and free a logger
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/clogger.h> void clog_close(clog logger);
DESCRIPTION
Releases every resource that the logger owns:
- For a logger on a file, the call closes the file descriptor when
this is the last handle that shares the file. While another
handle (the root or a derived logger) is open, the fd stays open
and the call only lowers the refcount of the file. A handle that
a second clog_create_from_file_path_mp(3) of the same rotating file
returns shares the file the same way.
- With async logging on (see clog_create_from_fd_mp(3)) and this the
last handle, the call drains everything that is already queued and
joins the writer thread before it releases the output (and, for a
logger on a file, closes the file descriptor). That drain is the
last delivery attempt of the logger and nothing is retried after
it, so whatever it cannot deliver is replaced by a "log record
truncated: undelivered batch dropped (records=<n> bytes=<m>): the
logger closed" record.
The one exception is a logger whose drain at exit(3), or at a
CLOG_FATAL call, gave up because its writer thread did not answer
in time. Closing such a logger, from a destructor for example,
neither waits for that thread nor joins it, and the logger is left
in place for the end of the process.
- When the logger compresses its rotated files (see
clog_create_from_file_path_mp(3)) and this is the last handle, the call
waits until every compression that a rotation queued has finished
and then stops the compression thread. So by the time the call
returns, every rotated file of the logger is compressed, or left
whole and uncompressed after a failed compression.
- It frees every key=value field of the logger.
- It frees the internal write buffer.
- It frees the state of this handle, and frees the shared backing
store only when it releases the last handle.
Do not use the handle after this call.
PARAMETERS
logger Logger handle to close.
NOTES
Do not call clog_close() while another thread makes any call with the same handle: A clog_<level> macro, clog_set_level, clog_set_field and the functions next to it, clog_derive, or another clog_close(). You must make sure that every other use of a handle has stopped before you close it. This rule does not cover a sibling handle that keeps logging while one handle closes (the root against a derived logger, or two derived loggers); that close is safe, because the sibling is a different handle and it is open. For a logger on an fd, from clog_create_from_fd_mp, this call does NOT close the file descriptor; you have to do that yourself. Asserts when logger is CLOG_NULL, stale or already closed. The destructor of the library releases the table of loggers at the end of the process, so a close from a destructor that runs after it finds every handle stale and reports the same error.