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
34struct LogMessage {
35 const char *sender;
36 const char *message;
37 int mask;
38};
39
47class RMMLog {
48 public:
49 RMMLog(const char *name);
50 ~RMMLog();
51
52 void debug(const char *sender, const char *printer...);
53 void info(const char *sender, const char *printer...);
54 void warn(const char *sender, const char *printer...);
55 void error(const char *sender, const char *printer...);
56 void critical(const char *sender, const char *printer...);
57 void logRelay(std::string logLevel, FILE *out);
58 void getTimestamp(char message[MAX_CHAR_LOG_MESSAGE]);
59 void getLogMask(int *mask);
60 void getLogMask(char *mask);
61 rmmStatus setLogMask(int mask);
62 void setLogTimeout(int milliseconds);
63 void setFileWriteTimeout(int milliseconds);
64 int getFailedWrites();
65 rmmStatus putMsg(std::string msg);
66 rmmStatus putInfo(const char *sender, std::string msg);
67 rmmStatus putMsg(const char *message...);
68 rmmStatus putInfo(const char *sender, const char *msg...);
69 rmmStatus getMsg(std::string *msg);
70 rmmStatus getLogFilePath(std::string *filePath);
71 rmmStatus getLogFilePath(std::string *path, std::string *name);
72 rmmStatus setLogFilePath(std::string filePath, bool useName = false);
73 rmmStatus setLogFilePath(std::string path, std::string name);
74 rmmStatus setWriteToFile(bool option, char mode = 'w');
75
76 private:
77 void log(FILE *out, const char *msg);
78 // Used to define which log will print which message.
79 FILE *debug_log = NULL;
80 FILE *info_log = NULL;
81 FILE *warn_log = NULL;
82 FILE *err_log = NULL;
83 FILE *crit_log = NULL;
84 std::string file_name;
85 std::string name;
86
87 std::string message;
88 std::atomic_uint failedFileWrites = {0};
89 std::atomic_uint failedLogs = {0};
90 std::ofstream logFile;
91 std::chrono::milliseconds max_log_timeout_ms = std::chrono::milliseconds(10);
92 std::chrono::milliseconds max_file_write_timeout_ms = std::chrono::milliseconds(10);
93 int log_mask = 28; // Default is Warn, Error and Critical. Can be changed dynamically.
94 std::atomic_bool writeToFile = {false};
95};
rmmStatus putInfo(const char *sender, std::string msg)
Calls both putMsg and info methods.
Definition RMMLog.cpp:463
void logRelay(std::string logLevel, FILE *out)
Relays log output to a specified file for a given log level.
Definition RMMLog.cpp:137
rmmStatus setLogFilePath(std::string filePath, bool useName=false)
Sets the log file path for the log file.
Definition RMMLog.cpp:353
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:306
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:317
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:408
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:290
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:34
int mask
Log mask associated with the message.
Definition RMMLog.h:37
const char * sender
Sender of the log message.
Definition RMMLog.h:35
const char * message
The actual log message.
Definition RMMLog.h:36