-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cpp
64 lines (59 loc) · 1.21 KB
/
Log.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Log.cpp
*
* Created on: 07-12-2015
* Author: felipe-narvaez
*/
#include "Log.h"
/**
* Log class
*/
Log::Log(std::string path) {
if (folderExist(path)) {
buffer.open(path.c_str(), std::ios::in);
} else {
createFolder(path);
buffer.open(path.c_str());
}
writeDate();
}
void Log::close() {
buffer.close();
}
Log::~Log() {
close();
}
bool Log::writeDate() {
time_t now = time(0);
tm *ltm = localtime(&now);
std::string date(ctime(&now));
write(date);
}
bool Log::write(std::string& data) {
if (this->buffer.is_open()) {
this->buffer << "================================================";
this->buffer << data;
this->buffer << "================================================";
return true;
}
return false;
}
bool Log::folderExist(std::string& path) {
try {
boost::filesystem::path p(path);
if (exists(p)) // does p actually exist?
{
if (is_directory(p)) // is p a regular file?
return true;
}
} catch (const boost::filesystem::filesystem_error& ex) {
std::cerr << ex.what() << "\n";
return false;
}
return false;
}
void Log::createFolder(std::string& path) {
boost::filesystem::path p(path);
if (!folderExist(path))
create_directory(p.parent_path());
}