Use map for storing available log levels

This commit is contained in:
Uwe Seimet 2021-08-05 12:59:53 +02:00
parent 0d20cb4401
commit 505751e64a

View File

@ -35,7 +35,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <list> #include <map>
#include <filesystem> #include <filesystem>
using namespace std; using namespace std;
@ -65,7 +65,7 @@ int monsocket; // Monitor Socket
pthread_t monthread; // Monitor Thread pthread_t monthread; // Monitor Thread
pthread_mutex_t ctrl_mutex; // Semaphore for the ctrl array pthread_mutex_t ctrl_mutex; // Semaphore for the ctrl array
static void *MonThread(void *param); static void *MonThread(void *param);
list<string> available_log_levels; map<string, level::level_enum> available_log_levels;
string current_log_level; // Some versions of spdlog do not support get_log_level() string current_log_level; // Some versions of spdlog do not support get_log_level()
string default_image_folder = "/home/pi/images"; string default_image_folder = "/home/pi/images";
@ -434,31 +434,13 @@ bool ReturnStatus(int fd, bool status = true, const string msg = "") {
} }
bool SetLogLevel(const string& log_level) { bool SetLogLevel(const string& log_level) {
if (log_level == "trace") { map<string, level::level_enum>::iterator it = available_log_levels.find(log_level);
set_level(level::trace); if (it == available_log_levels.end()) {
}
else if (log_level == "debug") {
set_level(level::debug);
}
else if (log_level == "info") {
set_level(level::info);
}
else if (log_level == "warn") {
set_level(level::warn);
}
else if (log_level == "err") {
set_level(level::err);
}
else if (log_level == "critical") {
set_level(level::critical);
}
else if (log_level == "off") {
set_level(level::off);
}
else {
return false; return false;
} }
set_level(it->second);
current_log_level = log_level; current_log_level = log_level;
return true; return true;
@ -477,7 +459,7 @@ void LogDeviceList(const string& device_list)
void GetAvailableLogLevels(PbServerInfo& serverInfo) void GetAvailableLogLevels(PbServerInfo& serverInfo)
{ {
for (auto it = available_log_levels.begin(); it != available_log_levels.end(); ++it) { for (auto it = available_log_levels.begin(); it != available_log_levels.end(); ++it) {
serverInfo.add_available_log_levels(*it); serverInfo.add_available_log_levels((*it).first);
} }
} }
@ -964,13 +946,13 @@ int main(int argc, char* argv[])
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
struct sched_param schparam; struct sched_param schparam;
available_log_levels.push_back("trace"); available_log_levels["trace"] = level::trace;
available_log_levels.push_back("debug"); available_log_levels["debug"] = level::debug;
available_log_levels.push_back("info"); available_log_levels["info"] = level::info;
available_log_levels.push_back("warn"); available_log_levels["warn"] = level::warn;
available_log_levels.push_back("err"); available_log_levels["err"] = level::err;
available_log_levels.push_back("critical"); available_log_levels["critical"] = level::critical;
available_log_levels.push_back("off"); available_log_levels["off"] = level::off;
SetLogLevel("trace"); SetLogLevel("trace");
// Create a thread-safe stdout logger to process the log messages // Create a thread-safe stdout logger to process the log messages