Better defaults management, logger class implementation with logging levels, and write to file configuration implementation

This commit is contained in:
Chris Punches
2025-03-03 03:56:51 -05:00
parent e79fa3b89f
commit ee1df1fb0c
14 changed files with 389 additions and 67 deletions

View File

@@ -48,7 +48,13 @@
class ConfigManager {
public:
// Constructor
ConfigManager( const std::string& config_dir = DPMDefaultPaths::CONFIG_DIR );
ConfigManager();
// Set the configuration directory
void setConfigDir(const std::string& config_dir);
// Get the current configuration directory
std::string getConfigDir() const;
// Load all configuration files from the config directory
bool loadConfigurations();

52
include/DPMDefaults.hpp Normal file
View File

@@ -0,0 +1,52 @@
/**
* @file DPMDefaults.hpp
* @brief Default configuration values for the DPM utility
*
* Defines the DPMDefaults structure which provides default configuration values
* for paths, logging settings, and other system-wide defaults used by the DPM
* utility when explicit configuration is not provided.
*
* @copyright Copyright (c) 2025 SILO GROUP LLC
* @author Chris Punches <chris.punches@silogroup.org>
*
* Part of the Dark Horse Linux Package Manager (DPM)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* For bug reports or contributions, please contact the dhlp-contributors
* mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors
*/
#pragma once
#include <string>
#include "LoggingLevels.hpp"
// default system configuration
struct DPMDefaults {
static const char* const MODULE_PATH;
static const char* const CONFIG_DIR;
static const char* const LOG_FILE;
static const bool write_to_log;
static const LoggingLevels LOG_LEVEL;
};
// Initialize static constants
inline const char * const DPMDefaults::MODULE_PATH = "/usr/lib/dpm/modules/";
inline const char * const DPMDefaults::CONFIG_DIR = "/etc/dpm/conf.d/";
inline const char * const DPMDefaults::LOG_FILE = "/var/log/dpm.log";
inline const bool DPMDefaults::write_to_log = false;
inline const LoggingLevels DPMDefaults::LOG_LEVEL = LoggingLevels::INFO;

46
include/Logger.hpp Normal file
View File

@@ -0,0 +1,46 @@
// Logger.hpp
#pragma once
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <filesystem>
#include <stdexcept>
#include <cstdlib>
#include "LoggingLevels.hpp"
#include "DPMDefaults.hpp"
class Logger {
public:
// constructor
Logger();
// destructor
~Logger();
// Log method that accepts a string
void log(LoggingLevels log_level, const std::string& message);
// Configuration setters
void setLogFile(const std::string& log_file);
void setWriteToLog(bool write_to_log);
void setLogLevel(LoggingLevels log_level);
// String to LoggingLevels conversion
static LoggingLevels stringToLogLevel(const std::string& level_str, LoggingLevels default_level = LoggingLevels::INFO);
private:
// the logging level to stay initialized to
LoggingLevels log_level;
// whether or not to log to file
bool log_to_file;
// log file path
std::string log_file;
};
// Global logger instance
extern Logger g_logger;

10
include/LoggingLevels.hpp Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
// Log level enum that will be accessible to modules
enum LoggingLevels {
FATAL = 0,
ERROR = 1,
WARN = 2,
INFO = 3,
DEBUG = 4
};

View File

@@ -39,6 +39,7 @@
#include "error.hpp"
#include "ModuleLoader.hpp"
#include "dpm_interface_helpers.hpp"
#include "Logger.hpp"
/*
*

View File

@@ -1,5 +1,5 @@
/**
* @file dpm_interface_helpers.hpp
* @file dpm_interface_helpers.hpp
* @brief Helper functions for DPM command-line interface
*
* Provides utility functions for command-line argument parsing and
@@ -34,18 +34,17 @@
#include <iostream>
#include <getopt.h>
#include "Logger.hpp"
#include "LoggingLevels.hpp"
#include "DPMDefaults.hpp"
// data structure for supplied arguments
struct CommandArgs {
std::string module_path;
std::string config_dir;
std::string module_name;
std::string command; // All arguments combined into a single command string
std::string command;
};
// parse dpm cli arguments into a serialized structure
CommandArgs parse_args( int argc, char * argv[] );
// default system paths
struct DPMDefaultPaths {
static const std::string MODULE_PATH;
static const std::string CONFIG_DIR;
};