-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
51 lines (41 loc) · 1.1 KB
/
Copy pathlogger.cpp
File metadata and controls
51 lines (41 loc) · 1.1 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
#include "logger.hpp"
#include <time.h>
#include <ctime>
#include <string>
#include <fstream>
Logger::Logger(std::shared_ptr<Config> ptr):
pCnf(ptr)
{
logfile_path = pCnf->findConfigParamValue("GENERAL", "logfile");
logfile.exceptions ( ifstream::failbit | ifstream::badbit );
try {
logfile.open(logfile_path.c_str(), fstream::in | fstream::out | fstream::app);
} catch (fstream::failure) {
throw std::runtime_error("Cannot open logfile");
}
}
Logger::~Logger() {
logfile.close();
}
Logger& Logger::operator<< (const string& record) {
logfile << record;
logfile.flush();
return *this;
}
Logger& Logger::operator<< (const long& record) {
logfile << record;
logfile.flush();
return *this;
}
string Logger::date() const {
/* INFO:
* Returns string with newline character at the beginning and space at the end
*/
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
string str_time(asctime(timeinfo));
str_time.erase(str_time.end()-1);
return ( string("\n") += str_time += " " );
}