forked from cilel/VS_positioning_Z
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLog.cpp
158 lines (130 loc) · 3.63 KB
/
myLog.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "myLog.h"
#include "unistd.h"
#include "fcntl.h"
const enum myLog::logLevels DEFAULT_LOG_LEVEL = myLog::LEVEL_1;
myLog::myLog()
{
initVars();
init();
}
myLog::myLog(const string& fileName)
{
initVars();
init(fileName);
}
myLog::myLog(const string& fileName,enum logLevels levelIn)
{
initVars();
logLevel = levelIn;
init(fileName);
}
myLog::~myLog()
{
if ( logLevel<QUIET_MODE )
{
clear(ios::goodbit);
*this << endl;
printHeader(1); // add ending time to log file
}
char line_0[61];
sprintf(line_0,"*************************************************************");
*this << line_0 << endl<<endl;
close();
}
void myLog::init(const string& fileName)
{
if ( (fileName.c_str())[0] )
openLog(fileName,LOG_WRITE);
else
openLog(SD_DEFAULT_LOGFILE,LOG_WRITE);
}
void myLog::init(const string& fileName, int mode)
{
if ( (fileName.c_str())[0] )
openLog(fileName,mode);
else
openLog(SD_DEFAULT_LOGFILE, mode);
}
void myLog::init()
{
openLog(SD_DEFAULT_LOGFILE,LOG_WRITE);
}
void myLog::openLog(const string& fileName, int mode)
{
if (logLevel < QUIET_MODE)
{
open(fileName.c_str(),fstream::in | fstream::out | fstream::app);
if ( fail() == 0 )
{
logName = fileName;
printHeader(0); // insert start time into top of log file
}
else
{
cout << "ERROR: Log file " << fileName.c_str()
<< "could not be opened for write access." << endl;
logLevel = QUIET_MODE;
}
}
else
{
cout << "Logging disabled (QUIET_MODE set)" << endl;
}
}
void myLog::initVars()
{
time(&startTime);
logLevel = DEFAULT_LOG_LEVEL;
}
void myLog::printHeader(int theEnd)
{
if ( logLevel < QUIET_MODE )
{
clear(ios::goodbit);
// setup time
time_t sttime;
time(&sttime);
// convert to gm time
struct tm * tim = gmtime(&sttime);
// set data items
int sec = tim->tm_sec; // second (0-61, allows for leap seconds)
int min = tim->tm_min; // minute (0-59)
int hour = tim->tm_hour; // hour (0-23)
int mon = tim->tm_mon + 1; // month (0-11)
int mday = tim->tm_mday; // day of the month (1-31)
int year = tim->tm_year % 100; // years since 1900
char cur_time[9];
char cur_date[9];
sprintf(cur_time,"%02d:%02d:%02d",hour,min,sec);
sprintf(cur_date,"%02d/%02d/%02d",mon,mday,year);
// char line_0[61];
// sprintf(line_0,"*************************************************************");
char line_1[61];
sprintf(line_1,"Work Log File");
char line_2[61];
sprintf(line_2,"Date and time: %s - %s%30s",cur_date,cur_time,logName.c_str());
// *this << line_0 << endl<<endl;
*this << line_1 << endl;
*this << line_2 << endl << endl;
if (theEnd)
{
*this << getExecTime() << endl;
}
}
}
void myLog::getExecTime(int* min, int* sec)
{
time_t endTime;
time(&endTime);
*min = (int)((endTime - startTime)/60);
*sec = (int)((endTime - startTime)%60);
}
char* myLog::getExecTime()
{
int min = 0;
int sec = 0;
getExecTime(&min, &sec);
static char execTime[128];
sprintf(execTime, "Execution time: %d minutes %d seconds", min, sec);
return execTime;
}