rex/src/logger/Logger.h

71 lines
2.2 KiB
C
Raw Normal View History

2021-04-05 01:32:00 +00:00
/*
Rex - A configuration management and workflow automation tool that
compiles and runs in minimal environments.
© SILO GROUP and Chris Punches, 2020.
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/>.
*/
2020-06-21 00:09:32 +00:00
2021-04-05 01:21:05 +00:00
#ifndef REX_LOGGER_H
#define REX_LOGGER_H
2020-06-21 00:09:32 +00:00
#include <string>
#include <iostream>
2025-08-04 09:50:11 +00:00
#include <fstream>
#include <fcntl.h> // For O_RDONLY, O_WRONLY, open()
#include <ctime>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <memory>
#include <cstdarg> // for va_list in FileGuard::printf
2023-02-15 23:44:21 +00:00
#include "../misc/helpers.h"
2020-06-21 00:09:32 +00:00
enum L_LVL {
E_FATAL,
E_WARN,
2020-06-21 00:25:45 +00:00
E_INFO,
2020-06-21 00:09:32 +00:00
E_DEBUG
};
class Logger {
2023-02-15 23:44:21 +00:00
public:
2025-08-04 09:50:11 +00:00
// Constructor and Destructor
Logger(int LOG_LEVEL, std::string mask);
~Logger(); // Added Destructor
// Logging methods
void log(int LOG_LEVEL, std::string msg);
void log_task(int LOG_LEVEL, std::string task_name, std::string msg);
void log_to_json_file(const std::string& log_level, const std::string& message,
const std::string& user, const std::string& group,
const std::string& command, bool log_to_console = true);
// Helper methods
std::string get_current_timestamp();
std::string get_user_name();
std::string get_group_name();
2023-02-15 23:44:21 +00:00
private:
int LOG_LEVEL;
std::string mask;
2025-08-04 09:50:11 +00:00
// Internal helper methods
void create_json_log_entry(char* buffer, size_t buffer_size,
const char* log_level, const char* message,
const char* user, const char* group,
const char* command);
2020-06-21 00:09:32 +00:00
};
2021-04-05 01:21:05 +00:00
#endif //REX_LOGGER_H