| 228 | } |
| 229 | |
| 230 | void cbm_log(CBMLogLevel level, const char *msg, ...) { |
| 231 | if (level < atomic_load_explicit(&g_log_level, memory_order_relaxed)) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | char line_buf[CBM_SZ_4K]; |
| 236 | size_t pos = 0; |
| 237 | va_list args; |
| 238 | va_start(args, msg); |
| 239 | |
| 240 | if (atomic_load_explicit(&g_log_format, memory_order_relaxed) == CBM_LOG_FORMAT_JSON) { |
| 241 | append_raw(line_buf, sizeof(line_buf), &pos, "{\"level\":"); |
| 242 | append_json_string(line_buf, sizeof(line_buf), &pos, level_str(level)); |
| 243 | append_raw(line_buf, sizeof(line_buf), &pos, ",\"event\":"); |
| 244 | append_json_string(line_buf, sizeof(line_buf), &pos, msg ? msg : ""); |
| 245 | for (;;) { |
| 246 | const char *key = va_arg(args, const char *); |
| 247 | if (!key) { |
| 248 | break; |
| 249 | } |
| 250 | const char *val = va_arg(args, const char *); |
| 251 | append_char(line_buf, sizeof(line_buf), &pos, ','); |
| 252 | append_json_string(line_buf, sizeof(line_buf), &pos, key); |
| 253 | append_char(line_buf, sizeof(line_buf), &pos, ':'); |
| 254 | append_json_string(line_buf, sizeof(line_buf), &pos, val ? val : ""); |
| 255 | } |
| 256 | append_char(line_buf, sizeof(line_buf), &pos, '}'); |
| 257 | } else { |
| 258 | append_raw(line_buf, sizeof(line_buf), &pos, "level="); |
| 259 | append_text_atom(line_buf, sizeof(line_buf), &pos, level_str(level)); |
| 260 | append_raw(line_buf, sizeof(line_buf), &pos, " msg="); |
| 261 | append_text_atom(line_buf, sizeof(line_buf), &pos, msg ? msg : ""); |
| 262 | for (;;) { |
| 263 | const char *key = va_arg(args, const char *); |
| 264 | if (!key) { |
| 265 | break; |
| 266 | } |
| 267 | const char *val = va_arg(args, const char *); |
| 268 | append_char(line_buf, sizeof(line_buf), &pos, ' '); |
| 269 | append_text_atom(line_buf, sizeof(line_buf), &pos, key); |
| 270 | append_char(line_buf, sizeof(line_buf), &pos, '='); |
| 271 | append_text_atom(line_buf, sizeof(line_buf), &pos, val ? val : ""); |
| 272 | } |
| 273 | } |
| 274 | va_end(args); |
| 275 | |
| 276 | finish_line(line_buf, sizeof(line_buf), pos); |
| 277 | emit_line(line_buf); |
| 278 | } |
| 279 | |
| 280 | void cbm_log_control_record(const char *msg, ...) { |
| 281 | /* No threshold check: a control record's whole purpose is surviving |
no test coverage detected