Peano
Loading...
Searching...
No Matches
ChromeTraceFileLogger.cpp
Go to the documentation of this file.
2
3#include "tarch/Assertions.h"
4
7
8#include <sstream>
9#include <fstream>
10#include <stdlib.h>
11#include <algorithm>
12
13#include "LogFilter.h"
14
15
16#include "../mpi/Rank.h"
17
18
19
20tarch::logging::Log tarch::logging::ChromeTraceFileLogger::_log( "tarch::logging::ChromeTraceFileLogger" );
21
22
24
25
27 _outputStream(nullptr),
28 _quitOnError(false),
29 _hasWrittenEntry(false) {
31}
32
33
37
38
40 if ( _hasWrittenEntry and _outputStream!=nullptr) {
41 *_outputStream << ",\n";
42 }
43 _hasWrittenEntry = true;
44}
45
46
48 if (_outputStream!=nullptr) {
49 _outputStream->setf( std::ios_base::scientific, std::ios_base::floatfield );
50 _outputStream->precision(20);
51 }
52 std::cerr.setf( std::ios_base::scientific, std::ios_base::floatfield );
53 std::cerr.precision(20);
54 std::cout.setf( std::ios_base::scientific, std::ios_base::floatfield );
55 std::cout.precision(20);
56}
57
58
62
63
65
66
70
71
72
73
74std::string tarch::logging::ChromeTraceFileLogger::addSeparators(std::string message) const {
75 const int ColumnWidth = 8;
76 if ( message.size() > 0 ) {
77 while (message.size() < ColumnWidth) {
78 message += " ";
79 }
80 }
81 message += " ";
82
83 return message;
84}
85
86
88 [[maybe_unused]] std::string messageType,
89 [[maybe_unused]] long int timestampMS,
90 [[maybe_unused]] int rank,
91 [[maybe_unused]] int threadId,
92 [[maybe_unused]] const std::string& trace,
93 [[maybe_unused]] const std::string& message
94) {
95 std::string result;
96
97 result += addSeparators(std::to_string(timestampMS));
98 result += addSeparators("rank:" + std::to_string(rank) );
99 result += addSeparators("core:" + std::to_string(threadId) );
100 result += addSeparators(messageType);
101 result += message;
102 result += "\n";
103
104 return result;
105}
106
108 [[maybe_unused]] std::string messageType,
109 [[maybe_unused]] long int timestampMS,
110 [[maybe_unused]] int rank,
111 [[maybe_unused]] int threadId,
112 [[maybe_unused]] const std::string& trace,
113 [[maybe_unused]] const std::string& message
114) {
115 std::ostringstream result;
116 std::string args = message;
117 std::replace( args.begin(), args.end(), '"', ' '); // replace all 'x' to 'y'
118 result << " {"
119 << " \"name\": \"" << trace << "\","
120 << " \"cat\": \"" << messageType << "\","
121 << " \"ph\": \"i\","
122 << " \"ts\": " << timestampMS << ","
123 << " \"pid\": " << rank << ","
124 << " \"tid\": " << threadId << ","
125 << " \"args\": { \"message\": \"" + args + "\"}"
126 " }";
127 return result.str();
128}
129
130
132 [[maybe_unused]] long int timestampMS,
133 [[maybe_unused]] int rank,
134 [[maybe_unused]] int threadId,
135 [[maybe_unused]] const std::string& trace,
136 [[maybe_unused]] const std::string& message
137) {
138 std::string eventEntry = constructEventEntryInTraceFile(
139 "debug",
140 timestampMS, rank, threadId, trace, message
141 );
142 tarch::multicore::Lock lockCout( _semaphore );
143 if (_outputStream!=nullptr) {
144 nextEntry();
145 *_outputStream << eventEntry;
146 #if PeanoDebug>=4
147 _outputStream->flush();
148 #endif
149 }
150}
151
152
153void tarch::logging::ChromeTraceFileLogger::info(long int timestampMS, int rank, int threadId, const std::string& trace, const std::string& message) {
154 std::string outputMessage = constructMessageString(
155 "info",
156 timestampMS, rank, threadId, trace, message
157 );
158 std::string eventEntry = constructEventEntryInTraceFile(
159 "info",
160 timestampMS, rank, threadId, trace, message
161 );
162 tarch::multicore::Lock lockCout( _semaphore );
163 std::cout << outputMessage;
164 if (_outputStream!=nullptr) {
165 nextEntry();
166 *_outputStream << eventEntry;
167 #if PeanoDebug>=4
168 _outputStream->flush();
169 #endif
170 }
171}
172
173
174void tarch::logging::ChromeTraceFileLogger::warning(long int timestampMS, int rank, int threadId, const std::string& trace, const std::string& message) {
175 std::string outputMessage = constructMessageString(
176 "warning",
177 timestampMS, rank, threadId, trace, message
178 );
179 std::string eventEntry = constructEventEntryInTraceFile(
180 "warning",
181 timestampMS, rank, threadId, trace, message
182 );
183 tarch::multicore::Lock lockCout( _semaphore );
184 std::cout << outputMessage;
185 if (_outputStream!=nullptr) {
186 nextEntry();
187 *_outputStream << eventEntry;
188 #if PeanoDebug>=4
189 _outputStream->flush();
190 #endif
191 }
192}
193
194
195void tarch::logging::ChromeTraceFileLogger::traceIn(long int timestampMS, int rank, int threadId, const std::string& trace, const std::string& message) {
196 if (_outputStream!=nullptr) {
197 std::string args = message;
198 std::replace( args.begin(), args.end(), '"', ' '); // replace all 'x' to 'y'
199 tarch::multicore::Lock lockCout( _semaphore );
200 nextEntry();
201 *_outputStream << " {"
202 << " \"name\": \"" << trace << "\","
203 << " \"cat\": \"trace\","
204 << " \"ph\": \"B\","
205 << " \"ts\": " << timestampMS << ","
206 << " \"pid\": " << rank << ","
207 << " \"tid\": " << threadId << ","
208 << " \"args\": { \"params\": \"" + args + "\"}"
209 " }";
210 #if PeanoDebug>=4
211 _outputStream->flush();
212 #endif
213 }
214}
215
216
217void tarch::logging::ChromeTraceFileLogger::traceOut(long int timestampMS, int rank, int threadId, const std::string& trace, const std::string& message) {
218 if (_outputStream!=nullptr) {
219 std::string args = message;
220 std::replace( args.begin(), args.end(), '"', ' '); // replace all 'x' to 'y'
221 tarch::multicore::Lock lockCout( _semaphore );
222 nextEntry();
223 *_outputStream << " {"
224 << " \"name\": \"" << trace << "\","
225 << " \"cat\": \"trace\","
226 << " \"ph\": \"E\","
227 << " \"ts\": " << timestampMS << ","
228 << " \"pid\": " << rank << ","
229 << " \"tid\": " << threadId << ","
230 << " \"args\": { \"result\": \"" + args + "\"}"
231 " }";
232 #if PeanoDebug>=4
233 _outputStream->flush();
234 #endif
235 }
236}
237
238
239void tarch::logging::ChromeTraceFileLogger::error(long int timestampMS, int rank, int threadId, const std::string& trace, const std::string& message) {
240 std::string outputMessage = constructMessageString(
241 "error",
242 timestampMS, rank, threadId, trace, message
243 );
244 std::string eventEntry = constructEventEntryInTraceFile(
245 "error",
246 timestampMS, rank, threadId, trace, message
247 );
248 tarch::multicore::Lock lockCout( _semaphore );
249 std::cerr << outputMessage;
250 if (_outputStream!=nullptr) {
251 nextEntry();
252 *_outputStream << eventEntry;
253 }
254
255 if (_quitOnError) {
257 }
258}
259
260void tarch::logging::ChromeTraceFileLogger::indent( bool, const std::string&, const std::string& ) {}
261
262void tarch::logging::ChromeTraceFileLogger::setOutputFile( const std::string& outputLogFileName ) {
263 std::ostringstream myOutputFileName;
264 #ifdef Parallel
265 if (!outputLogFileName.empty()) {
266 myOutputFileName << "rank-" << tarch::mpi::Rank::getInstance().getRank() << "-" << outputLogFileName;
267 }
268 #else
269 myOutputFileName << outputLogFileName;
270 #endif
271
272 if (_outputStream!=nullptr) {
273 _outputStream->flush();
274 delete _outputStream;
275 _outputStream = nullptr;
276 }
277
278 _outputStream = new std::ofstream( myOutputFileName.str().c_str() );
279
280 configureOutputStreams();
281
282 *_outputStream << "[\n";
283}
284
285
287 _quitOnError = value;
288}
289
290
292 std::cout.flush();
293 std::cerr.flush();
294 if (_outputStream!=nullptr) {
295 *_outputStream << "]";
296 delete _outputStream;
297 _outputStream = nullptr;
298 } else {
299 std::cerr << "Warning: ChromeTraceFileLogger used, but no trace file set. Use singleton's setOuputFile(). No trace information dumped" << std::endl;
300 }
301}
void debug(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
std::string constructEventEntryInTraceFile(std::string messageType, long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
void indent(bool indent, const std::string &trace, const std::string &message)
Tells the logger to increment/decrement the indent.
void info(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
ChromeTraceFileLogger & operator=(const ChromeTraceFileLogger &)
Declared private since assignment does not make sense for an output class (output information mismatc...
void traceIn(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
static ChromeTraceFileLogger _singleton
void error(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
void configureOutputStreams()
Configures the output streams.
void setOutputFile(const std::string &outputLogFileName)
Is redundant, as you could use setLogFormat() instead.
std::string addSeparators(std::string message) const
Ensures each column has same length.
void traceOut(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
static ChromeTraceFileLogger & getInstance()
void warning(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
std::string constructMessageString(std::string messageType, long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
Construct message string for output to terminal.
Log Device.
Definition Log.h:516
static Rank & getInstance()
This operation returns the singleton instance.
Definition Rank.cpp:539
int getRank() const
Return rank of this node.
Definition Rank.cpp:529
static void abort(int errorCode)
A proper abort in an MPI context has to use MPI_Abort.
Definition Rank.cpp:592
Create a lock around a boolean semaphore region.
Definition Lock.h:19