Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-or-later
3 : *
4 : * This file is part of colopresso
5 : *
6 : * Copyright (C) 2025 COLOPL, Inc.
7 : *
8 : * Author: Go Kudo <g-kudo@colopl.co.jp>
9 : * Developed with AI (LLM) code assistance. See `NOTICE` for details.
10 : */
11 :
12 : #include <stdarg.h>
13 : #include <stdio.h>
14 :
15 : #include <colopresso.h>
16 :
17 : #include "internal/log.h"
18 :
19 : #ifndef CPRES_DEBUG
20 : #ifdef DEBUG
21 : #define CPRES_DEBUG 1
22 : #else
23 : #define CPRES_DEBUG 0
24 : #endif
25 : #endif
26 :
27 : static cpres_log_callback_t g_log_callback = NULL;
28 :
29 1 : void cpres_log_init(void) { g_log_callback = NULL; }
30 :
31 301 : void cpres_log(cpres_log_level_t level, const char *format, ...) {
32 301 : const char truncated[] = "... [truncated]";
33 : char buffer[2048];
34 : int result;
35 : va_list args;
36 : size_t pos;
37 :
38 301 : if (g_log_callback) {
39 182 : va_start(args, format);
40 182 : result = vsnprintf(buffer, sizeof(buffer), format, args);
41 182 : va_end(args);
42 :
43 182 : if (result >= (int)sizeof(buffer)) {
44 1 : pos = sizeof(buffer) - sizeof(truncated) - 1;
45 1 : if (pos > 0) {
46 1 : snprintf(buffer + pos, sizeof(truncated), "%s", truncated);
47 : }
48 : }
49 :
50 182 : g_log_callback(level, buffer);
51 : } else if (CPRES_DEBUG && level >= CPRES_LOG_LEVEL_DEBUG) {
52 : va_start(args, format);
53 : vfprintf(stderr, format, args);
54 : va_end(args);
55 : fprintf(stderr, "\n");
56 : }
57 301 : }
58 :
59 84 : void cpres_set_log_callback(cpres_log_callback_t callback) { g_log_callback = callback; }
|