27 _outputStream(nullptr),
29 _hasWrittenEntry(false) {
40 if ( _hasWrittenEntry and _outputStream!=
nullptr) {
41 *_outputStream <<
",\n";
43 _hasWrittenEntry =
true;
48 if (_outputStream!=
nullptr) {
49 _outputStream->setf( std::ios_base::scientific, std::ios_base::floatfield );
50 _outputStream->precision(20);
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);
75 const int ColumnWidth = 8;
76 if ( message.size() > 0 ) {
77 while (message.size() < ColumnWidth) {
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
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);
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
115 std::ostringstream result;
116 std::string args = message;
117 std::replace( args.begin(), args.end(),
'"',
' ');
119 <<
" \"name\": \"" << trace <<
"\","
120 <<
" \"cat\": \"" << messageType <<
"\","
122 <<
" \"ts\": " << timestampMS <<
","
123 <<
" \"pid\": " << rank <<
","
124 <<
" \"tid\": " << threadId <<
","
125 <<
" \"args\": { \"message\": \"" + args +
"\"}"
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
138 std::string eventEntry = constructEventEntryInTraceFile(
140 timestampMS, rank, threadId, trace, message
143 if (_outputStream!=
nullptr) {
145 *_outputStream << eventEntry;
147 _outputStream->flush();
154 std::string outputMessage = constructMessageString(
156 timestampMS, rank, threadId, trace, message
158 std::string eventEntry = constructEventEntryInTraceFile(
160 timestampMS, rank, threadId, trace, message
163 std::cout << outputMessage;
164 if (_outputStream!=
nullptr) {
166 *_outputStream << eventEntry;
168 _outputStream->flush();
175 std::string outputMessage = constructMessageString(
177 timestampMS, rank, threadId, trace, message
179 std::string eventEntry = constructEventEntryInTraceFile(
181 timestampMS, rank, threadId, trace, message
184 std::cout << outputMessage;
185 if (_outputStream!=
nullptr) {
187 *_outputStream << eventEntry;
189 _outputStream->flush();
196 if (_outputStream!=
nullptr) {
197 std::string args = message;
198 std::replace( args.begin(), args.end(),
'"',
' ');
201 *_outputStream <<
" {"
202 <<
" \"name\": \"" << trace <<
"\","
203 <<
" \"cat\": \"trace\","
205 <<
" \"ts\": " << timestampMS <<
","
206 <<
" \"pid\": " << rank <<
","
207 <<
" \"tid\": " << threadId <<
","
208 <<
" \"args\": { \"params\": \"" + args +
"\"}"
211 _outputStream->flush();
218 if (_outputStream!=
nullptr) {
219 std::string args = message;
220 std::replace( args.begin(), args.end(),
'"',
' ');
223 *_outputStream <<
" {"
224 <<
" \"name\": \"" << trace <<
"\","
225 <<
" \"cat\": \"trace\","
227 <<
" \"ts\": " << timestampMS <<
","
228 <<
" \"pid\": " << rank <<
","
229 <<
" \"tid\": " << threadId <<
","
230 <<
" \"args\": { \"result\": \"" + args +
"\"}"
233 _outputStream->flush();
240 std::string outputMessage = constructMessageString(
242 timestampMS, rank, threadId, trace, message
244 std::string eventEntry = constructEventEntryInTraceFile(
246 timestampMS, rank, threadId, trace, message
249 std::cerr << outputMessage;
250 if (_outputStream!=
nullptr) {
252 *_outputStream << eventEntry;
263 std::ostringstream myOutputFileName;
265 if (!outputLogFileName.empty()) {
269 myOutputFileName << outputLogFileName;
272 if (_outputStream!=
nullptr) {
273 _outputStream->flush();
274 delete _outputStream;
275 _outputStream =
nullptr;
278 _outputStream =
new std::ofstream( myOutputFileName.str().c_str() );
280 configureOutputStreams();
282 *_outputStream <<
"[\n";
287 _quitOnError = value;
294 if (_outputStream!=
nullptr) {
295 *_outputStream <<
"]";
296 delete _outputStream;
297 _outputStream =
nullptr;
299 std::cerr <<
"Warning: ChromeTraceFileLogger used, but no trace file set. Use singleton's setOuputFile(). No trace information dumped" << std::endl;
Chrome Trace File Logger.
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.
ChromeTraceFileLogger()
It's a singleton.
void info(long int timestampMS, int rank, int threadId, const std::string &trace, const std::string &message)
void setQuitOnError(bool value)
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.
static Rank & getInstance()
This operation returns the singleton instance.
int getRank() const
Return rank of this node.
static void abort(int errorCode)
A proper abort in an MPI context has to use MPI_Abort.
Create a lock around a boolean semaphore region.