-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.cpp
147 lines (121 loc) · 5.25 KB
/
Protocol.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
/*
* File: Protocol.cpp
* Author: RM
*
* Created on 17. August 2013, 22:53
*/
#include "Protocol.h"
#include "MHESonCore.h"
const string Protocol::FILEPATH_BASE = "MHESonProtocol_";
ofstream Protocol::_protocol;
string Protocol::_currentPhase = "";
uint Protocol::_currentPhaseCounter = 0;
bool Protocol::_printToConsole = false;
Protocol::Protocol()
{
}
Protocol::Protocol(const Protocol& orig)
{
}
Protocol::~Protocol()
{
}
void Protocol::init(string filepath, string subdirectory, bool printToConsole)
{
_printToConsole = printToConsole;
string completeFilepath;
if (filepath == "") {
subdirectory = subdirectory != "" ? subdirectory + "/" : "";
completeFilepath = subdirectory + Protocol::FILEPATH_BASE;
/*
// Getting current date in C++11: See http://stackoverflow.com/questions/16357999/current-date-and-time-as-string
auto t = time(nullptr);
auto tm = *localtime(&t);
std::cout << put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
*/
// Get current date, doing it it ye olde way (at this time, the cygwin
// repository doesn't offer g++ 4.8, which would allow us to operate
// with dates, strings and numbers in a way sane people would expect).
time_t t = time(0);
struct tm* now = localtime( &t );
string date;
// Append leading zeros, if necessary
string leadingZeroMo = (now->tm_mon + 1) < 10 ? "0" : "";
string leadingZeroD = (now->tm_mday + 1) < 10 ? "0" : "";
string leadingZeroH = (now->tm_hour + 1) < 10 ? "0" : "";
string leadingZeroMi = (now->tm_min + 1) < 10 ? "0" : "";
string leadingZeroSec = (now->tm_sec) < 10 ? "0" : "";
// Concatenate to date string
stringstream stream;
stream << (now->tm_year + 1900) << '-'
<< leadingZeroMo << (now->tm_mon + 1) << '-'
<< leadingZeroD << now->tm_mday << "_"
<< leadingZeroH << now->tm_hour << "-"
<< leadingZeroMi << now->tm_min << "-"
<< leadingZeroSec << now->tm_sec
<< endl;
// Redirect stream into date string
stream >> date;
// Create full file path
completeFilepath += date + ".txt";
}
else
completeFilepath = filepath;
// Open date-stamped protocol file
Protocol::_protocol.open(completeFilepath, ios::out);
// Print file header
_protocol << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << endl;
_protocol << "° MHESon " << ParametrizationSettings::META_VERSION << " °" << endl;
_protocol << "° Last change: " << ParametrizationSettings::META_LAST_CHANGE << " °" << endl;
_protocol << "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°" << endl;
if (_printToConsole) {
cout << "*************************************************" << endl;
cout << "* MHESon " << ParametrizationSettings::META_VERSION << " *" << endl;
cout << "* Last change: " << ParametrizationSettings::META_LAST_CHANGE << " *" << endl;
cout << "*************************************************" << endl;
}
}
void Protocol::finalize()
{
_currentPhase = "";
_currentPhaseCounter = 0;
_protocol.close();
}
void Protocol::introducePhase(const string phase)
{
_currentPhase = phase;
_protocol << endl;
_protocol << "================================================" << endl;
_protocol << "*** PHASE " << _currentPhaseCounter << ": " << _currentPhase << " ***" << endl;
_protocol << "------------------------------------------------" << endl << endl;
if (_printToConsole) {
cout << endl;
cout << "================================================" << endl;
cout << "*** PHASE " << _currentPhaseCounter << ": " << _currentPhase << " ***" << endl;
cout << "------------------------------------------------" << endl << endl;
}
}
void Protocol::concludePhase(chrono::milliseconds duration)
{
_protocol << endl << " ## Time elapsed: " << duration.count() << " ms" << endl;
_protocol << endl;
_protocol << "------------------------------------------------" << endl;
_protocol << "*** Finished phase " << _currentPhaseCounter << " ***" << endl;
_protocol << "================================================" << endl << endl << endl;
if (_printToConsole) {
cout << endl << " ## Time elapsed: " << duration.count() << " ms" << endl;
cout << endl;
cout << "------------------------------------------------" << endl;
cout << "*** Finished phase " << _currentPhaseCounter << " ***" << endl;
cout << "================================================" << endl << endl << endl;
}
_currentPhaseCounter++;
}
ofstream& Protocol::protocol()
{
return _protocol;
}
bool& Protocol::printToConsole()
{
return _printToConsole;
}