CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Console Logging

Data Structures

struct  LogConsoleConfig
 

Typedefs

typedef struct LogConsoleConfig LogConsoleConfig
 
typedef struct LogConsoleData LogConsoleData
 Opaque handle for console logging state.
 

Enumerations

enum  LOGCON_COLOR_MODE { LOGCON_ColorAuto = 0 , LOGCON_ColorOn , LOGCON_ColorOff }
 Color decision for a console destination. More...
 

Functions

LogDest * logconsoleRegister (int maxlevel, strref chanfilter, ConStream *out, ConStream *err, const LogConsoleConfig *config, LogSerializer *ser)
 
LogConsoleDatalogconsoleCreate (ConStream *out, ConStream *err, const LogConsoleConfig *config, LogSerializer *ser)
 
void logconsoleMsgFunc (const LogRecord *rec, void *userdata)
 
void logconsoleBatchFunc (uint32 batchid, void *userdata)
 
void logconsoleCloseFunc (void *userdata)
 

Detailed Description

Console-based logging destination that writes formatted, level-colored log lines to conOut()/conErr() (see Console).

The console is a transport and takes a serializer like any other (see Log Serializers), so a line looks the same whether it ends up in a file or on a terminal. What the console destination adds is routing between stdout/stderr by severity and a per-level ConStyle, both automatically downgraded (or dropped entirely) by the destination stream's own capabilities – a line sent to a redirected-to-a-file stdout gets no escape codes even though the identical line to a real terminal would be colored.

Basic Usage:

.stderrLevel = LOG_Count, // everything to stderr, stdout stays clean
};
LogTextConfig tcfg = {
};
// ...or, on a console where the timestamp is just noise, LOG_OmitDate leaves it out
// entirely and the line starts with whatever comes next:
// LogTextConfig tcfg = { .flags = LOG_OmitDate | LOG_ShortLevel | LOG_IncludeChannel };
// real conOut()/conErr()
LogDest *dest = logconsoleRegister(LOG_Info, NULL, NULL, NULL, &cfg,
// Later, unregister to release
LogDest * logconsoleRegister(int maxlevel, strref chanfilter, ConStream *out, ConStream *err, const LogConsoleConfig *config, LogSerializer *ser)
bool logUnregisterDest(LogDest *dhandle)
struct LogDest LogDest
Opaque handle to a registered log destination.
Definition log.h:187
@ LOG_Info
Informational messages.
Definition log.h:103
LogSerializer * logTextSerializer(LogTextConfig *config)
@ LOG_IncludeChannel
Include channel path in output.
@ LOG_ShortLevel
Use single-character level abbreviations.
@ LOG_DateISOCompact
Compact ISO: "2026-01-02 15:04:05".
int dateFormat
Date format from LOG_DATE_FORMATS; ignored under LOG_OmitDate.

Typedef Documentation

◆ LogConsoleConfig

Configuration for console-based logging

Controls stdout/stderr routing and per-level coloring. Output formatting belongs to the serializer the console is created with, not here.

Enumeration Type Documentation

◆ LOGCON_COLOR_MODE

Color decision for a console destination.

Enumerator
LOGCON_ColorAuto 

Style only when the destination stream reports color support.

LOGCON_ColorOn 

Always apply per-level style, letting ConStyle's own downgrade ladder decide what the stream can actually render

LOGCON_ColorOff 

Never apply style; plain text only.

Definition at line 49 of file logconsole.h.

Function Documentation

◆ logconsoleBatchFunc()

void logconsoleBatchFunc ( uint32  batchid,
void *  userdata 
)

Batch completion callback for console destinations

Flushes both conOut() and conErr() so batch messages reach the terminal together.

Parameters
batchidCompleted batch identifier
userdataLogConsoleData pointer from logconsoleCreate()

◆ logconsoleCloseFunc()

void logconsoleCloseFunc ( void *  userdata)

Cleanup callback for console destinations

Releases the console logging handle. Never closes conOut()/conErr() themselves – they are process singletons.

Parameters
userdataLogConsoleData pointer from logconsoleCreate()

◆ logconsoleCreate()

LogConsoleData * logconsoleCreate ( ConStream *  out,
ConStream *  err,
const LogConsoleConfig config,
LogSerializer ser 
)

Create a console logging destination

Only needed to register the console by hand with logRegisterDest()logconsoleRegister() does this and the registration together. The returned handle belongs to exactly one destination: it is freed by logconsoleCloseFunc() and cannot be registered twice.

Parameters
outStream for messages less severe than stderrLevel; NULL uses conOut(). Not acquired or owned – must outlive the returned handle.
errStream for messages at or more severe than stderrLevel; NULL uses conErr(). Not acquired or owned – must outlive the returned handle.
configConsole configuration (copied, caller retains ownership)
serSerializer to render records with; ownership transfers. NULL gets a default text serializer.
Returns
Console logging handle
LogConsoleConfig cfg = { .stderrLevel = LOG_Count };
LogConsoleData *lcd = logconsoleCreate(NULL, NULL, &cfg, NULL);
void logconsoleCloseFunc(void *userdata)
struct LogConsoleData LogConsoleData
Opaque handle for console logging state.
Definition logconsole.h:74
void logconsoleMsgFunc(const LogRecord *rec, void *userdata)
void logconsoleBatchFunc(uint32 batchid, void *userdata)
LogConsoleData * logconsoleCreate(ConStream *out, ConStream *err, const LogConsoleConfig *config, LogSerializer *ser)
LogDest * logRegisterDest(int maxlevel, strref chanfilter, LogDestMsg msgfunc, LogDestBatchDone batchfunc, LogDestClose closefunc, void *userdata)

◆ logconsoleMsgFunc()

void logconsoleMsgFunc ( const LogRecord rec,
void *  userdata 
)

Log message callback for console destinations

Renders a log record and writes it to conOut() or conErr(), styled per level according to the destination's configuration and the destination stream's capabilities.

Parameters
recLog record to write
userdataLogConsoleData pointer from logconsoleCreate()

◆ logconsoleRegister()

LogDest * logconsoleRegister ( int  maxlevel,
strref  chanfilter,
ConStream *  out,
ConStream *  err,
const LogConsoleConfig config,
LogSerializer ser 
)

Register a console logging destination

Builds the console destination and registers it with the logging system in one step. It is closed and released when logUnregisterDest() retires the returned handle.

Parameters
maxlevelMaximum log level to write to the console
chanfilterChannel path pattern, or NULL for every unrestricted channel
outStream for messages less severe than stderrLevel; NULL uses conOut(). Not acquired or owned – must outlive the destination.
errStream for messages at or more severe than stderrLevel; NULL uses conErr(). Not acquired or owned – must outlive the destination.
configConsole configuration (copied, caller retains ownership)
serSerializer to render records with; ownership transfers, including if this call fails. NULL gets a default text serializer.
Returns
Destination handle for later unregistration, or NULL on failure
LogConsoleConfig cfg = { .stderrLevel = LOG_Count };
LogDest *dest = logconsoleRegister(LOG_Info, NULL, NULL, NULL, &cfg, NULL);
// or, for a test that asserts exact output:
ConStream *mem = conCreateMem(&(ConCaps){ 0 });
LogDest *tdest = logconsoleRegister(LOG_Info, NULL, mem, mem, &cfg, NULL);
ConStream * conCreateMem(const ConCaps *caps)
struct ConStream ConStream
Definition console.h:20