-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog2file.hpp
More file actions
70 lines (60 loc) · 1.52 KB
/
log2file.hpp
File metadata and controls
70 lines (60 loc) · 1.52 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <filesystem>
#include <fstream>
#include <mutex>
#include <sstream>
class Log2File
{
enum class LogLevel
{
Debug,
Info,
Error
};
public:
Log2File() = default;
Log2File(const std::filesystem::path& filepath);
~Log2File();
bool openFile(const std::filesystem::path& filepath);
bool is_open() const;
template <typename... Args>
void debug(Args... args)
{
#if !defined(NDEBUG) || defined(_DEBUG) || defined(DEBUG)
std::ostringstream messageStream;
messageStream.str("");
messageStream.clear();
(messageStream << ... << args);
log(LogLevel::Debug, messageStream.str());
#else
(static_cast<void>(args), ...);
#endif
}
template <typename... Args>
void info(Args... args)
{
std::ostringstream messageStream;
messageStream.str("");
messageStream.clear();
(messageStream << ... << args);
log(LogLevel::Info, messageStream.str());
}
template <typename... Args>
void err(Args... args)
{
std::ostringstream messageStream;
messageStream.str("");
messageStream.clear();
(messageStream << ... << args);
log(LogLevel::Error, messageStream.str());
}
private:
void log(LogLevel level, const std::string& message);
private:
std::string filename_;
std::mutex mutex_; // Protects concurrent access within the same process.
#ifdef __GNUC__
int fd_ = -1;
#else
HANDLE hFile_ = INVALID_HANDLE_VALUE;
#endif
};