fixed some issues with logging paths
parent
c27d91a573
commit
b8ee0f9eff
|
@ -1,4 +1,4 @@
|
||||||
[logging]
|
[logging]
|
||||||
log_file = /var/log/dpm.log
|
log_file = /home/phanes/dpm.log
|
||||||
write_to_log = true
|
write_to_log = true
|
||||||
log_level = INFO
|
log_level = INFO
|
|
@ -1,4 +1,3 @@
|
||||||
// Logger.cpp
|
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
|
||||||
// Global logger instance
|
// 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)) {
|
if (!log_dir.empty() && !std::filesystem::exists(log_dir)) {
|
||||||
try {
|
try {
|
||||||
if (!std::filesystem::create_directories(log_dir)) {
|
if (!std::filesystem::create_directories(log_dir)) {
|
||||||
std::cerr << "FATAL: Failed to create log directory: " << log_dir.string() << std::endl;
|
std::cerr << "Warning: Failed to create log directory: " << log_dir.string() << std::endl;
|
||||||
exit(1);
|
// Continue execution, just disable file logging
|
||||||
|
log_to_file = false;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} catch (const std::filesystem::filesystem_error& e) {
|
} catch (const std::filesystem::filesystem_error& e) {
|
||||||
std::cerr << "FATAL: Error creating log directory: " << e.what() << std::endl;
|
std::cerr << "Warning: Error creating log directory: " << e.what() << std::endl;
|
||||||
exit(1);
|
// 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 {
|
try {
|
||||||
std::ofstream test_log_file(log_file, std::ios::app);
|
std::ofstream test_log_file(log_file, std::ios::app);
|
||||||
if (!test_log_file.is_open()) {
|
if (!test_log_file.is_open()) {
|
||||||
std::cerr << "FATAL: Cannot open log file for writing: " << log_file << std::endl;
|
std::cerr << "Warning: Cannot open log file for writing: " << log_file << std::endl;
|
||||||
exit(1);
|
// Continue execution, just disable file logging
|
||||||
|
log_to_file = false;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
test_log_file.close();
|
test_log_file.close();
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << "FATAL: Error validating log file access: " << e.what() << std::endl;
|
std::cerr << "Warning: Error validating log file access: " << e.what() << std::endl;
|
||||||
exit(1);
|
// 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)
|
void Logger::setWriteToLog(bool new_write_to_log)
|
||||||
{
|
{
|
||||||
log_to_file = 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)
|
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 << formatted_message << std::endl;
|
||||||
log_stream.close();
|
log_stream.close();
|
||||||
} else {
|
} 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) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
60
src/dpm.cpp
60
src/dpm.cpp
|
@ -80,7 +80,8 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load configuration files
|
// 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
|
// failed to load any configuration files, so alert the user
|
||||||
std::cerr << "Warning: No configuration files present or loaded from '"
|
std::cerr << "Warning: No configuration files present or loaded from '"
|
||||||
|
@ -101,17 +102,24 @@ int main( int argc, char* argv[] )
|
||||||
g_logger.setWriteToLog(config_write_to_log);
|
g_logger.setWriteToLog(config_write_to_log);
|
||||||
g_logger.setLogFile(config_log_file);
|
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)
|
// Determine the module path (CLI arg > config > default)
|
||||||
std::string module_path;
|
std::string module_path;
|
||||||
|
|
||||||
// If CLI argument was provided, use it
|
// If CLI argument was provided, use it
|
||||||
if ( !args.module_path.empty() )
|
if (!args.module_path.empty())
|
||||||
{
|
{
|
||||||
module_path = args.module_path;
|
module_path = args.module_path;
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, check configuration file
|
// Otherwise, check configuration file
|
||||||
const char * config_module_path = g_config_manager.getConfigValue( "modules", "module_path" );
|
const char* config_module_path = g_config_manager.getConfigValue("modules", "module_path");
|
||||||
if ( config_module_path )
|
if (config_module_path)
|
||||||
{
|
{
|
||||||
module_path = config_module_path;
|
module_path = config_module_path;
|
||||||
} else {
|
} else {
|
||||||
|
@ -121,25 +129,49 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a module loader object with the determined path
|
// 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
|
// check the module path for the loader object
|
||||||
int path_check_result = main_check_module_path( loader );
|
int path_check_result = main_check_module_path(loader);
|
||||||
if ( path_check_result != 0 )
|
if (path_check_result != 0)
|
||||||
{
|
{
|
||||||
// exit if there's an error and ensure
|
// exit if there's an error and ensure
|
||||||
// it has an appropriate return code
|
// it has an appropriate return code
|
||||||
return path_check_result;
|
return path_check_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If help is requested, show it and exit
|
return main_list_modules(loader);
|
||||||
if (args.show_help) {
|
|
||||||
return main_show_help();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If list modules is requested, show the list and exit
|
// Determine the module path (CLI arg > config > default)
|
||||||
if (args.list_modules) {
|
std::string module_path;
|
||||||
return main_list_modules(loader);
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no module is provided to execute, then trigger the default
|
// if no module is provided to execute, then trigger the default
|
||||||
|
@ -149,7 +181,7 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute the module
|
// 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;
|
return return_code;
|
||||||
}
|
}
|
Loading…
Reference in New Issue