Skip to content

Commit ae04607

Browse files
killownkillownammen99
authored
Refactor load_xml_files: suppress per-file logging, summarize loaded … (#80)
* Refactor load_xml_files: suppress per-file logging, summarize loaded XML files per directory * Fix code style --------- Co-authored-by: killown <systemofdown@gmai.com> Co-authored-by: Ilia Bozhinov <ammen99@gmail.com>
1 parent 6e25b6d commit ae04607

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/file.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class line_t : public std::string
4040

4141
size_t source_line_number;
4242
};
43+
4344
using lines_t = std::vector<line_t>;
4445

4546
static lines_t split_to_lines(const std::string& source)
@@ -214,9 +215,8 @@ enum option_parsing_result
214215
};
215216

216217
/**
217-
* Try to parse an option line.
218-
* If the option line is valid, the corresponding option is modified or added
219-
* to @current_section, and the option is added to @reloaded.
218+
* Try to parse an option line. If the option line is valid, the corresponding option is modified or added to
219+
* @current_section, and the option is added to @reloaded.
220220
*
221221
* @return The parse status of the line.
222222
*/
@@ -252,9 +252,8 @@ static option_parsing_result parse_option_line(
252252
}
253253

254254
/**
255-
* Check whether the @line is a valid section start.
256-
* If yes, it will either return the section in @config with the same name, or
257-
* create a new section and register it in config.
255+
* Check whether the @line is a valid section start. If yes, it will either return the section in @config with
256+
* the same name, or create a new section and register it in config.
258257
*
259258
* @return nullptr if line is not a valid section, the section otherwise.
260259
*/
@@ -518,16 +517,13 @@ void wf::config::save_configuration_to_file(
518517
flock(fd, LOCK_UN);
519518
close(fd);
520519

521-
/* Modify the file one last time. Now programs waiting for updates can
522-
* acquire a shared lock. */
520+
/* Modify the file one last time. Now programs waiting for updates can acquire a shared lock. */
523521
fout << std::endl;
524522
}
525523

526524
static void process_xml_file(wf::config::config_manager_t& manager,
527525
const std::string & filename)
528526
{
529-
LOGI("Reading XML configuration options from file ", filename);
530-
531527
/* Parse the XML file. */
532528
auto doc = xmlParseFile(filename.c_str());
533529
if (!doc)
@@ -562,23 +558,23 @@ static void process_xml_file(wf::config::config_manager_t& manager,
562558
// xmlFreeDoc(doc); - May clear the XML nodes before they are used
563559
}
564560

565-
static wf::config::config_manager_t load_xml_files(
566-
const std::vector<std::string>& xmldirs)
561+
static wf::config::config_manager_t load_xml_files(const std::vector<std::string>& xmldirs)
567562
{
568563
wf::config::config_manager_t manager;
569564

570565
for (auto& xmldir : xmldirs)
571566
{
572567
auto xmld = opendir(xmldir.c_str());
573-
if (NULL == xmld)
568+
if (!xmld)
574569
{
575570
LOGW("Failed to open XML directory ", xmldir);
576571
continue;
577572
}
578573

579-
LOGI("Reading XML configuration options from directory ", xmldir);
574+
std::vector<std::string> loaded_files;
575+
580576
struct dirent *entry;
581-
while ((entry = readdir(xmld)) != NULL)
577+
while ((entry = readdir(xmld)) != nullptr)
582578
{
583579
if ((entry->d_type != DT_LNK) && (entry->d_type != DT_REG) &&
584580
(entry->d_type != DT_UNKNOWN))
@@ -591,10 +587,29 @@ static wf::config::config_manager_t load_xml_files(
591587
(filename.rfind(".xml") == filename.length() - 4))
592588
{
593589
process_xml_file(manager, filename);
590+
loaded_files.push_back(entry->d_name);
594591
}
595592
}
596593

597594
closedir(xmld);
595+
596+
if (!loaded_files.empty())
597+
{
598+
LOGI("Loaded XML configuration options from ", loaded_files.size(),
599+
" files in ", xmldir, ":");
600+
601+
std::string list;
602+
for (size_t i = 0; i < loaded_files.size(); ++i)
603+
{
604+
list += loaded_files[i];
605+
if (i + 1 != loaded_files.size())
606+
{
607+
list += ", ";
608+
}
609+
}
610+
611+
LOGI(list);
612+
}
598613
}
599614

600615
return manager;

0 commit comments

Comments
 (0)