Zth (libzth)
Loading...
Searching...
No Matches
config.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 */
6
7#include <libzth/config.h>
8
9#include <libzth/util.h>
10
11#include <cstdlib>
12#include <cstring>
13
14namespace zth {
15
19static bool config(char const* name, bool whenUnset)
20{
21 // NOLINTNEXTLINE(concurrency-mt-unsafe)
22 char const* e = getenv(name);
23
24 if(!e || !*e)
25 return whenUnset;
26 else if(strcmp(e, "0") == 0)
27 // Explicitly set to disabled
28 return false;
29 else
30 // Any other value is true
31 return true;
32}
33
39bool config(int env, bool whenUnset)
40{
41 switch(env) {
42 case Env::EnableDebugPrint: {
43 static bool const e = Config::SupportDebugPrint
44 && config("ZTH_CONFIG_ENABLE_DEBUG_PRINT", whenUnset);
45 return e;
46 }
47 case Env::DoPerfEvent: {
48 static bool const e = config("ZTH_CONFIG_DO_PERF_EVENT", whenUnset);
49 return e;
50 }
51 case Env::PerfSyscall: {
52 static bool const e = config("ZTH_CONFIG_PERF_SYSCALL", whenUnset);
53 return e;
54 }
55 case Env::CheckTimesliceOverrun: {
56 static bool const e = config("ZTH_CONFIG_CHECK_TIMESLICE_OVERRUN", whenUnset);
57 return e;
58 }
59 default:
60 return whenUnset;
61 }
62}
63
70void checkConfig(int check, size_t value)
71{
72 bool ok = false;
73 char const* name = nullptr;
74
75#define ZTH_CHECK(x) \
76 case Check::Config_##x: \
77 ok = (size_t)Config::x == value; \
78 if(Config::EnableFullAssert) \
79 name = "" #x; \
80 break;
81
82 switch(check) {
83 ZTH_CHECK(Debug)
84 ZTH_CHECK(EnableAssert)
85 ZTH_CHECK(EnableFullAssert)
86 ZTH_CHECK(EnableBacktrace)
87 ZTH_CHECK(EnableThreads)
88 ZTH_CHECK(SupportDebugPrint)
89 ZTH_CHECK(EnableColorLog)
90 ZTH_CHECK(DefaultFiberStackSize)
91 ZTH_CHECK(EnableStackGuard)
92 ZTH_CHECK(EnableStackWaterMark)
93 ZTH_CHECK(ContextSignals)
94 ZTH_CHECK(CheckTimesliceOverrun)
95 ZTH_CHECK(PerfEventBufferSize)
96 ZTH_CHECK(EnablePerfEvent)
97 ZTH_CHECK(PerfSyscall)
98 ZTH_CHECK(UseZMQ)
99 ZTH_CHECK(UseLimitedFormatSpecifiers)
100 ZTH_CHECK(EnableExceptions)
101 default:;
102 }
103
104#undef ZTH_CHECK
105
106 if(!ok) {
107 if(Config::EnableFullAssert && name)
108 abort("Config check %s failed", name);
109 else
110 abort("Config check %d failed", check);
111 }
112}
113
114} // namespace zth
#define ZTH_CHECK(x)
void abort(char const *fmt,...) noexcept
Aborts the process after printing the given printf() formatted message.
Definition util.cpp:149
void checkConfig(int check, size_t value)
Check if a given Config field is the same as the given value.
Definition config.cpp:70
bool config(int env, bool whenUnset)
Checks if a given environment option is set.
Definition config.cpp:39