forked from zpb1992/ReentryTrajectoryGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cpp
79 lines (69 loc) · 1.27 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Log.h"
#include"Console.h"
Log *Log::_instance = nullptr;
Log::Log()
{
file();
}
bool Log::file()
{
setDateFile();
if (_ofs.is_open())
_ofs.close();
_ofs.open(_filename, std::ofstream::out | std::ofstream::app);
printTime();
return true;
}
bool Log::file(std::string filename)
{
if (_ofs.is_open())
_ofs.close();
_ofs.open(_filename, std::ofstream::out | std::ofstream::app);
printTime();
return true;
}
Log::~Log()
{
_ofs << std::endl;
close();
}
Log * Log::instance()
{
if (_instance == nullptr)
_instance = new Log;
return nullptr;
}
bool Log::close()
{
if (_ofs)
_ofs.close();
return true;
}
bool Log::printTime()
{
std::string t;
time_t time_sec = time(nullptr);
tm time;
localtime_s(&time, &time_sec);
t += "at ";
t += std::to_string(time.tm_hour);
t += ":";
t += std::to_string(time.tm_min);
t += ":";
t += std::to_string(time.tm_sec);
_ofs << t << std::endl;
return false;
}
std::string & Log::setDateFile()
{
time_t tempTime = time(nullptr);
tm localTime;
localtime_s(&localTime, &tempTime);
_filename += std::to_string(localTime.tm_year + 1900);
_filename += "_";
_filename += std::to_string(localTime.tm_mon + 1);
_filename += "_";
_filename += std::to_string(localTime.tm_mday);
_filename += ".log";
return _filename;
}