fixed logging pattern
parent
ee1df1fb0c
commit
34bab86b69
|
@ -1,4 +1,3 @@
|
||||||
// Logger.hpp
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -31,6 +30,9 @@ public:
|
||||||
// String to LoggingLevels conversion
|
// String to LoggingLevels conversion
|
||||||
static LoggingLevels stringToLogLevel(const std::string& level_str, LoggingLevels default_level = LoggingLevels::INFO);
|
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:
|
private:
|
||||||
// the logging level to stay initialized to
|
// the logging level to stay initialized to
|
||||||
LoggingLevels log_level;
|
LoggingLevels log_level;
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// Log level enum that will be accessible to modules
|
extern "C" {
|
||||||
enum LoggingLevels {
|
// Log level enum that will be accessible to modules
|
||||||
|
enum LoggingLevels {
|
||||||
FATAL = 0,
|
FATAL = 0,
|
||||||
ERROR = 1,
|
ERROR = 1,
|
||||||
WARN = 2,
|
WARN = 2,
|
||||||
INFO = 3,
|
INFO = 3,
|
||||||
DEBUG = 4
|
DEBUG = 4
|
||||||
};
|
};
|
||||||
|
}
|
|
@ -35,7 +35,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "ConfigManager.hpp"
|
#include "ConfigManager.hpp"
|
||||||
|
#include "LoggingLevels.hpp"
|
||||||
|
#include "Logger.hpp"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Provides reserved symbol names we look for in modules.
|
* Provides reserved symbol names we look for in modules.
|
||||||
|
@ -62,9 +63,9 @@ extern "C" {
|
||||||
// Module description information
|
// Module description information
|
||||||
const char* dpm_get_description(void);
|
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
|
// Direct configuration access function
|
||||||
const char* dpm_get_config(const char* section, const char* key);
|
const char* dpm_get_config(const char* section, const char* key);
|
||||||
|
|
||||||
|
// Direct logging function
|
||||||
|
void dpm_log(int level, const char* message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,9 @@
|
||||||
* mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors
|
* mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
// Implementation of the info module
|
||||||
|
// This module provides information about the DPM system
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -39,12 +41,19 @@
|
||||||
#define MODULE_VERSION "0.1.0"
|
#define MODULE_VERSION "0.1.0"
|
||||||
#define DPM_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
|
// Declaration of the DPM config function we want to call
|
||||||
extern "C" const char* dpm_get_config(const char* section, const char* key);
|
extern "C" const char* dpm_get_config(const char* section, const char* key);
|
||||||
|
|
||||||
// Implementation of the info module
|
// Declaration of the DPM log function
|
||||||
// This module provides information about the DPM system
|
extern "C" void dpm_log(int level, const char* message);
|
||||||
|
|
||||||
// Version information
|
// Version information
|
||||||
extern "C" const char* dpm_module_get_version(void) {
|
extern "C" const char* dpm_module_get_version(void) {
|
||||||
|
@ -70,6 +79,7 @@ std::string detect_architecture() {
|
||||||
struct utsname system_info;
|
struct utsname system_info;
|
||||||
|
|
||||||
if (uname(&system_info) == -1) {
|
if (uname(&system_info) == -1) {
|
||||||
|
dpm_log(LOG_ERROR, "Failed to detect system architecture");
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +91,7 @@ std::string detect_os() {
|
||||||
struct utsname system_info;
|
struct utsname system_info;
|
||||||
|
|
||||||
if (uname(&system_info) == -1) {
|
if (uname(&system_info) == -1) {
|
||||||
|
dpm_log(LOG_ERROR, "Failed to detect operating system");
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,39 +137,62 @@ std::string detect_os() {
|
||||||
|
|
||||||
// Command handler functions
|
// Command handler functions
|
||||||
int cmd_help(int argc, char** argv) {
|
int cmd_help(int argc, char** argv) {
|
||||||
std::cout << "DPM Info Module - Provides information about the DPM system\n\n";
|
dpm_log(LOG_INFO, "DPM Info Module - Provides information about the DPM system");
|
||||||
std::cout << "Available commands:\n\n";
|
dpm_log(LOG_INFO, "Available commands:");
|
||||||
std::cout << " version - Display DPM version information\n";
|
dpm_log(LOG_INFO, " version - Display DPM version information");
|
||||||
std::cout << " system - Display system information\n";
|
dpm_log(LOG_INFO, " system - Display system information");
|
||||||
std::cout << " config - Display configuration information\n";
|
dpm_log(LOG_INFO, " config - Display configuration information");
|
||||||
std::cout << " help - Display this help message\n";
|
dpm_log(LOG_INFO, " help - Display this help message");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_version(int argc, char** argv) {
|
int cmd_version(int argc, char** argv) {
|
||||||
std::cout << "DPM Version: " << DPM_VERSION << "\n";
|
std::string version_msg = "DPM Version: ";
|
||||||
std::cout << "Build Date: " << __DATE__ << "\n";
|
version_msg += DPM_VERSION;
|
||||||
std::cout << "Build Time: " << __TIME__ << "\n";
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_system(int argc, char** argv) {
|
int cmd_system(int argc, char** argv) {
|
||||||
std::cout << "System Information:\n";
|
dpm_log(LOG_INFO, "System Information:");
|
||||||
std::cout << " OS: " << detect_os() << "\n";
|
|
||||||
std::cout << " Architecture: " << detect_architecture() << "\n";
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_config(int argc, char** argv) {
|
int cmd_config(int argc, char** argv) {
|
||||||
const char* module_path = dpm_get_config("modules", "module_path");
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_unknown(const char* command, int argc, char** argv) {
|
int cmd_unknown(const char* command, int argc, char** argv) {
|
||||||
std::cerr << "Unknown command: " << (command ? command : "") << "\n";
|
std::string msg = "Unknown command: ";
|
||||||
std::cerr << "Run 'dpm info help' for a list of available commands\n";
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,6 +220,8 @@ Command parse_command(const char* cmd_str) {
|
||||||
|
|
||||||
// Main entry point that will be called by DPM
|
// Main entry point that will be called by DPM
|
||||||
extern "C" int dpm_module_execute(const char* command, int argc, char** argv) {
|
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);
|
Command cmd = parse_command(command);
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
|
|
|
@ -67,6 +67,24 @@ void Logger::setLogLevel(LoggingLevels new_log_level)
|
||||||
log_level = 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)
|
LoggingLevels Logger::stringToLogLevel(const std::string& level_str, LoggingLevels default_level)
|
||||||
{
|
{
|
||||||
if (level_str == "FATAL") {
|
if (level_str == "FATAL") {
|
||||||
|
@ -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
|
// Only process if the message level is less than or equal to the configured level
|
||||||
if (message_level <= log_level) {
|
if (message_level <= log_level) {
|
||||||
// Convert log level to string
|
// Convert log level to string
|
||||||
std::string level_str;
|
std::string level_str = LogLevelToString(message_level);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Console output without timestamp
|
// Console output without timestamp
|
||||||
if (message_level == LoggingLevels::FATAL ||
|
if (message_level == LoggingLevels::FATAL ||
|
||||||
|
|
|
@ -29,68 +29,6 @@
|
||||||
|
|
||||||
#include "module_interface.hpp"
|
#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
|
* @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) {
|
extern "C" const char* dpm_get_config(const char* section, const char* key) {
|
||||||
return g_config_manager.getConfigValue(section, 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);
|
||||||
|
}
|
Loading…
Reference in New Issue