cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
logger.c File Reference

logger.c interface. More...

#include <cwist/sys/app/logger.h>
#include <cwist/core/mem/alloc.h>
#include "log_async.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Include dependency graph for logger.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 

Functions

cwist_logger * cwist_logger_create (const char *name)
 Creates a logger instance.
 
void cwist_logger_destroy (cwist_logger *log)
 Destroys a logger instance and frees its name.
 
void cwist_logger_set_level (cwist_logger *log, cwist_log_level_t level)
 Sets the minimum log level that will be emitted.
 
void cwist_logger_set_colors (cwist_logger *log, bool enabled)
 Enables or disables ANSI color output in log lines.
 
void cwist_logger_log (cwist_logger *log, cwist_log_level_t level, const char *fmt,...)
 Logs a formatted message at the given level.
 
void cwist_logger_debug (cwist_logger *log, const char *fmt,...)
 Logs a formatted message at DEBUG level on stdout.
 
void cwist_logger_info (cwist_logger *log, const char *fmt,...)
 Logs a formatted message at INFO level on stdout.
 
void cwist_logger_warn (cwist_logger *log, const char *fmt,...)
 Logs a formatted message at WARN level on stderr.
 
void cwist_logger_error (cwist_logger *log, const char *fmt,...)
 Logs a formatted message at ERROR level on stderr.
 

Detailed Description

logger.c interface.

Definition in file logger.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 4 of file logger.c.

Function Documentation

◆ cwist_logger_create()

cwist_logger * cwist_logger_create ( const char *  name)

Creates a logger instance.

Create a logger.

Parameters
nameLogger name copied into the instance; NULL selects "cwist".
Returns
Newly allocated logger on success, NULL on allocation failure.
Note
Ownership of the returned logger transfers to the caller; release it with cwist_logger_destroy().

Definition at line 32 of file logger.c.

References cap, cwist_alloc(), cwist_free(), CWIST_LOG_INFO, cwist_strdup(), cwist_logger::level, cwist_logger::name, and cwist_logger::use_colors.

Referenced by cwist_app_create().

◆ cwist_logger_debug()

void cwist_logger_debug ( cwist_logger *  log,
const char *  fmt,
  ... 
)

Logs a formatted message at DEBUG level on stdout.

Write a debug message.

Parameters
logLogger to log through; NULL is safely ignored.
fmtprintf-style format string.

Definition at line 154 of file logger.c.

References cap, and CWIST_LOG_DEBUG.

◆ cwist_logger_destroy()

void cwist_logger_destroy ( cwist_logger *  log)

Destroys a logger instance and frees its name.

Destroy a logger.

Parameters
logLogger to free; NULL is safely ignored.

Definition at line 48 of file logger.c.

References cwist_free(), and cwist_logger::name.

Referenced by cwist_app_destroy().

◆ cwist_logger_error()

void cwist_logger_error ( cwist_logger *  log,
const char *  fmt,
  ... 
)

Logs a formatted message at ERROR level on stderr.

Write an error message.

Parameters
logLogger to log through; NULL is safely ignored.
fmtprintf-style format string.

Definition at line 187 of file logger.c.

References cap, and CWIST_LOG_ERROR.

◆ cwist_logger_info()

void cwist_logger_info ( cwist_logger *  log,
const char *  fmt,
  ... 
)

Logs a formatted message at INFO level on stdout.

Write an info message.

Parameters
logLogger to log through; NULL is safely ignored.
fmtprintf-style format string.

Definition at line 165 of file logger.c.

References cap, and CWIST_LOG_INFO.

◆ cwist_logger_log()

void cwist_logger_log ( cwist_logger *  log,
cwist_log_level_t  level,
const char *  fmt,
  ... 
)

Logs a formatted message at the given level.

Write a log message.

Parameters
logLogger to log through; NULL is safely ignored.
levelLevel of the message; messages below the logger's level are suppressed.
fmtprintf-style format string.
Note
The message is written to stdout for levels below WARN and to stderr otherwise, with a timestamp, level tag, and logger name prefix, followed by a newline. The shared FILE streams are not locked across the prefix and the message, so lines from concurrent threads may interleave. In the sync fallback each line is still one fwrite(3) call (line-at-a-time write granularity), but stdio block-buffers the bytes, so lines are delivered in batches rather than per line; a TTY destination stays line-buffered. Call cwist_log_async_start() for immediate per-line delivery.

Definition at line 143 of file logger.c.

References cap.

◆ cwist_logger_set_colors()

void cwist_logger_set_colors ( cwist_logger *  log,
bool  enabled 
)

Enables or disables ANSI color output in log lines.

Enable or disable colored output.

Parameters
logLogger to configure; NULL is safely ignored.
enabledtrue to colorize level prefixes, false for plain output.

Definition at line 66 of file logger.c.

References cwist_logger::use_colors.

◆ cwist_logger_set_level()

void cwist_logger_set_level ( cwist_logger *  log,
cwist_log_level_t  level 
)

Sets the minimum log level that will be emitted.

Set the minimum log level.

Parameters
logLogger to configure; NULL is safely ignored.
levelNew minimum level; messages below it are suppressed.

Definition at line 58 of file logger.c.

References cwist_logger::level.

◆ cwist_logger_warn()

void cwist_logger_warn ( cwist_logger *  log,
const char *  fmt,
  ... 
)

Logs a formatted message at WARN level on stderr.

Write a warning message.

Parameters
logLogger to log through; NULL is safely ignored.
fmtprintf-style format string.

Definition at line 176 of file logger.c.

References cap, and CWIST_LOG_WARN.