fixed some issues with logging paths
parent
c27d91a573
commit
b8ee0f9eff
|
@ -1,4 +1,4 @@
|
|||
[logging]
|
||||
log_file = /var/log/dpm.log
|
||||
log_file = /home/phanes/dpm.log
|
||||
write_to_log = true
|
||||
log_level = INFO
|
|
@ -1,4 +1,3 @@
|
|||
// Logger.cpp
|
||||
#include "Logger.hpp"
|
||||
|
||||
// Global logger instance
|
||||
|
@ -28,12 +27,16 @@ void Logger::setLogFile(const std::string& new_log_file)
|
|||
if (!log_dir.empty() && !std::filesystem::exists(log_dir)) {
|
||||
try {
|
||||
if (!std::filesystem::create_directories(log_dir)) {
|
||||
std::cerr << "FATAL: Failed to create log directory: " << log_dir.string() << std::endl;
|
||||
exit(1);
|
||||
std::cerr << "Warning: Failed to create log directory: " << log_dir.string() << std::endl;
|
||||
// Continue execution, just disable file logging
|
||||
log_to_file = false;
|
||||
return;
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "FATAL: Error creating log directory: " << e.what() << std::endl;
|
||||
exit(1);
|
||||
std::cerr << "Warning: Error creating log directory: " << e.what() << std::endl;
|
||||
// Continue execution, just disable file logging
|
||||
log_to_file = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,13 +44,17 @@ void Logger::setLogFile(const std::string& new_log_file)
|
|||
try {
|
||||
std::ofstream test_log_file(log_file, std::ios::app);
|
||||
if (!test_log_file.is_open()) {
|
||||
std::cerr << "FATAL: Cannot open log file for writing: " << log_file << std::endl;
|
||||
exit(1);
|
||||
std::cerr << "Warning: Cannot open log file for writing: " << log_file << std::endl;
|
||||
// Continue execution, just disable file logging
|
||||
log_to_file = false;
|
||||
return;
|
||||
}
|
||||
test_log_file.close();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "FATAL: Error validating log file access: " << e.what() << std::endl;
|
||||
exit(1);
|
||||
std::cerr << "Warning: Error validating log file access: " << e.what() << std::endl;
|
||||
// Continue execution, just disable file logging
|
||||
log_to_file = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,11 +62,6 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::setLogLevel(LoggingLevels new_log_level)
|
||||
|
@ -137,10 +139,20 @@ void Logger::log(LoggingLevels message_level, const std::string& message)
|
|||
log_stream << formatted_message << std::endl;
|
||||
log_stream.close();
|
||||
} else {
|
||||
std::cerr << "Failed to write to log file: " << log_file << std::endl;
|
||||
// Just log to console, don't error out the program just for log file issues
|
||||
if (message_level != LoggingLevels::FATAL && message_level != LoggingLevels::ERROR) {
|
||||
std::cerr << "Warning: Failed to write to log file: " << log_file << std::endl;
|
||||
}
|
||||
// Disable file logging for future messages
|
||||
log_to_file = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error writing to log file: " << e.what() << std::endl;
|
||||
// Just log to console, don't error out the program just for log file issues
|
||||
if (message_level != LoggingLevels::FATAL && message_level != LoggingLevels::ERROR) {
|
||||
std::cerr << "Warning: Error writing to log file: " << e.what() << std::endl;
|
||||
}
|
||||
// Disable file logging for future messages
|
||||
log_to_file = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
68
src/dpm.cpp
68
src/dpm.cpp
|
@ -80,7 +80,8 @@ int main( int argc, char* argv[] )
|
|||
}
|
||||
|
||||
// Load configuration files
|
||||
if ( !g_config_manager.loadConfigurations() )
|
||||
bool config_loaded = g_config_manager.loadConfigurations();
|
||||
if (!config_loaded)
|
||||
{
|
||||
// failed to load any configuration files, so alert the user
|
||||
std::cerr << "Warning: No configuration files present or loaded from '"
|
||||
|
@ -101,17 +102,58 @@ int main( int argc, char* argv[] )
|
|||
g_logger.setWriteToLog(config_write_to_log);
|
||||
g_logger.setLogFile(config_log_file);
|
||||
|
||||
// If help is requested, show it and exit - handle this early before any logging is needed
|
||||
if (args.show_help) {
|
||||
return main_show_help();
|
||||
}
|
||||
|
||||
// If list modules is requested, handle it early too
|
||||
if (args.list_modules) {
|
||||
// Determine the module path (CLI arg > config > default)
|
||||
std::string module_path;
|
||||
|
||||
// If CLI argument was provided, use it
|
||||
if (!args.module_path.empty())
|
||||
{
|
||||
module_path = args.module_path;
|
||||
} else {
|
||||
// Otherwise, check configuration file
|
||||
const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path");
|
||||
if (config_module_path)
|
||||
{
|
||||
module_path = config_module_path;
|
||||
} else {
|
||||
// use default if nothing else is available
|
||||
module_path = DPMDefaults::MODULE_PATH;
|
||||
}
|
||||
}
|
||||
|
||||
// create a module loader object with the determined path
|
||||
ModuleLoader loader(module_path);
|
||||
|
||||
// check the module path for the loader object
|
||||
int path_check_result = main_check_module_path(loader);
|
||||
if (path_check_result != 0)
|
||||
{
|
||||
// exit if there's an error and ensure
|
||||
// it has an appropriate return code
|
||||
return path_check_result;
|
||||
}
|
||||
|
||||
return main_list_modules(loader);
|
||||
}
|
||||
|
||||
// Determine the module path (CLI arg > config > default)
|
||||
std::string module_path;
|
||||
|
||||
// If CLI argument was provided, use it
|
||||
if ( !args.module_path.empty() )
|
||||
if (!args.module_path.empty())
|
||||
{
|
||||
module_path = args.module_path;
|
||||
} else {
|
||||
// Otherwise, check configuration file
|
||||
const char * config_module_path = g_config_manager.getConfigValue( "modules", "module_path" );
|
||||
if ( config_module_path )
|
||||
const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path");
|
||||
if (config_module_path)
|
||||
{
|
||||
module_path = config_module_path;
|
||||
} else {
|
||||
|
@ -121,27 +163,17 @@ int main( int argc, char* argv[] )
|
|||
}
|
||||
|
||||
// create a module loader object with the determined path
|
||||
ModuleLoader loader( module_path );
|
||||
ModuleLoader loader(module_path);
|
||||
|
||||
// check the module path for the loader object
|
||||
int path_check_result = main_check_module_path( loader );
|
||||
if ( path_check_result != 0 )
|
||||
int path_check_result = main_check_module_path(loader);
|
||||
if (path_check_result != 0)
|
||||
{
|
||||
// exit if there's an error and ensure
|
||||
// it has an appropriate return code
|
||||
return path_check_result;
|
||||
}
|
||||
|
||||
// If help is requested, show it and exit
|
||||
if (args.show_help) {
|
||||
return main_show_help();
|
||||
}
|
||||
|
||||
// If list modules is requested, show the list and exit
|
||||
if (args.list_modules) {
|
||||
return main_list_modules(loader);
|
||||
}
|
||||
|
||||
// if no module is provided to execute, then trigger the default
|
||||
// behaviour (show help)
|
||||
if (args.module_name.empty()) {
|
||||
|
@ -149,7 +181,7 @@ int main( int argc, char* argv[] )
|
|||
}
|
||||
|
||||
// execute the module
|
||||
int return_code = main_execute_module( loader, args.module_name, args.command );
|
||||
int return_code = main_execute_module(loader, args.module_name, args.command);
|
||||
|
||||
return return_code;
|
||||
}
|
Loading…
Reference in New Issue