fixed logging pattern
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
// Global logger instance
|
||||
Logger g_logger;
|
||||
|
||||
Logger::Logger()
|
||||
: log_level(DPMDefaults::LOG_LEVEL),
|
||||
Logger::Logger()
|
||||
: log_level(DPMDefaults::LOG_LEVEL),
|
||||
log_to_file(DPMDefaults::write_to_log),
|
||||
log_file(DPMDefaults::LOG_FILE)
|
||||
{
|
||||
@@ -18,7 +18,7 @@ Logger::~Logger()
|
||||
void Logger::setLogFile(const std::string& new_log_file)
|
||||
{
|
||||
log_file = new_log_file;
|
||||
|
||||
|
||||
// If logging to file is enabled, ensure the log directory exists and is writable
|
||||
if (log_to_file) {
|
||||
std::filesystem::path log_path(log_file);
|
||||
@@ -55,7 +55,7 @@ void Logger::setLogFile(const std::string& new_log_file)
|
||||
void Logger::setWriteToLog(bool new_write_to_log)
|
||||
{
|
||||
log_to_file = new_write_to_log;
|
||||
|
||||
|
||||
// If logging was just enabled, validate the log file
|
||||
if (log_to_file) {
|
||||
setLogFile(log_file);
|
||||
@@ -67,6 +67,24 @@ void Logger::setLogLevel(LoggingLevels new_log_level)
|
||||
log_level = new_log_level;
|
||||
}
|
||||
|
||||
std::string Logger::LogLevelToString(LoggingLevels level)
|
||||
{
|
||||
switch (level) {
|
||||
case LoggingLevels::FATAL:
|
||||
return "FATAL";
|
||||
case LoggingLevels::ERROR:
|
||||
return "ERROR";
|
||||
case LoggingLevels::WARN:
|
||||
return "WARN";
|
||||
case LoggingLevels::INFO:
|
||||
return "INFO";
|
||||
case LoggingLevels::DEBUG:
|
||||
return "DEBUG";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
LoggingLevels Logger::stringToLogLevel(const std::string& level_str, LoggingLevels default_level)
|
||||
{
|
||||
if (level_str == "FATAL") {
|
||||
@@ -80,7 +98,7 @@ LoggingLevels Logger::stringToLogLevel(const std::string& level_str, LoggingLeve
|
||||
} else if (level_str == "DEBUG") {
|
||||
return LoggingLevels::DEBUG;
|
||||
}
|
||||
|
||||
|
||||
// Return default if no match
|
||||
return default_level;
|
||||
}
|
||||
@@ -90,27 +108,7 @@ void Logger::log(LoggingLevels message_level, const std::string& message)
|
||||
// Only process if the message level is less than or equal to the configured level
|
||||
if (message_level <= log_level) {
|
||||
// Convert log level to string
|
||||
std::string level_str;
|
||||
switch (message_level) {
|
||||
case LoggingLevels::FATAL:
|
||||
level_str = "FATAL";
|
||||
break;
|
||||
case LoggingLevels::ERROR:
|
||||
level_str = "ERROR";
|
||||
break;
|
||||
case LoggingLevels::WARN:
|
||||
level_str = "WARN";
|
||||
break;
|
||||
case LoggingLevels::INFO:
|
||||
level_str = "INFO";
|
||||
break;
|
||||
case LoggingLevels::DEBUG:
|
||||
level_str = "DEBUG";
|
||||
break;
|
||||
default:
|
||||
level_str = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
std::string level_str = LogLevelToString(message_level);
|
||||
|
||||
// Console output without timestamp
|
||||
if (message_level == LoggingLevels::FATAL ||
|
||||
@@ -146,4 +144,4 @@ void Logger::log(LoggingLevels message_level, const std::string& message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,68 +29,6 @@
|
||||
|
||||
#include "module_interface.hpp"
|
||||
|
||||
/**
|
||||
* @brief Function that modules can call to access DPM core functionality
|
||||
*
|
||||
* This function is exported by the DPM executable and can be called by
|
||||
* dynamically loaded modules to access services or features of the core application.
|
||||
*
|
||||
* Note: In order for a module to use this functionality, it requires a forward declaration in the module:
|
||||
* // Example:
|
||||
* // Declaration of the DPM core function we want to call
|
||||
* extern "C" int dpm_core_callback(const char* action, const char* data);
|
||||
*
|
||||
* @param action The specific action or operation being requested
|
||||
* @param data Additional data or parameters needed for the action
|
||||
* @return Result code indicating success (0) or failure (non-zero)
|
||||
*/
|
||||
extern "C" int dpm_core_callback(const char* action, const char* data) {
|
||||
// Implementation here
|
||||
if (!action) {
|
||||
std::cerr << "Error: Module callback received null action" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string action_str(action);
|
||||
|
||||
if (action_str == "log") {
|
||||
if (data) {
|
||||
std::cout << "Module log: " << data << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (action_str == "get_config") {
|
||||
if (!data) {
|
||||
std::cerr << "Error: get_config requires section.key format" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parse the section.key format
|
||||
std::string data_str(data);
|
||||
size_t dot_pos = data_str.find('.');
|
||||
|
||||
if (dot_pos == std::string::npos) {
|
||||
std::cerr << "Error: Invalid config format. Use section.key" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string section = data_str.substr(0, dot_pos);
|
||||
std::string key = data_str.substr(dot_pos + 1);
|
||||
|
||||
// Get the configuration value
|
||||
const char* value = g_config_manager.getConfigValue(section.c_str(), key.c_str());
|
||||
|
||||
// Log the request for debugging purposes
|
||||
std::cout << "Module requested config: " << data << " = "
|
||||
<< (value ? value : "not found") << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cerr << "Error: Unknown module callback action: " << action_str << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function that provides direct access to configuration values
|
||||
*
|
||||
@@ -104,3 +42,42 @@ extern "C" int dpm_core_callback(const char* action, const char* data) {
|
||||
extern "C" const char* dpm_get_config(const char* section, const char* key) {
|
||||
return g_config_manager.getConfigValue(section, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct logging function for modules
|
||||
*
|
||||
* This function allows modules to log messages directly through the DPM logger.
|
||||
*
|
||||
* @param level The log level as an integer (0=FATAL, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG)
|
||||
* @param message The message to log
|
||||
*/
|
||||
extern "C" void dpm_log(int level, const char* message) {
|
||||
if (!message) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert integer level to LoggingLevels enum
|
||||
LoggingLevels log_level;
|
||||
switch (level) {
|
||||
case 0:
|
||||
log_level = LoggingLevels::FATAL;
|
||||
break;
|
||||
case 1:
|
||||
log_level = LoggingLevels::ERROR;
|
||||
break;
|
||||
case 2:
|
||||
log_level = LoggingLevels::WARN;
|
||||
break;
|
||||
case 3:
|
||||
log_level = LoggingLevels::INFO;
|
||||
break;
|
||||
case 4:
|
||||
log_level = LoggingLevels::DEBUG;
|
||||
break;
|
||||
default:
|
||||
log_level = LoggingLevels::INFO;
|
||||
break;
|
||||
}
|
||||
|
||||
g_logger.log(log_level, message);
|
||||
}
|
||||
Reference in New Issue
Block a user