forked from MortezaBashsiz/nipovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.hpp
48 lines (37 loc) · 1.18 KB
/
log.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <ctime>
#include <fstream>
#include <iomanip>
#include <memory>
#include <mutex>
#include <string>
#include "config.hpp"
#include "general.hpp"
/**
* @brief A class for logging messages with different levels (INFO, TRACE,
* ERROR, DEBUG).
*
* The Log class handles logging messages to a file and/or console based on the
* configured log level. It provides facilities to create log instances, set
* the log level, and write log messages.
*/
class Log : private Uncopyable {
public:
enum class Level { INFO, TRACE, ERROR, DEBUG };
using pointer = std::shared_ptr<Log>;
static pointer create(const std::shared_ptr<Config> &config) {
return pointer(new Log(config));
}
explicit Log(const std::shared_ptr<Log> &log);
~Log();
const std::shared_ptr<Config> &config() const { return config_; }
Level level() const { return level_; }
void write(const std::string &message, Level level) const;
private:
explicit Log(const std::shared_ptr<Config> &config);
static std::string levelToString(Level level);
const std::shared_ptr<Config> &config_;
Level level_;
std::string mode_;
mutable std::mutex logMutex_; // Mutex to protect write operations
};