Skip to content

Commit

Permalink
Log config file statistics after writing
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 1, 2023
1 parent 74414c0 commit 09df180
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/config/toml_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ bool writeFTLtoml(const bool verbose)
return false;
}

// Log that we are (re-)writing the config file if either in verbose or
// debug mode
if(verbose || config.debug.config.v.b)
log_info("Writing config file");
// Log that we are (re-)writing the config file in debug mode
log_debug(DEBUG_CONFIG, "Writing config file");

// Write header
fputs("# This file is managed by pihole-FTL\n#\n", fp);
Expand All @@ -54,6 +52,7 @@ bool writeFTLtoml(const bool verbose)

// Iterate over configuration and store it into the file
char *last_path = (char*)"";
unsigned int modified = 0, env_vars = 0;
for(unsigned int i = 0; i < CONFIG_ELEMENTS; i++)
{
// Get pointer to memory location of this conf_item
Expand Down Expand Up @@ -90,7 +89,10 @@ bool writeFTLtoml(const bool verbose)

// Print info if this value is overwritten by an env var
if(conf_item->f & FLAG_ENV_VAR)
{
print_comment(fp, ">>> This config is overwritten by an environmental variable <<<", "", 85, level-1);
env_vars++;
}

// Write value
indentTOML(fp, level-1);
Expand All @@ -110,12 +112,26 @@ bool writeFTLtoml(const bool verbose)
{
fprintf(fp, " ### CHANGED, default = ");
writeTOMLvalue(fp, -1, conf_item->t, &conf_item->d);
modified++;
}

// Add newlines after each entry
fputs("\n\n", fp);
}

// Log some statistics in verbose mode
if(verbose || config.debug.config.v.b)
{
log_info("Wrote config file:");
log_info(" - %zu total entries", CONFIG_ELEMENTS);
log_info(" - %zu %s default", CONFIG_ELEMENTS - modified,
CONFIG_ELEMENTS - modified == 1 ? "entry is" : "entries are");
log_info(" - %u %s modified", modified,
modified == 1 ? "entry is" : "entries are");
log_info(" - %u %s forced through environment", env_vars,
env_vars == 1 ? "entry is" : "entries are");
}

// Close file and release exclusive lock
closeFTLtoml(fp);

Expand Down

0 comments on commit 09df180

Please sign in to comment.