hemocell
Loading...
Searching...
No Matches
logfile.h
Go to the documentation of this file.
1/*
2This file is part of the HemoCell library
3
4HemoCell is developed and maintained by the Computational Science Lab
5in the University of Amsterdam. Any questions or remarks regarding this library
6can be sent to: info@hemocell.eu
7
8When using the HemoCell library in scientific work please cite the
9corresponding paper: https://doi.org/10.3389/fphys.2017.00563
10
11The HemoCell library is free software: you can redistribute it and/or
12modify it under the terms of the GNU Affero General Public License as
13published by the Free Software Foundation, either version 3 of the
14License, or (at your option) any later version.
15
16The library is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU Affero General Public License for more details.
20
21You should have received a copy of the GNU Affero General Public License
22along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24#ifndef LOGFILE_H
25#define LOGFILE_H
26
27#include <fstream>
28#include <iostream>
29#include <sstream>
30#include <string>
31namespace hemo {
41class Logfile {
42public:
43 Logfile() : logfile(*(new std::fstream())) {}
44 Logfile(std::fstream & logfile_) : logfile(logfile_) {}
45 std::fstream & logfile;
46 std::string filename = "";
48 bool mainProcess = false;
50 std::ostringstream pending;
51
54 bool open(const std::string & filename_) {
55 if (!logfile.is_open()) {
56 filename = filename_;
57 logfile.open(filename_, std::fstream::out);
58 if (logfile.is_open()) {
59 logfile << pending.str();
60 pending.str("");
61 }
62 }
63 return logfile.is_open();
64 }
66 void close() {
67 if (logfile.is_open()) { logfile.close(); }
68 pending.str("");
69 mainProcess = false;
70 }
71};
72template <typename Val>
73Logfile & operator<<(Logfile & lf, Val const & rhs) {
74 if (lf.logfile.is_open()) {
75 std::cout << rhs;
76 lf.logfile << rhs;
77 } else if (lf.mainProcess) {
78 std::cout << rhs;
79 lf.pending << rhs;
80 }
81 return lf;
82}
83inline Logfile & operator<<(Logfile & lf, std::ostream & (*op)(std::ostream &)) {
84 if (lf.logfile.is_open()) {
85 std::cout << op << std::flush;
86 lf.logfile << op;
87 } else if (lf.mainProcess) {
88 std::cout << op << std::flush;
89 lf.pending << op;
90 }
91 return lf;
92}
93extern Logfile hlog;
94
96class Logfile_only {};
97template <typename Val>
98Logfile_only & operator<<(Logfile_only & lf, Val const & rhs) {
99 if (hlog.logfile.is_open()) {
100 hlog.logfile << rhs;
101 } else if (hlog.mainProcess) {
102 hlog.pending << rhs;
103 }
104 return lf;
105}
106inline Logfile_only & operator<<(Logfile_only & lf, std::ostream & (*op)(std::ostream &)) {
107 if (hlog.logfile.is_open()) {
108 hlog.logfile << op;
109 } else if (hlog.mainProcess) {
110 hlog.pending << op;
111 }
112 return lf;
113}
114extern Logfile_only hlogfile;
115
116}
117#endif /* LOGFILE_H */
Like hlog, but only to the log file (not stdout).
Definition logfile.h:96
Definition logfile.h:41
Logfile(std::fstream &logfile_)
Definition logfile.h:44
std::string filename
Definition logfile.h:46
bool mainProcess
True on the process that writes the log (MPI rank 0); set by HemoCell.
Definition logfile.h:48
Logfile()
Definition logfile.h:43
void close()
Close the log file and stop logging (called when HemoCell is destroyed).
Definition logfile.h:66
bool open(const std::string &filename_)
Definition logfile.h:54
std::ostringstream pending
Messages logged on the main process before the log file was opened.
Definition logfile.h:50
std::fstream & logfile
Definition logfile.h:45
Definition config.cpp:37
Logfile_only hlogfile
Definition logfile.cpp:27
Logfile & operator<<(Logfile &lf, Val const &rhs)
Definition logfile.h:73
Logfile hlog
Definition logfile.cpp:26