ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
json.c File Reference

📝 JSON structured logging output using yyjson More...

Go to the source code of this file.

Macros

#define LOG_JSON_JSON_BUFFER_SIZE   2048
#define LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE   512
#define LOG_JSON_ESCAPED_FILE_BUFFER_SIZE   256
#define LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE   128

Functions

void log_json_write (int fd, log_level_t level, uint64_t time_nanoseconds, const char *file, int line, const char *func, const char *message)
 Write a log entry as a JSON object to the json output fd.
void log_json_async_safe (int fd, log_level_t level, const char *file, int line, const char *func, const char *message)
 Async-safe JSON logging for signal handlers.

Detailed Description

📝 JSON structured logging output using yyjson

Definition in file log/json.c.

Macro Definition Documentation

◆ LOG_JSON_ESCAPED_FILE_BUFFER_SIZE

#define LOG_JSON_ESCAPED_FILE_BUFFER_SIZE   256

Definition at line 10 of file log/json.c.

Referenced by log_json_async_safe().

◆ LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE

#define LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE   128

Definition at line 11 of file log/json.c.

Referenced by log_json_async_safe().

◆ LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE

#define LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE   512

Definition at line 9 of file log/json.c.

Referenced by log_json_async_safe().

◆ LOG_JSON_JSON_BUFFER_SIZE

#define LOG_JSON_JSON_BUFFER_SIZE   2048

Definition at line 8 of file log/json.c.

Referenced by log_json_async_safe().

Function Documentation

◆ log_json_async_safe()

void log_json_async_safe ( int fd,
log_level_t level,
const char * file,
int line,
const char * func,
const char * message )

Async-safe JSON logging for signal handlers.

This function formats and writes JSON logs using ONLY async-safe operations:

  • snprintf for string formatting
  • write() for output
  • No allocations, no locks, no library calls

Suitable for calling from signal handlers (SIGTERM, SIGINT, etc.)

Parameters
fdFile descriptor to write to (typically STDOUT_FILENO or STDERR_FILENO)
levelLog level
fileSource file name (may be NULL)
lineSource line number
funcFunction name (may be NULL)
messageLog message (must not be NULL)

Definition at line 282 of file log/json.c.

283 {
284 if (fd < 0 || !message) {
285 return;
286 }
287
288 /* Format JSON manually on stack using only async-safe operations */
289 char json_buffer[LOG_JSON_JSON_BUFFER_SIZE];
290 char escaped_message[LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE];
291 char escaped_file[LOG_JSON_ESCAPED_FILE_BUFFER_SIZE];
292 char escaped_func[LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE];
293 char timestamp_buf[LOG_TIMESTAMP_BUFFER_SIZE];
294
295 /* Escape the strings (normalize file path to project-relative) */
296 json_escape_async_safe(message, escaped_message, sizeof(escaped_message));
297 if (file) {
298 const char *rel_file =
299 extract_project_relative_path(file, ASCIICHAT_OWNED_SOURCE_DIRS);
300 json_escape_async_safe(rel_file, escaped_file, sizeof(escaped_file));
301 } else {
302 escaped_file[0] = '\0';
303 }
304 json_escape_async_safe(func ? func : "", escaped_func, sizeof(escaped_func));
305
306 /* Format timestamp using only async-safe operations */
307 uint64_t time_ns = time_get_realtime_ns();
308 format_timestamp_microseconds(time_ns, timestamp_buf, sizeof(timestamp_buf));
309
310 /* Get thread ID (note: thread_current_id is assumed to be async-safe) */
311 uint64_t tid = (uint64_t)thread_current_id();
312
313 /* Format complete JSON object with timestamp */
314 int written =
315 snprintf(json_buffer, sizeof(json_buffer),
316 "{\"header\":{\"timestamp\":\"%s\",\"level\":\"%s\",\"tid\":%lu,"
317 "\"file\":\"%s\",\"line\":%d,\"func\":\"%s\"},"
318 "\"body\":{\"message\":\"%s\"}}\n",
319 timestamp_buf, log_level_to_string(level), (unsigned long)tid,
320 file ? escaped_file : "", line, func ? escaped_func : "",
321 escaped_message);
322
323 /* Write to fd if successful */
324 if (written > 0 && written < (int)sizeof(json_buffer)) {
325 platform_write_all(fd, (const uint8_t *)json_buffer, (size_t)written);
326 }
327}
unsigned long long uint64_t
unsigned char uint8_t
#define LOG_TIMESTAMP_BUFFER_SIZE
Maximum size of a timestamp string.
Definition log/log.h:95
const char * extract_project_relative_path(const char *file, const char *const *owned_dirs)
Extract a path relative to the source root embedded by CMake.
Definition path.c:137
#define LOG_JSON_JSON_BUFFER_SIZE
Definition log/json.c:8
#define LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE
Definition log/json.c:9
#define LOG_JSON_ESCAPED_FILE_BUFFER_SIZE
Definition log/json.c:10
#define LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE
Definition log/json.c:11

References extract_project_relative_path(), LOG_JSON_ESCAPED_FILE_BUFFER_SIZE, LOG_JSON_ESCAPED_FUNC_BUFFER_SIZE, LOG_JSON_ESCAPED_MESSAGE_BUFFER_SIZE, LOG_JSON_JSON_BUFFER_SIZE, and LOG_TIMESTAMP_BUFFER_SIZE.

Referenced by log_console_impl().

◆ log_json_write()

void log_json_write ( int fd,
log_level_t level,
uint64_t time_nanoseconds,
const char * file,
int line,
const char * func,
const char * message )

Write a log entry as a JSON object to the json output fd.

Outputs a newline-delimited JSON (NDJSON) format log entry with the following structure:

{
"header": {
"timestamp": "14:30:45.123456",
"level": "INFO",
"tid": 12345,
"file": "lib/network/server.c",
"line": 42,
"func": "handle_client"
},
"body": {
"message": "Client connected"
}
}

The file, line, and func fields are omitted from the output when they are NULL/0 (which typically occurs in release builds).

Parameters
fdFile descriptor to write JSON to (-1 = disabled)
levelLog level (DEBUG, INFO, WARN, ERROR, FATAL)
time_nanosecondsCurrent time in nanoseconds (used to format timestamp)
fileSource file name (can be NULL)
lineLine number (can be 0)
funcFunction name (can be NULL)
messageFormatted log message
See also
log_set_json_output() - Set the JSON output file descriptor

Definition at line 106 of file log/json.c.

108 {
109 if (fd < 0 || !message) {
110 return;
111 }
112
113 /* Create root object */
114 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
115 if (!doc) {
116 return;
117 }
118
119 yyjson_mut_val *root = yyjson_mut_obj(doc);
120 if (!root) {
121 yyjson_mut_doc_free(doc);
122 return;
123 }
124 yyjson_mut_doc_set_root(doc, root);
125
126 /* Create "header" object */
127 yyjson_mut_val *header = yyjson_mut_obj(doc);
128 if (header) {
129 yyjson_mut_obj_add_val(doc, root, "header", header);
130
131 /* Add timestamp (HH:MM:SS.microseconds) */
132 char timestamp_buf[LOG_TIMESTAMP_BUFFER_SIZE];
133 size_t ts_len = format_timestamp_microseconds(
134 time_nanoseconds, timestamp_buf, sizeof(timestamp_buf));
135 if (ts_len > 0) {
136 yyjson_mut_obj_add_strncpy(doc, header, "timestamp", timestamp_buf,
137 ts_len);
138 }
139
140 /* Add level */
141 yyjson_mut_obj_add_str(doc, header, "level", log_level_to_string(level));
142
143 /* Add thread ID */
144 uint64_t tid = (uint64_t)thread_current_id();
145 yyjson_mut_obj_add_uint(doc, header, "tid", tid);
146
147 /* Add file (if not NULL) - normalize to project-relative path */
148 if (file) {
149 const char *rel_file =
150 extract_project_relative_path(file, ASCIICHAT_OWNED_SOURCE_DIRS);
151 yyjson_mut_obj_add_str(doc, header, "file", rel_file);
152 }
153
154 /* Add line (if > 0) */
155 if (line > 0) {
156 yyjson_mut_obj_add_int(doc, header, "line", line);
157 }
158
159 /* Add func (if not NULL) */
160 if (func) {
161 yyjson_mut_obj_add_str(doc, header, "func", func);
162 }
163 }
164
165 /* Create "body" object with message */
166 yyjson_mut_val *body = yyjson_mut_obj(doc);
167 if (body) {
168 yyjson_mut_obj_add_val(doc, root, "body", body);
169 yyjson_mut_obj_add_str(doc, body, "message", message);
170 }
171
172 /* Serialize to compact JSON (single line, no pretty-printing) */
173 size_t json_len = 0;
174 char *json_str = yyjson_mut_write(doc, 0, &json_len);
175
176 if (json_str && json_len > 0) {
177 // Include the delimiter in the same write so concurrent records stay on
178 // separate lines.
179 json_str[json_len] = '\n';
180 platform_write_all(fd, (const uint8_t *)json_str, json_len + 1);
181
182 /* Free serialized JSON string */
183 free(json_str);
184 }
185
186 /* Free document */
187 yyjson_mut_doc_free(doc);
188}

References extract_project_relative_path(), and LOG_TIMESTAMP_BUFFER_SIZE.

Referenced by log_msg(), and log_plain_msg().