Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
logger.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <cstdlib>
24#include <time.h>
25#include "logger.h"
26#include <iostream>
27
28using namespace std;
29
33 fileOpen = false;
34 masterStream = NULL;
35}
36
40 close();
41 if (masterStream != NULL) {
42 masterStream->close();
43 delete masterStream;
44 }
45}
46
52 if (fileOpen == false) return false;
53 bool success = true;
54 masterStream->close();
55 delete masterStream;
56 masterStream = NULL;
57 fileOpen = false;
58 return success;
59}
60
67bool Logger::flush(bool verbose) {
68 if (fileOpen == false) return false;
69 bool success = true;
70 if (mpiRank != masterRank) return true;
71 stringstream tmp;
72 string strTime;
73 if (verbose == true)
74 {
75 // Get the current time. Remove the line break from
76 // the string output given by ctime.
77 const time_t rawTime = time(NULL);
78 strTime = ctime(&rawTime);
79
80 const size_t lineBreak = strTime.find('\n');
81 if (lineBreak != string::npos) {
82 strTime.erase(lineBreak,1);
83 }
84
85 // Form a new output buffer containing the process number, date
86 // and the user-given message. Attempt to write it to file.
87
88 tmp << strTime << ' ' << endl;
89 strTime = outStream.str();
90 tmp << strTime << endl;
91
92 } else {
93 strTime = outStream.str();
94 tmp << strTime;
95 }
96 (*masterStream) << tmp.str() << std::flush;
97 outStream.str(string(""));
98 outStream.clear();
99 return success;
100}
101
117bool Logger::open(MPI_Comm comm,const int& MASTERRANK,const std::string& fname,const bool& append) {
118 // Store the MPI rank of this process
119 MPI_Comm_rank(comm,&mpiRank);
120 masterRank = MASTERRANK;
121 bool rvalue = true;
122
123 if (mpiRank != MASTERRANK) return rvalue;
124 masterStream = new fstream;
125 if(append)
126 masterStream->open(fname.c_str(), fstream::out|fstream::app);
127 else
128 masterStream->open(fname.c_str(), fstream::out);
129
130 if (masterStream->good() == false) rvalue = false;
131 fileOpen = true;
132 return rvalue;
133}
134
135bool Logger::print(const std::string& s) {
136 if (mpiRank != masterRank) return true;
137 (*masterStream) << s;
138 return true;
139}
140
141// *********************************
142// ****** STREAM MANIPULATORS ******
143// *********************************
144
152 if (mpiRank != masterRank) return *this;
153 return (*pf)(*this);
154}
155
162Logger& Logger::operator<<(std::ostream& (*pf)(std::ostream& )) {
163 if (mpiRank != masterRank) return *this;
164 (*pf)(outStream);
165 return *this;
166}
167
178 if (logger.flush(true) == false) {
179 cerr << "Logger failed to write!" << endl;
180 }
181 return logger;
182}
183
193Logger& write(Logger& logger) {
194 if (logger.flush(false) == false) {
195 cerr << "Logger failed to write!" << endl;
196 }
197 return logger;
198}
bool open(MPI_Comm comm, const int &MASTERRANK, const std::string &fname, const bool &append=false)
Open a logfile.
Definition logger.cpp:117
int masterRank
Definition logger.h:73
bool fileOpen
Definition logger.h:71
int mpiRank
Definition logger.h:72
Logger & operator<<(const T &value)
Definition logger.h:84
bool print(const std::string &s)
Definition logger.cpp:135
std::fstream * masterStream
Definition logger.h:75
bool flush(bool verbose)
Definition logger.cpp:67
Logger()
Definition logger.cpp:32
~Logger()
Definition logger.cpp:39
bool close()
Definition logger.cpp:51
std::stringstream outStream
Definition logger.h:74
Logger & writeVerbose(Logger &logger)
Definition logger.cpp:177
Logger & write(Logger &logger)
Definition logger.cpp:193