MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_log_init_from_env

Function cbm_log_init_from_env

src/foundation/log.c:36–88  ·  view source on GitHub ↗

CBM_LOG_LEVEL support — distilled from #414 (closes #413, thanks @santanusinha). */

Source from the content-addressed store, hash-verified

34
35/* CBM_LOG_LEVEL support — distilled from #414 (closes #413, thanks @santanusinha). */
36void cbm_log_init_from_env(void) {
37 /* getenv() is safe here: this runs at startup before any thread is created,
38 * so there is no concurrent setenv() to race against. */
39 const char *raw = getenv("CBM_LOG_LEVEL");
40 if (raw && raw[0] != '\0') {
41 /* Textual form, case-insensitive. Index of each name == its enum value. */
42 static const char *const names[] = {"debug", "info", "warn", "error", "none"};
43 char lower[8];
44 size_t i = 0;
45 for (; i < sizeof(lower) - 1 && raw[i] != '\0'; i++) {
46 lower[i] = (char)tolower((unsigned char)raw[i]);
47 }
48 lower[i] = '\0';
49 if (raw[i] == '\0') { /* fully consumed — candidate textual match */
50 for (size_t lvl = 0; lvl < sizeof(names) / sizeof(names[0]); lvl++) {
51 if (strcmp(lower, names[lvl]) == 0) {
52 cbm_log_set_level((CBMLogLevel)lvl);
53 goto parse_format;
54 }
55 }
56 }
57
58 /* Numeric form: 0=debug .. 4=none, matching CBMLogLevel. */
59 char *end = NULL;
60 long n = strtol(raw, &end, CBM_DECIMAL_BASE);
61 if (end != raw && *end == '\0' && n >= CBM_LOG_DEBUG && n <= CBM_LOG_NONE) {
62 cbm_log_set_level((CBMLogLevel)n);
63 }
64 }
65
66 /* Unrecognised value: leave the level unchanged (fail-open). */
67
68parse_format:;
69 const char *fmt = getenv("CBM_LOG_FORMAT");
70 if (fmt && fmt[0] != '\0') {
71 char lower_fmt[8];
72 size_t i = 0;
73 for (; i < sizeof(lower_fmt) - 1 && fmt[i] != '\0'; i++) {
74 lower_fmt[i] = (char)tolower((unsigned char)fmt[i]);
75 }
76 lower_fmt[i] = '\0';
77 if (fmt[i] == '\0' && strcmp(lower_fmt, "json") == 0) {
78 cbm_log_set_format(CBM_LOG_FORMAT_JSON);
79 } else if (fmt[i] == '\0' && strcmp(lower_fmt, "text") == 0) {
80 cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
81 }
82 return;
83 }
84
85 /* Format is intentionally explicit-only. Logs stay local to stderr and the
86 * optional in-process sink; deployment environment variables must not
87 * silently change the operator-selected output shape. */
88}
89
90void cbm_log_set_sink(cbm_log_sink_fn fn) {
91 cbm_log_set_sink_ex(fn, CBM_LOG_SINK_REPLACE);

Callers 2

mainFunction · 0.85
test_log.cFile · 0.85

Calls 2

cbm_log_set_levelFunction · 0.85
cbm_log_set_formatFunction · 0.85

Tested by

no test coverage detected