-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
33 lines (28 loc) · 753 Bytes
/
logger.cpp
File metadata and controls
33 lines (28 loc) · 753 Bytes
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
#include <fstream>
#include <string>
#include <mutex>
class AofLogger {
private:
std::ofstream file_;
std::mutex mutex_;
std::string filename_;
public:
AofLogger(const std::string& filename) : filename_(filename) {
file_.open(filename, std::ios::app);
}
void append(const std::string& command) {
std::lock_guard<std::mutex> lock(mutex_);
if (file_.is_open()) {
file_ << command << "\n";
file_.flush();
}
}
void clearFile() {
std::lock_guard<std::mutex> lock(mutex_);
if (file_.is_open()) file_.close();
file_.open(filename_, std::ios::out | std::ios::trunc);
}
~AofLogger() {
if (file_.is_open()) file_.close();
}
};