-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
40 lines (32 loc) · 852 Bytes
/
Logger.cpp
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
#include "Logger.h"
void Logger::save_message_log(std::string s, std::string sender, std::string receiver) {
if (file.is_open())
{
std::lock_guard<std::mutex> lock(mutex);
file << (countLines() + 1) << ". " << "user " << sender << " say '" << s
<< "' to user " << receiver << std::endl;
}
};
std::string Logger::readLine() {
std::ifstream file(filename);
std::string line;
if(file.is_open())
{
std::lock_guard<std::mutex> lock(mutex);
std::getline(file, line);
}
return line;
}
int Logger::countLines() {
std::ifstream file(filename);
std::string str;
int res = 0;
if (file.is_open())
{
std::lock_guard<std::mutex> lock(mutex);
for (std::string line; getline(file, str); ) {
res++;
}
}
return res;
}