From 34bab86b69bb844fb0de084a377e3ccb8812806a Mon Sep 17 00:00:00 2001 From: Chris Punches Date: Fri, 7 Mar 2025 23:53:31 -0500 Subject: [PATCH] fixed logging pattern --- include/Logger.hpp | 4 +- include/LoggingLevels.hpp | 18 ++++--- include/module_interface.hpp | 9 ++-- modules/info.cpp | 76 +++++++++++++++++++------- src/Logger.cpp | 52 +++++++++--------- src/module_interface.cpp | 101 ++++++++++++++--------------------- 6 files changed, 138 insertions(+), 122 deletions(-) diff --git a/include/Logger.hpp b/include/Logger.hpp index 84587d8..dae9d15 100644 --- a/include/Logger.hpp +++ b/include/Logger.hpp @@ -1,4 +1,3 @@ -// Logger.hpp #pragma once #include @@ -31,6 +30,9 @@ public: // String to LoggingLevels conversion static LoggingLevels stringToLogLevel(const std::string& level_str, LoggingLevels default_level = LoggingLevels::INFO); + // Convert LoggingLevels enum to string + static std::string LogLevelToString(LoggingLevels level); + private: // the logging level to stay initialized to LoggingLevels log_level; diff --git a/include/LoggingLevels.hpp b/include/LoggingLevels.hpp index c9e8a60..799ee86 100644 --- a/include/LoggingLevels.hpp +++ b/include/LoggingLevels.hpp @@ -1,10 +1,12 @@ #pragma once -// Log level enum that will be accessible to modules -enum LoggingLevels { - FATAL = 0, - ERROR = 1, - WARN = 2, - INFO = 3, - DEBUG = 4 -}; +extern "C" { + // Log level enum that will be accessible to modules + enum LoggingLevels { + FATAL = 0, + ERROR = 1, + WARN = 2, + INFO = 3, + DEBUG = 4 + }; +} \ No newline at end of file diff --git a/include/module_interface.hpp b/include/module_interface.hpp index df674e1..6df23dc 100644 --- a/include/module_interface.hpp +++ b/include/module_interface.hpp @@ -35,7 +35,8 @@ #include #include "ConfigManager.hpp" - +#include "LoggingLevels.hpp" +#include "Logger.hpp" /* * Provides reserved symbol names we look for in modules. @@ -62,9 +63,9 @@ extern "C" { // Module description information const char* dpm_get_description(void); - // Callback function exposed by DPM core for modules to use - int dpm_core_callback(const char* action, const char* data); - // Direct configuration access function const char* dpm_get_config(const char* section, const char* key); + + // Direct logging function + void dpm_log(int level, const char* message); } diff --git a/modules/info.cpp b/modules/info.cpp index e252e87..021330a 100644 --- a/modules/info.cpp +++ b/modules/info.cpp @@ -28,7 +28,9 @@ * mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors */ -#include +// Implementation of the info module +// This module provides information about the DPM system + #include #include #include @@ -39,12 +41,19 @@ #define MODULE_VERSION "0.1.0" #define DPM_VERSION "0.1.0" +// Define constants for logging levels +// These must match. +const int LOG_FATAL = 0; +const int LOG_ERROR = 1; +const int LOG_WARN = 2; +const int LOG_INFO = 3; +const int LOG_DEBUG = 4; // Declaration of the DPM config function we want to call extern "C" const char* dpm_get_config(const char* section, const char* key); -// Implementation of the info module -// This module provides information about the DPM system +// Declaration of the DPM log function +extern "C" void dpm_log(int level, const char* message); // Version information extern "C" const char* dpm_module_get_version(void) { @@ -70,6 +79,7 @@ std::string detect_architecture() { struct utsname system_info; if (uname(&system_info) == -1) { + dpm_log(LOG_ERROR, "Failed to detect system architecture"); return "Unknown"; } @@ -81,6 +91,7 @@ std::string detect_os() { struct utsname system_info; if (uname(&system_info) == -1) { + dpm_log(LOG_ERROR, "Failed to detect operating system"); return "Unknown"; } @@ -126,39 +137,62 @@ std::string detect_os() { // Command handler functions int cmd_help(int argc, char** argv) { - std::cout << "DPM Info Module - Provides information about the DPM system\n\n"; - std::cout << "Available commands:\n\n"; - std::cout << " version - Display DPM version information\n"; - std::cout << " system - Display system information\n"; - std::cout << " config - Display configuration information\n"; - std::cout << " help - Display this help message\n"; + dpm_log(LOG_INFO, "DPM Info Module - Provides information about the DPM system"); + dpm_log(LOG_INFO, "Available commands:"); + dpm_log(LOG_INFO, " version - Display DPM version information"); + dpm_log(LOG_INFO, " system - Display system information"); + dpm_log(LOG_INFO, " config - Display configuration information"); + dpm_log(LOG_INFO, " help - Display this help message"); return 0; } int cmd_version(int argc, char** argv) { - std::cout << "DPM Version: " << DPM_VERSION << "\n"; - std::cout << "Build Date: " << __DATE__ << "\n"; - std::cout << "Build Time: " << __TIME__ << "\n"; + std::string version_msg = "DPM Version: "; + version_msg += DPM_VERSION; + dpm_log(LOG_INFO, version_msg.c_str()); + + std::string date_msg = "Build Date: "; + date_msg += __DATE__; + dpm_log(LOG_INFO, date_msg.c_str()); + + std::string time_msg = "Build Time: "; + time_msg += __TIME__; + dpm_log(LOG_INFO, time_msg.c_str()); + return 0; } int cmd_system(int argc, char** argv) { - std::cout << "System Information:\n"; - std::cout << " OS: " << detect_os() << "\n"; - std::cout << " Architecture: " << detect_architecture() << "\n"; + dpm_log(LOG_INFO, "System Information:"); + + std::string os_msg = " OS: "; + os_msg += detect_os(); + dpm_log(LOG_INFO, os_msg.c_str()); + + std::string arch_msg = " Architecture: "; + arch_msg += detect_architecture(); + dpm_log(LOG_INFO, arch_msg.c_str()); + return 0; } int cmd_config(int argc, char** argv) { const char* module_path = dpm_get_config("modules", "module_path"); - std::cout << "Configuration Information:\n"; - std::cout << " Module Path: " << (module_path ? module_path : "Not configured") << "\n"; + + dpm_log(LOG_INFO, "Configuration Information:"); + + std::string path_msg = " Module Path: "; + path_msg += (module_path ? module_path : "Not configured"); + dpm_log(LOG_INFO, path_msg.c_str()); + return 0; } int cmd_unknown(const char* command, int argc, char** argv) { - std::cerr << "Unknown command: " << (command ? command : "") << "\n"; - std::cerr << "Run 'dpm info help' for a list of available commands\n"; + std::string msg = "Unknown command: "; + msg += (command ? command : ""); + dpm_log(LOG_WARN, msg.c_str()); + dpm_log(LOG_WARN, "Run 'dpm info help' for a list of available commands"); return 1; } @@ -186,6 +220,8 @@ Command parse_command(const char* cmd_str) { // Main entry point that will be called by DPM extern "C" int dpm_module_execute(const char* command, int argc, char** argv) { + dpm_log(LOG_INFO, "Info module execution started"); + Command cmd = parse_command(command); switch (cmd) { @@ -205,4 +241,4 @@ extern "C" int dpm_module_execute(const char* command, int argc, char** argv) { default: return cmd_unknown(command, argc, argv); } -} \ No newline at end of file +} diff --git a/src/Logger.cpp b/src/Logger.cpp index a1cad88..f938350 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -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) } } } -} +} \ No newline at end of file diff --git a/src/module_interface.cpp b/src/module_interface.cpp index c6548e2..6206906 100644 --- a/src/module_interface.cpp +++ b/src/module_interface.cpp @@ -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); +} \ No newline at end of file