n2p2 - A neural network potential package
Log.cpp
Go to the documentation of this file.
1// n2p2 - A neural network potential package
2// Copyright (C) 2018 Andreas Singraber (University of Vienna)
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#include "Log.h"
18#include <iostream> // std::cout
19
20using namespace std;
21using namespace nnp;
22
23Log::Log() : writeToStdout(true ),
24 silent (false)
25{
26}
27
28Log& Log::operator<<(string const& entry)
29{
30 addLogEntry(entry);
31 return *this;
32}
33
34Log& Log::operator<<(vector<string> const& entries)
35{
36 addMultipleLogEntries(entries);
37 return *this;
38}
39
40void Log::addLogEntry(string const& entry)
41{
42 if (silent) return;
43 memory.push_back(entry);
44
45 if (writeToStdout)
46 {
47 cout << entry;
48 cout.flush();
49 }
50
51 for (vector<FILE**>::const_iterator it = cFilePointers.begin();
52 it != cFilePointers.end(); ++it)
53 {
54 if ((**it) != 0)
55 {
56 fprintf((**it), "%s", entry.c_str());
57 fflush((**it));
58 }
59 }
60
61 for (vector<ofstream*>::const_iterator it = streamPointers.begin();
62 it != streamPointers.end(); ++it)
63 {
64 if ((**it).is_open())
65 {
66 (**it) << entry;
67 (**it).flush();
68 }
69 }
70
71 return;
72}
73
74void Log::addMultipleLogEntries(vector<string> const& entries)
75{
76 for (vector<string>::const_iterator it = entries.begin();
77 it != entries.end(); ++it)
78 {
79 addLogEntry(*it);
80 }
81
82 return;
83}
84
85void Log::registerCFilePointer(FILE** const& filePointer)
86{
87 cFilePointers.push_back(filePointer);
88 return;
89}
90
91void Log::registerStreamPointer(ofstream* const& streamPointer)
92{
93 streamPointers.push_back(streamPointer);
94 return;
95}
96
97vector<string> Log::getLog() const
98{
99 return memory;
100}
Logging class for library output.
Definition: Log.h:34
void addLogEntry(std::string const &entry)
Add string as new log entry.
Definition: Log.cpp:40
std::vector< std::string > getLog() const
Get complete log memory.
Definition: Log.cpp:97
Log & operator<<(std::string const &entry)
Overload << operator.
Definition: Log.cpp:28
void registerStreamPointer(std::ofstream *const &streamPointer)
Register new C++ ofstream pointer.
Definition: Log.cpp:91
void addMultipleLogEntries(std::vector< std::string > const &entries)
Add multiple log entries at once.
Definition: Log.cpp:74
bool silent
Completely silence output.
Definition: Log.h:87
std::vector< std::string > memory
Memory with all log entries.
Definition: Log.h:91
void registerCFilePointer(FILE **const &filePointer)
Register new C-style FILE* pointer for output.
Definition: Log.cpp:85
std::vector< FILE ** > cFilePointers
Storage for C-style FILE* pointers.
Definition: Log.h:93
bool writeToStdout
Turn on/off output to stdout.
Definition: Log.h:85
std::vector< std::ofstream * > streamPointers
Storage for C++ ofstream pointers.
Definition: Log.h:95
Definition: Atom.h:29