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:56–107  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

54
55/* CBM_LOG_LEVEL support — distilled from #414 (closes #413, thanks @santanusinha). */
56void cbm_log_init_from_env(void) {
57 /* Resolve the immutable startup override before the inherited environment. */
58 const char *raw = cbm_runtime_getenv("CBM_LOG_LEVEL");
59 if (raw && raw[0] != '\0') {
60 /* Textual form, case-insensitive. Index of each name == its enum value. */
61 static const char *const names[] = {"debug", "info", "warn", "error", "none"};
62 char lower[8];
63 size_t i = 0;
64 for (; i < sizeof(lower) - 1 && raw[i] != '\0'; i++) {
65 lower[i] = (char)tolower((unsigned char)raw[i]);
66 }
67 lower[i] = '\0';
68 if (raw[i] == '\0') { /* fully consumed — candidate textual match */
69 for (size_t lvl = 0; lvl < sizeof(names) / sizeof(names[0]); lvl++) {
70 if (strcmp(lower, names[lvl]) == 0) {
71 cbm_log_set_level((CBMLogLevel)lvl);
72 goto parse_format;
73 }
74 }
75 }
76
77 /* Numeric form: 0=debug .. 4=none, matching CBMLogLevel. */
78 char *end = NULL;
79 long n = strtol(raw, &end, CBM_DECIMAL_BASE);
80 if (end != raw && *end == '\0' && n >= CBM_LOG_DEBUG && n <= CBM_LOG_NONE) {
81 cbm_log_set_level((CBMLogLevel)n);
82 }
83 }
84
85 /* Unrecognised value: leave the level unchanged (fail-open). */
86
87parse_format:;
88 const char *fmt = cbm_runtime_getenv("CBM_LOG_FORMAT");
89 if (fmt && fmt[0] != '\0') {
90 char lower_fmt[8];
91 size_t i = 0;
92 for (; i < sizeof(lower_fmt) - 1 && fmt[i] != '\0'; i++) {
93 lower_fmt[i] = (char)tolower((unsigned char)fmt[i]);
94 }
95 lower_fmt[i] = '\0';
96 if (fmt[i] == '\0' && strcmp(lower_fmt, "json") == 0) {
97 cbm_log_set_format(CBM_LOG_FORMAT_JSON);
98 } else if (fmt[i] == '\0' && strcmp(lower_fmt, "text") == 0) {
99 cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
100 }
101 return;
102 }
103
104 /* Format is intentionally explicit-only. Logs stay local to stderr and the
105 * optional in-process sink; deployment environment variables must not
106 * silently change the operator-selected output shape. */
107}
108
109void cbm_log_init_for_process(bool quiet_by_default, bool require_info_liveness) {
110 cbm_log_set_level(quiet_by_default ? CBM_LOG_WARN : CBM_LOG_INFO);

Callers 2

cbm_log_init_for_processFunction · 0.85
test_log.cFile · 0.85

Calls 3

cbm_runtime_getenvFunction · 0.85
cbm_log_set_levelFunction · 0.85
cbm_log_set_formatFunction · 0.85

Tested by

no test coverage detected