6 Module LOG
gullradriel edited this page 2026-10-29 20:54:40 +01:00

LOGGING: logging to console, to file, to syslog, to event log

Structures

Name
struct TS_LOG
ThreadSafe LOGging structure.

Functions

Name
int write_safe_log(TS_LOG * log, char * pat, ... )
write to a thread-safe logging file
void set_log_level(const int log_level)
Set global log level.
int set_log_file_fd(FILE * fd)
set an already open stream (stdout, stderr, a caller's FILE) as standard log output, never closed by the library
int set_log_file(char * file)
set a file as standard log output
char * open_sysjrnl(const char * identity)
Open a syslog / set name for event log.
int open_safe_logging(TS_LOG ** log, char * pathname, char * opt)
Open a thread-safe logging file.
int get_log_level(void )
Return the global log level.
FILE * get_log_file(void )
get the log file name if any
void close_sysjrnl(void )
close syslog / free name for event log
int close_safe_logging(TS_LOG * log)
close a thread-safe logging file
void _n_log(int level, const char * file, const char * func, int line, const char * format, ... )
Full log function.

Defines

Name
n_log(LEVEL, ...)
Logging function wrapper to get line and func.
N_PRINTF_FORMAT(fmt, args)
declare a function printf-like; nothing on a compiler without attributes
LOG_WARNING
warning conditions
LOG_SYSJRNL
to sysjrnl
LOG_STDERR
internal, default LOG_TYPE
LOG_NULL
no log output
LOG_NOTICE
normal but significant condition
LOG_INFO
informational
LOG_FILE
internal, logging to file
LOG_ERR
error conditions
LOG_EMERG
system is unusable
LOG_DEBUG
debug-level messages
LOG_CRIT
critical conditions
LOG_ALERT
action must be taken immediately

Functions Documentation

function write_safe_log

int write_safe_log(
    TS_LOG * log,
    char * pat,
    ... 
)

write to a thread-safe logging file

Parameters:

  • log A TS_LOG handler
  • pat Pattern for writting (i.e "%d %d %s")

Return: TRUE on success, FALSE on error

function set_log_level

void set_log_level(
    const int log_level
)

Set global log level.

Parameters:

  • log_level Log level value. Supported: NOLOG,LOG_NOTICE/INFO/ERR/DEBUG,LOG_FILE/STDERR/SYSJRNL

Set the global log level value ( static int LOG_LEVEL ).

Possible values: NOLOG , NOTICE , VERBOSE , ERROR , DEBUG

Set global log level.

function set_log_file_fd

int set_log_file_fd(
    FILE * fd
)

set an already open stream (stdout, stderr, a caller's FILE) as standard log output, never closed by the library

Parameters:

  • fd The open stream to log to

Return: TRUE or FALSE

Set the logging to an already open stream instead of stderr.

set an already open stream (stdout, stderr, a caller's FILE) as standard log output, never closed by the library

The stream stays owned by the caller: the library never closes it, not here and not when a later set_log_file() / set_log_file_fd() replaces it. A log file previously opened by set_log_file() is closed first. Typical use is set_log_file_fd(stdout), e.g. under Emscripten where stderr shows up as console errors in the browser.

function set_log_file

int set_log_file(
    char * file
)

set a file as standard log output

Parameters:

  • file The filename where to log

Return: TRUE or FALSE

Set the logging to a file instead of stderr.

set a file as standard log output

function open_sysjrnl

char * open_sysjrnl(
    const char * identity
)

Open a syslog / set name for event log.

Parameters:

  • identity Tag for syslog or NULL to use argv[0]

Return: NULL or identity if success

Open connection to syslog or create internals for event log.

Open a syslog / set name for event log.

function open_safe_logging

int open_safe_logging(
    TS_LOG ** log,
    char * pathname,
    char * opt
)

Open a thread-safe logging file.

Parameters:

  • log A TS_LOG handler
  • pathname The file path (if any) and name
  • opt Options for opening (please never forget to use "w")

Return: TRUE on success , FALSE on error , -1000 if already open

function get_log_level

int get_log_level(
    void 
)

Return the global log level.

Return: static int LOG_LEVEL

Get the global log level value.

Possible values: NOLOG , NOTICE , VERBOSE , ERROR , DEBUG

Return the global log level.

function get_log_file

FILE * get_log_file(
    void 
)

get the log file name if any

Return: a valid FILE handle or NULL

return the current log_file

get the log file name if any

function close_sysjrnl

void close_sysjrnl(
    void 
)

close syslog / free name for event log

Close syslog connection or clean internals for event log.

close syslog / free name for event log

function close_safe_logging

int close_safe_logging(
    TS_LOG * log
)

close a thread-safe logging file

Parameters:

Return: TRUE on success, FALSE on error

function _n_log

void _n_log(
    int level,
    const char * file,
    const char * func,
    int line,
    const char * format,
    ... 
)

Full log function.

Parameters:

  • level Logging level
  • file File containing the emitted log
  • func Function emitting the log
  • line Line of the log
  • format Format and string of the log, printf style

Logging function.

Must be wrapped in a MACRO to get the correct file/func/line information

Full log function.

log( level , const char *format , ... ) is a macro around _log

Macros Documentation

define n_log

#define n_log(
    __LEVEL__,
    ...
)
do {                                                              \
    _n_log(__LEVEL__, __FILE__, __func__, __LINE__, __VA_ARGS__); \
} while (0)

Logging function wrapper to get line and func.

define N_PRINTF_FORMAT

#define N_PRINTF_FORMAT(
    fmt,
    args
)

declare a function printf-like; nothing on a compiler without attributes

define LOG_WARNING

#define LOG_WARNING 4

warning conditions

define LOG_SYSJRNL

#define LOG_SYSJRNL 100

to sysjrnl

define LOG_STDERR

#define LOG_STDERR -4

internal, default LOG_TYPE

define LOG_NULL

#define LOG_NULL -1

no log output

define LOG_NOTICE

#define LOG_NOTICE 5

normal but significant condition

define LOG_INFO

#define LOG_INFO 6

informational

define LOG_FILE

#define LOG_FILE -3

internal, logging to file

define LOG_ERR

#define LOG_ERR 3

error conditions

define LOG_EMERG

#define LOG_EMERG 0

system is unusable

define LOG_DEBUG

#define LOG_DEBUG 7

debug-level messages

define LOG_CRIT

#define LOG_CRIT 2

critical conditions

define LOG_ALERT

#define LOG_ALERT 1

action must be taken immediately