RMM API 2.0.0
Loading...
Searching...
No Matches
RMMLog.h
1#pragma once
2
3#include <sys/stat.h>
4
5#include <atomic>
6#include <chrono>
7#include <cstdarg>
8#include <cstring>
9#include <fstream>
10#include <iostream>
11#include <map>
12#include <mutex>
13#include <thread>
14#include <vector>
15
16#include "RMMParams.h"
17#include "RMMUtils.h"
18
19#define MAX_CHAR_LOG_MESSAGE 1024
20#define TIMESTAMP_SIZE 32
21#define SEPARATORS_SIZE 5
22#define MAX_FINAL_MESSAGE_SIZE (MAX_CHAR_LOG_MESSAGE + TIMESTAMP_SIZE + SEPARATORS_SIZE)
23
24#define RMMLOG_DEBUG_MASK 1
25#define RMMLOG_INFO_MASK 2
26#define RMMLOG_WARN_MASK 4
27#define RMMLOG_ERROR_MASK 8
28#define RMMLOG_CRITICAL_MASK 16
29
33enum class LogMask : uint8_t { INFO, DEBUG, WARN, ERROR, CRITICAL };
34
39struct LogMessage {
40 const char *sender;
41 const char *message;
42 int mask;
43};
44
52class RMMLog {
53 public:
54 RMMLog(const char *name);
55 ~RMMLog();
56
57 void debug(const char *sender, const char *printer...);
58 void info(const char *sender, const char *printer...);
59 void warn(const char *sender, const char *printer...);
60 void error(const char *sender, const char *printer...);
61 void critical(const char *sender, const char *printer...);
62 void logRelay(std::string logLevel, FILE *out);
63 void getTimestamp(char message[MAX_CHAR_LOG_MESSAGE]);
64 void getLogMask(int *mask);
65 void getLogMask(char *mask);
66 rmmStatus setLogMask(int mask);
67 void setLogTimeout(int milliseconds);
68 void setFileWriteTimeout(int milliseconds);
69 int getFailedWrites();
70 rmmStatus logMsg(LogMask mask, const char *sender, const char *message...);
71 rmmStatus putMsg(std::string msg);
72 rmmStatus putInfo(const char *sender, std::string msg);
73 rmmStatus putMsg(const char *message...);
74 rmmStatus putInfo(const char *sender, const char *msg...);
75 rmmStatus getMsg(std::string *msg);
76 rmmStatus getLogFilePath(std::string *filePath);
77 rmmStatus getLogFilePath(std::string *path, std::string *name);
78 rmmStatus setLogFilePath(std::string filePath, bool useName = false);
79 rmmStatus setLogFilePath(std::string path, std::string name);
80 rmmStatus setWriteToFile(bool option, char mode = 'w');
81
82 private:
83 void log(FILE *out, const char *msg);
84 // Used to define which log will print which message.
85 FILE *debug_log = NULL;
86 FILE *info_log = NULL;
87 FILE *warn_log = NULL;
88 FILE *err_log = NULL;
89 FILE *crit_log = NULL;
90 std::string file_name;
91 std::string name;
92
93 std::string message;
94 std::atomic_uint failedFileWrites = {0};
95 std::atomic_uint failedLogs = {0};
96 std::ofstream logFile;
97 std::chrono::milliseconds max_log_timeout_ms = std::chrono::milliseconds(10);
98 std::chrono::milliseconds max_file_write_timeout_ms = std::chrono::milliseconds(10);
99 int log_mask = 28; // Default is Warn, Error and Critical. Can be changed dynamically.
100 std::atomic_bool writeToFile = {false};
101};
rmmStatus putInfo(const char *sender, std::string msg)
Calls both putMsg and info methods.
Definition RMMLog.cpp:505
void logRelay(std::string logLevel, FILE *out)
Relays log output to a specified file for a given log level.
Definition RMMLog.cpp:137
rmmStatus logMsg(LogMask mask, const char *sender, const char *message...)
Store message in a buffer and put on relative type of log.
Definition RMMLog.cpp:269
rmmStatus setLogFilePath(std::string filePath, bool useName=false)
Sets the log file path for the log file.
Definition RMMLog.cpp:395
void error(const char *sender, const char *printer...)
Logs an error message.
Definition RMMLog.cpp:89
void setFileWriteTimeout(int milliseconds)
Sets the timeout for file write operations.
Definition RMMLog.cpp:256
rmmStatus getMsg(std::string *msg)
Get the message stored in object container.
Definition RMMLog.cpp:348
RMMLog(const char *name)
Constructs the RMMLog object with a name for the log.
Definition RMMLog.cpp:30
void critical(const char *sender, const char *printer...)
Logs a critical message.
Definition RMMLog.cpp:104
void info(const char *sender, const char *printer...)
Logs an info message.
Definition RMMLog.cpp:59
rmmStatus getLogFilePath(std::string *filePath)
Get filepath to log.
Definition RMMLog.cpp:359
rmmStatus setLogMask(int mask)
Sets the log mask to the given value.
Definition RMMLog.cpp:227
void debug(const char *sender, const char *printer...)
Logs a debug message.
Definition RMMLog.cpp:44
void getLogMask(int *mask)
Gets the current log mask.
Definition RMMLog.cpp:182
int getFailedWrites()
Gets the count of failed write attempts to the log file.
Definition RMMLog.cpp:242
rmmStatus setWriteToFile(bool option, char mode='w')
Enable/disable writing to log.
Definition RMMLog.cpp:450
void getTimestamp(char message[MAX_CHAR_LOG_MESSAGE])
Retrieves the current timestamp in a formatted string.
Definition RMMLog.cpp:159
~RMMLog()
Destructor for the RMMLog object.
Definition RMMLog.cpp:36
rmmStatus putMsg(std::string msg)
Store message in a buffer.
Definition RMMLog.cpp:332
void setLogTimeout(int milliseconds)
Sets the timeout for logging operations.
Definition RMMLog.cpp:249
void warn(const char *sender, const char *printer...)
Logs a warning message.
Definition RMMLog.cpp:74
Struct representing a log message with sender, message, and log mask.
Definition RMMLog.h:39
int mask
Log mask associated with the message.
Definition RMMLog.h:42
const char * sender
Sender of the log message.
Definition RMMLog.h:40
const char * message
The actual log message.
Definition RMMLog.h:41