Skip to content

Commit

Permalink
add compile commands for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoKle committed Apr 9, 2024
1 parent 1701450 commit c91928e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
41 changes: 39 additions & 2 deletions src/log/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <chrono>
#include <numeric>

#include "utils/String.h"

using namespace std::chrono_literals;
using namespace vacdm::logging;

Expand Down Expand Up @@ -59,7 +61,8 @@ void Logger::run() {
auto logsetting = std::find_if(logSettings.begin(), logSettings.end(),
[it](const LogSetting &setting) { return setting.sender == it->sender; });

if (logsetting != logSettings.end() && it->loglevel >= logsetting->minimumLevel) {
if (logsetting != logSettings.end() && it->loglevel >= logsetting->minimumLevel &&
false == this->m_LogAll) {
#ifdef DEBUG_BUILD
std::cout << logsetting->name << ": " << it->message << "\n";
#endif
Expand All @@ -85,7 +88,41 @@ void Logger::log(const LogSender &sender, const std::string &message, const LogL
if (true == this->loggingEnabled) m_asynchronousLogs.push_back({sender, message, loglevel});
}

std::string Logger::handleLogLevelCommand(std::string sender, std::string newLevel) {
std::string Logger::handleLogCommand(std::string command) {
auto elements = vacdm::utils::String::splitString(command, " ");

std::string usageString = "Usage: .vacdm LOG ON/OFF/DEBUG";
if (elements.size() != 3) return usageString;

if ("ON" == elements[2]) {
this->enableLogging();
return "Enabled logging";
} else if ("OFF" == elements[2]) {
this->disableLogging();
return "Disabled logging";
} else if ("DEBUG" == elements[2]) {
std::lock_guard guard(this->m_logLock);
if (false == this->m_LogAll) {
this->m_LogAll = true;
return "Set all log levels to DEBUG";
} else {
this->m_LogAll = false;
return "Reset log levels, using previous settings";
}
}

return usageString;
}

std::string Logger::handleLogLevelCommand(std::string command) {
const auto elements = vacdm::utils::String::splitString(command, " ");
if (elements.size() != 4) {
return "Usage: .vacdm LOGLEVEL sender loglevel";
}

std::string sender = elements[2];
std::string newLevel = elements[3];

std::lock_guard guard(this->m_logLock);
auto logsetting = std::find_if(logSettings.begin(), logSettings.end(), [sender](const LogSetting &setting) {
std::string uppercaseName = setting.name;
Expand Down
4 changes: 3 additions & 1 deletion src/log/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Logger {
{Utils, "Utils", Disabled},
};
#endif
bool m_LogAll = false;

std::mutex m_logLock;
std::list<struct AsynchronousLog> m_asynchronousLogs;
Expand All @@ -80,7 +81,8 @@ class Logger {
/// @param message the message to be displayed
/// @param loglevel the severity, must be greater than m_minimumLogLevel to be logged
void log(const LogSender &sender, const std::string &message, const LogLevel loglevel);
std::string handleLogLevelCommand(std::string sender, std::string newLevel);
std::string handleLogCommand(std::string command);
std::string handleLogLevelCommand(std::string command);
static Logger &instance();
};
} // namespace vacdm::logging
9 changes: 4 additions & 5 deletions src/vACDM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,11 @@ bool vACDM::OnCompileCommand(const char *sCommandLine) {
} else if (std::string::npos != command.find("RELOAD")) {
this->reloadConfiguration();
return true;
} else if (std::string::npos != command.find("LOGLEVEL")) {
const auto elements = vacdm::utils::String::splitString(command, " ");
if (elements.size() == 4) {
DisplayMessage(Logger::instance().handleLogLevelCommand(elements[2], elements[3]));
} else if (std::string::npos != command.find("LOG")) {
if (std::string::npos != command.find("LOGLEVEL")) {
DisplayMessage(Logger::instance().handleLogLevelCommand(command));
} else {
DisplayMessage("Usage: .vacdm LOGLEVEL sender loglevel");
DisplayMessage(Logger::instance().handleLogCommand(command));
}
return true;
} else if (std::string::npos != command.find("UPDATERATE")) {
Expand Down

0 comments on commit c91928e

Please sign in to comment.