improved consistency in argument handling, introduction of dpm_con (not everything needs to be in the log)
This commit is contained in:
@@ -156,4 +156,24 @@ void Logger::log(LoggingLevels message_level, const std::string& message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::log_console(LoggingLevels level, const std::string& message)
|
||||
{
|
||||
// Only process if the message level is less than or equal to the configured level
|
||||
if (level <= log_level) {
|
||||
// Convert log level to string
|
||||
std::string level_str = LogLevelToString(level);
|
||||
|
||||
// Console output without timestamp
|
||||
if (level == LoggingLevels::FATAL ||
|
||||
level == LoggingLevels::ERROR ||
|
||||
level == LoggingLevels::WARN) {
|
||||
// Send to stderr
|
||||
std::cerr << level_str << ": " << message << std::endl;
|
||||
} else {
|
||||
// Send to stdout
|
||||
std::cout << message << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,8 @@
|
||||
#include "error.hpp"
|
||||
#include "ConfigManager.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "LoggingLevels.hpp"
|
||||
#include "module_interface.hpp"
|
||||
|
||||
/*
|
||||
* DPM serves three functions:
|
||||
@@ -84,8 +86,7 @@ int main( int argc, char* argv[] )
|
||||
if (!config_loaded)
|
||||
{
|
||||
// failed to load any configuration files, so alert the user
|
||||
std::cerr << "Warning: No configuration files present or loaded from '"
|
||||
<< g_config_manager.getConfigDir() << "*.conf', reverting to defaults." << std::endl;
|
||||
dpm_con( ERROR, ("Warning: No configuration files present or loaded from '" + g_config_manager.getConfigDir() + "*.conf', reverting to defaults.").c_str());
|
||||
}
|
||||
|
||||
// Configure logger (CLI args > config > defaults)
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
|
||||
#include "dpm_interface.hpp"
|
||||
|
||||
#include <handlers.hpp>
|
||||
|
||||
/*
|
||||
* DPM Interface methods.
|
||||
@@ -49,23 +48,23 @@ int main_check_module_path(const ModuleLoader& loader)
|
||||
loader.get_module_path(path);
|
||||
|
||||
if (!std::filesystem::exists(path)) {
|
||||
g_logger.log(LoggingLevels::FATAL, "modules.modules_path does not exist: " + path);
|
||||
dpm_con(FATAL, ("modules.modules_path does not exist: " + path).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!std::filesystem::is_directory(path)) {
|
||||
g_logger.log(LoggingLevels::FATAL, "modules.modules_path is not a directory: " + path);
|
||||
dpm_con(FATAL, ("modules.modules_path is not a directory: " + path).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
auto perms = std::filesystem::status(path).permissions();
|
||||
if ((perms & std::filesystem::perms::owner_read) == std::filesystem::perms::none) {
|
||||
g_logger.log(LoggingLevels::FATAL, "Permission denied: " + path);
|
||||
dpm_con(FATAL, ("Permission denied: " + path).c_str());
|
||||
return 1;
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error&) {
|
||||
g_logger.log(LoggingLevels::FATAL, "Permission denied: " + path);
|
||||
dpm_con(FATAL, ("Permission denied: " + path).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -82,18 +81,18 @@ int main_list_modules(const ModuleLoader& loader) {
|
||||
// set the module path
|
||||
DPMErrorCategory get_path_error = loader.get_module_path(path);
|
||||
if (get_path_error != DPMErrorCategory::SUCCESS) {
|
||||
g_logger.log(LoggingLevels::FATAL, "Failed to get modules.modules_path");
|
||||
dpm_con(LoggingLevels::FATAL, "Failed to get modules.modules_path");
|
||||
return 1;
|
||||
}
|
||||
|
||||
DPMErrorCategory list_error = loader.list_available_modules(modules);
|
||||
if (list_error != DPMErrorCategory::SUCCESS) {
|
||||
g_logger.log(LoggingLevels::FATAL, "No modules found in modules.modules_path: " + path);
|
||||
dpm_con(LoggingLevels::FATAL, ("No modules found in modules.modules_path: " + path).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (modules.empty()) {
|
||||
g_logger.log(LoggingLevels::FATAL, "No modules found in modules.modules_path: '" + path + "'.");
|
||||
dpm_con(LoggingLevels::FATAL, ("No modules found in modules.modules_path: '" + path + "'.").c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -115,7 +114,7 @@ int main_list_modules(const ModuleLoader& loader) {
|
||||
}
|
||||
|
||||
if (valid_modules.empty()) {
|
||||
g_logger.log(LoggingLevels::FATAL, "No valid DPM commands available.");
|
||||
dpm_con(LoggingLevels::FATAL, "No valid DPM commands available.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,12 +30,13 @@
|
||||
|
||||
#include "handlers.hpp"
|
||||
|
||||
// Helper function for validating required fields in a FlexDPMError
|
||||
void validate_field(FlexDPMError context, const char* field_name, const void* field_value)
|
||||
{
|
||||
if (!field_value) {
|
||||
std::cerr << "Error: Incomplete error context. Missing required field: " << field_name;
|
||||
std::cerr << " (Error category: " << static_cast<int>(context.error) << ")" << std::endl;
|
||||
std::string error_msg = "Error category " + std::to_string(static_cast<int>(context.error)) +
|
||||
": Incomplete error context. Missing required field: " + field_name;
|
||||
|
||||
dpm_log(FATAL, error_msg.c_str());
|
||||
|
||||
// Hard exit when a required field is missing
|
||||
exit(1);
|
||||
@@ -100,68 +101,78 @@ int handle_error(FlexDPMError context) {
|
||||
}
|
||||
|
||||
// Now the individual handlers can be simplified since required fields are guaranteed
|
||||
int handle_path_not_found( FlexDPMError context ) {
|
||||
std::cerr << "Fatal error: The module directory '" << context.module_path << "' was not found. Exiting." << std::endl;
|
||||
int handle_path_not_found(FlexDPMError context) {
|
||||
std::string error_msg = "Fatal error: The module directory '" + std::string(context.module_path) + "' was not found. Exiting.";
|
||||
dpm_log(FATAL, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_path_not_directory( FlexDPMError context ) {
|
||||
std::cerr << "Fatal error: The module path '" << context.module_path << "' is not a directory. Exiting." << std::endl;
|
||||
int handle_path_not_directory(FlexDPMError context) {
|
||||
std::string error_msg = "Fatal error: The module path '" + std::string(context.module_path) + "' is not a directory. Exiting.";
|
||||
dpm_log(FATAL, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_path_too_long( FlexDPMError context ) {
|
||||
std::cerr << "Error: Module path is too long: '" << context.module_path << "'. Exiting." << std::endl;
|
||||
int handle_path_too_long(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Module path is too long: '" + std::string(context.module_path) + "'. Exiting.";
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_permission_denied( FlexDPMError context ) {
|
||||
std::cerr << "Error: Permission denied accessing the modules path: '" << context.module_path << "'. Exiting." << std::endl;
|
||||
int handle_permission_denied(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Permission denied accessing the modules path: '" + std::string(context.module_path) + "'. Exiting.";
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_module_not_found( FlexDPMError context ) {
|
||||
std::cerr << "Error: Module '"<< context.module_name << "' not found in '" << context.module_path << "'. Exiting." << std::endl;
|
||||
int handle_module_not_found(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Module '" + std::string(context.module_name) + "' not found in '" + std::string(context.module_path) + "'. Exiting.";
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_module_not_loaded( FlexDPMError context ) {
|
||||
std::cerr << "Error: Attempted to execute module before loading it: " << context.module_name << std::endl;
|
||||
int handle_module_not_loaded(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Attempted to execute module before loading it: " + std::string(context.module_name);
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_module_load_failed( FlexDPMError context ) {
|
||||
std::cerr << "Error: Failed to load module: " << context.module_name << std::endl;
|
||||
int handle_module_load_failed(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Failed to load module: " + std::string(context.module_name);
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_invalid_module( FlexDPMError context ) {
|
||||
std::cerr << "Error: Invalid module format: " << context.module_name << std::endl;
|
||||
int handle_invalid_module(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Invalid module format: " + std::string(context.module_name);
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_symbol_not_found( FlexDPMError context ) {
|
||||
std::cerr << "Error: Symbol not found in module: " << context.module_name;
|
||||
|
||||
int handle_symbol_not_found(FlexDPMError context) {
|
||||
std::string error_msg = "Error: Symbol not found in module: " + std::string(context.module_name);
|
||||
if (context.message) {
|
||||
std::cerr << " (" << context.message << ")";
|
||||
error_msg += " (" + std::string(context.message) + ")";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_symbol_execution_failed(FlexDPMError context) {
|
||||
std::cerr << "Error: Module execution failed: " << context.module_name << std::endl;
|
||||
std::string error_msg = "Error: Module execution failed: " + std::string(context.module_name);
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int handle_undefined_error(FlexDPMError context) {
|
||||
std::cerr << "Error: Undefined error occurred";
|
||||
std::string error_msg = "Error: Undefined error occurred";
|
||||
if (context.module_name) {
|
||||
std::cerr << " with module: " << context.module_name;
|
||||
error_msg += " with module: " + std::string(context.module_name);
|
||||
}
|
||||
if (context.message) {
|
||||
std::cerr << " (" << context.message << ")";
|
||||
error_msg += " (" + std::string(context.message) + ")";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
dpm_log(ERROR, error_msg.c_str());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,37 @@ extern "C" void dpm_log(int level, const char* message) {
|
||||
g_logger.log(log_level, message);
|
||||
}
|
||||
|
||||
extern "C" void dpm_con(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_console(log_level, message);
|
||||
}
|
||||
|
||||
extern "C" void dpm_set_logging_level(int level) {
|
||||
// Convert integer level to LoggingLevels enum
|
||||
LoggingLevels log_level;
|
||||
|
||||
Reference in New Issue
Block a user