ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
log/format.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <ascii-chat/log/log.h>
14#include <stdbool.h>
15#include <stddef.h>
16#include <stdint.h>
17
18/* ============================================================================
19 * Format Specifier Types
20 * ============================================================================
21 */
22
28typedef enum {
29 LOG_FORMAT_LITERAL, /* Plain text (no % prefix) */
30 LOG_FORMAT_TIME, /* %time(fmt) - custom time format */
31 LOG_FORMAT_LEVEL, /* %level - log level as string */
32 LOG_FORMAT_LEVEL_ALIGNED, /* %level_aligned - log level padded */
33 LOG_FORMAT_FILE, /* %file - file path */
34 LOG_FORMAT_FILE_RELATIVE, /* %file_relative - file path relative to project
35 root */
36 LOG_FORMAT_LINE, /* %line - line number */
37 LOG_FORMAT_FUNC, /* %func - function name */
38 LOG_FORMAT_TID, /* %tid - thread ID */
39 LOG_FORMAT_TNAME, /* %tname - thread name (from debug registry) */
40 LOG_FORMAT_MESSAGE, /* %message - log message */
41 LOG_FORMAT_COLORLOG_LEVEL, /* %colorlog_level_string_to_color - color code */
42 LOG_FORMAT_COLOR, /* %color(LEVEL, content) - colorize content using LEVEL's
43 color */
44 LOG_FORMAT_COLORED_MESSAGE, /* %colored_message - message with things like
45 filenames and 0x numbers colored */
46 LOG_FORMAT_MICROSECONDS, /* %ms - microseconds component of current time
47 (000000-999999) */
48 LOG_FORMAT_NANOSECONDS, /* %ns - nanoseconds component of current time
49 (000000000-999999999) */
50 LOG_FORMAT_STRFTIME_CODE, /* %H, %M, %S, %A, %B, etc. - strftime format codes
51 */
52 LOG_FORMAT_NEWLINE, /* Platform-aware newline (\n) */
54
55/* ============================================================================
56 * Parsed Format Specifier
57 * ============================================================================
58 */
59
67typedef struct {
68 log_template_type_t type; /* Type of specifier */
69 char *literal; /* For LOG_FORMAT_LITERAL, the text; for LOG_FORMAT_TIME, the
70 format string */
71 size_t literal_len; /* Length of literal text */
73
74/* ============================================================================
75 * Compiled Log Format
76 * ============================================================================
77 */
78
82typedef struct {
83 log_format_spec_t *specs; /* Array of parsed specifiers */
84 size_t spec_count; /* Number of specifiers */
85 char *original; /* Original format string (for debugging) */
86 bool console_only; /* If true, apply only to console (not file) */
88
89/* ============================================================================
90 * Public API
91 * ============================================================================
92 */
93
94#ifdef __cplusplus
95extern "C" {
96#endif
97
115log_template_t *log_template_parse(const char *format_str, bool console_only);
116
123
156int log_template_apply(const log_template_t *format, char *buf, size_t buf_size,
157 log_level_t level, const char *timestamp,
158 const char *file, int line, const char *func,
159 uint64_t tid, const char *message, bool use_colors,
160 uint64_t time_nanoseconds);
161
162#ifdef __cplusplus
163}
164#endif
165
unsigned long long uint64_t
int log_template_apply(const log_template_t *format, char *buf, size_t buf_size, log_level_t level, const char *timestamp, const char *file, int line, const char *func, uint64_t tid, const char *message, bool use_colors, uint64_t time_nanoseconds)
Apply format to a log entry and write result to buffer.
Definition log/format.c:516
log_level_t
Logging levels enumeration.
Definition log/types.h:33
void log_template_free(log_template_t *format)
Free compiled format structure.
Definition log/format.c:341
log_template_t * log_template_parse(const char *format_str, bool console_only)
Parse a format string into compiled format structure.
Definition log/format.c:337
log_template_type_t
One literal or conversion token in a compiled log format.
Definition log/format.h:28
@ LOG_FORMAT_LEVEL
Definition log/format.h:31
@ LOG_FORMAT_COLOR
Definition log/format.h:42
@ LOG_FORMAT_NEWLINE
Definition log/format.h:52
@ LOG_FORMAT_FILE_RELATIVE
Definition log/format.h:34
@ LOG_FORMAT_LINE
Definition log/format.h:36
@ LOG_FORMAT_MICROSECONDS
Definition log/format.h:46
@ LOG_FORMAT_TID
Definition log/format.h:38
@ LOG_FORMAT_COLORED_MESSAGE
Definition log/format.h:44
@ LOG_FORMAT_LITERAL
Definition log/format.h:29
@ LOG_FORMAT_COLORLOG_LEVEL
Definition log/format.h:41
@ LOG_FORMAT_MESSAGE
Definition log/format.h:40
@ LOG_FORMAT_TIME
Definition log/format.h:30
@ LOG_FORMAT_LEVEL_ALIGNED
Definition log/format.h:32
@ LOG_FORMAT_FUNC
Definition log/format.h:37
@ LOG_FORMAT_NANOSECONDS
Definition log/format.h:48
@ LOG_FORMAT_TNAME
Definition log/format.h:39
@ LOG_FORMAT_STRFTIME_CODE
Definition log/format.h:50
@ LOG_FORMAT_FILE
Definition log/format.h:33
📝 Logging API with multiple log levels and terminal output control
One literal or conversion token in a compiled log format.
Definition log/format.h:67
log_template_type_t type
Definition log/format.h:68
Compiled log format ready for use in log_template_apply().
Definition log/format.h:82
char * original
Definition log/format.h:85
log_format_spec_t * specs
Definition log/format.h:83
size_t spec_count
Definition log/format.h:84