-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
103 lines (84 loc) · 1.93 KB
/
Logger.cpp
File metadata and controls
103 lines (84 loc) · 1.93 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Logger.cpp
* Created on: Jun 9, 2016
* Author: nicole
*/
#include "Logger.h"
Logger::Logger( const std::string fileName)
{
_logPath = fileName;
_createLogFile();
}
Logger::~Logger()
{
closeLog();
}
/**
* @brief: static function - document an operation to the log file.
*/
void Logger::logCommand( const std::string command)
{
time_t current_time;
struct tm * time_info;
std::string line;
char timeString[9]; // space for "HH:MM:SS\0"
if ( _logFile.is_open())
{
time(¤t_time);
time_info = localtime( ¤t_time);
strftime( timeString, sizeof(timeString), "%H:%M:%S", time_info);
line = std::string(timeString) + " \t" + command + "\n";
_logFile << line;
flushLogFile();
}
}
void Logger::logEvent(int ID, const std::string title,const std::string time,
const std::string description)
{
std::string command = std::to_string(ID) + "\t" + title + time + "\t";
command += description;
logCommand( command);
}
/**
* @brief: flush the log file. force any unwritten data to be delivered
* to the host environment to be written to the log file.
*/
void Logger::flushLogFile()
{
_logFile.exceptions(std::ifstream::failbit| std::ifstream::badbit);
try {
_logFile.flush();
} catch ( std::ifstream::failure& e) {
std::cerr << e.what() << std::endl;
}
}
/**
* @return: the log's path.
*/
std::string Logger::getLogFullPath() const
{
return _logPath;
}
void Logger::closeLog()
{
/* close the log file */
try {
if ( _logFile.is_open()) {
_logFile.close();
}
} catch ( std::ifstream::failure& e) {
std::cerr << e.what() << std::endl;
}
}
/**
* @brief: create a new log file in the root dir.
* @param rootDir: log's file root dir.
*/
void Logger::_createLogFile()
{
_logFile.open( _logPath.c_str(), std::fstream::in | std::fstream::out |
std::fstream::app);
if ( !_logFile.is_open()) {
std::cout << "Error opening file" << std::endl;
}
}