Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init the logging parts of the config before calling init_logging #751

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ DEFINE_OPTION_TYPE_GETTER(uint32_t, config_option_type::uint32)
DEFINE_OPTION_TYPE_GETTER(bool, config_option_type::boolean)

config_option gConfigOptions[] = {
#define OPTION(type, name, valdef) {option_type<type>(), #name, &config.name},
#define OPTION(type, name, valdef) \
{option_type<type>(), #name, &config.name, false},
#define EARLY_OPTION(type, name, valdef) \
{option_type<type>(), #name, &config.name, true},
#include "config.def"
#undef EARLY_OPTION
#undef OPTION
};

Expand Down Expand Up @@ -121,7 +125,7 @@ void read_config_file(std::unordered_map<std::string, std::string>& umap,
config_stream.close();
}

void parse_config_file() {
void parse_config_file(bool early_option) {
std::unordered_map<std::string, std::string> file_config_values;
std::string conf_file = "clvk.conf";
std::ifstream config_stream;
Expand Down Expand Up @@ -157,6 +161,9 @@ void parse_config_file() {
}

for (auto& opt : gConfigOptions) {
if (early_option != opt.early_option) {
continue;
}
if (file_config_values.find(opt.name) == file_config_values.end()) {
continue;
}
Expand All @@ -178,16 +185,17 @@ void parse_config_file() {
return;
}

void parse_env() {
void parse_env(bool early_option) {
for (auto& opt : gConfigOptions) {
// printf("var_name = '%s' ", var_name.c_str());
if (early_option != opt.early_option) {
continue;
}
auto var_name = get_clvk_env_name(opt.name);
const char* txt = getenv(var_name.c_str());
if (txt == nullptr) {
// printf("is not set\n");
continue;
}
// printf("is set\n");
cvk_debug_group_fn(loggroup::cfg, "'%s' = '%s'", var_name.c_str(), txt);
void* optval = const_cast<void*>(opt.value);
switch (opt.type) {
case config_option_type::string:
Expand All @@ -205,9 +213,12 @@ void parse_env() {

} // namespace

void init_config_from_env_only() { parse_env(); }

void init_config() {
parse_config_file();
parse_env();
parse_config_file(false);
parse_env(false);
}

void init_early_config() {
parse_config_file(true);
parse_env(true);
}
8 changes: 4 additions & 4 deletions src/config.def
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ OPTION(std::string, device_extensions_masked, "")
//
// Logging
//
OPTION(uint32_t, log, 0u)
OPTION(bool, log_colour, false)
OPTION(std::string, log_dest, "")
OPTION(std::string, log_groups, "")
EARLY_OPTION(uint32_t, log, 0u)
EARLY_OPTION(bool, log_colour, false)
EARLY_OPTION(std::string, log_dest, "")
EARLY_OPTION(std::string, log_groups, "")

//
// Debug
Expand Down
7 changes: 5 additions & 2 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct config_option {
config_option_type type;
std::string name;
const void* value;
bool early_option;
};

template <typename T> struct config_value {
Expand All @@ -48,12 +49,14 @@ template <typename T> struct config_value {

struct config_struct {
#define OPTION(type, name, valdef) const config_value<type> name{valdef};
#define EARLY_OPTION OPTION
#include "config.def"
#undef EARLY_OPTION
#undef OPTION
};

extern const config_struct config;

extern void init_config_from_env_only();

extern void init_config();

extern void init_early_config();
4 changes: 1 addition & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ void clvk_global_state::init_executors() {
void clvk_global_state::term_executors() { delete m_thread_pool; }

clvk_global_state::clvk_global_state() {
// Init the configuration using environment variables before logging so
// we can enable full logging support before parsing configuration files.
init_config_from_env_only();
init_early_config();
init_logging();
init_config();
cvk_info("Starting initialisation");
Expand Down