LBIBCell
 All Classes Functions Variables Friends Pages
Log.cpp
1 /* Copyright (c) 2013 David Sichau <mail"at"sichau"dot"eu>
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18  * THE SOFTWARE.
19  *
20  */
21 #include <UtilLib/config.hpp>
22 #include <UtilLib/include/MPIProxy.hpp>
23 #include <UtilLib/include/Log.hpp>
24 #include <UtilLib/include/Exception.hpp>
25 #include <iomanip>
26 #include <string>
27 
28 namespace UtilLib {
29 namespace {
35 std::string logLevelToString(const LogLevel& level) {
36  switch (level) {
37  case logERROR:
38  return std::string("Error");
39  break;
40  case logWARNING:
41  return std::string("Warning");
42  break;
43  case logINFO:
44  return std::string("Info");
45  break;
46  case logDEBUG:
47  return std::string("Debug");
48  break;
49  case logDEBUG1:
50  return std::string("Debug1");
51  break;
52  case logDEBUG2:
53  return std::string("Debug2");
54  break;
55  case logDEBUG3:
56  return std::string("Debug3");
57  break;
58  case logDEBUG4:
59  return std::string("Debug4");
60  break;
61  default:
62  break;
63  }
64  return std::string("");
65 }
66 
70 struct null_deleter {
74  void operator()(void const*) const {}
75 };
76 }
77 
78 /*
79  * The default log level is set to the compile flag DEBUGLEVEL.
80  */
81 LogLevel Log::reportingLevel_ = DEBUGLEVEL;
82 
84 // Default the log is printed to std::cerr. To avoid the deletion of std::cerr a null_deleter is provided.
85 std::shared_ptr<std::ostream> Log::pStream_(&std::cerr, null_deleter());
87 std::shared_ptr<std::ostream> Log::getStream() {
88  return pStream_;
89 }
90 
91 void Log::setStream(std::shared_ptr<std::ostream> pStream) {
92  if (!pStream) {
93  throw Exception(
94  "The stream is not available. You can only set the stream to an existing one.");
95  }
96  pStream_ = pStream;
97 }
98 
99 void Log::writeOutput(const std::string& msg) {
100  std::shared_ptr<std::ostream> pStream = getStream();
101  (*pStream) << msg;
102  pStream->flush();
103 }
104 
105 std::ostringstream& Log::writeReport(LogLevel level) {
106  // generate time in the format Date HH::MM::SS
107  time_t rawtime;
108  time(&rawtime);
109  struct tm tempTm1;
110  struct tm* tempTm2;
111  tempTm2 = localtime_r(&rawtime, &tempTm1);
112  char outstr[200];
113  strftime(outstr, sizeof(outstr), "%x% %H:%M:%S", tempTm2);
114 
115  buffer_ << "- " << outstr;
116  buffer_ << " Proc " << MPIProxy().getRank() << " of "
117  << MPIProxy().getSize();
118  buffer_ << std::setw(10) << logLevelToString(level) << ":\t";
119  return buffer_;
120 }
121 
122 void Log::setReportingLevel(LogLevel level) {
123  LOG(logINFO) << "Report Level changed from "
124  << logLevelToString(reportingLevel_) << " to "
125  << logLevelToString(level);
126  reportingLevel_ = level;
127 }
128 
130  return reportingLevel_;
131 }
132 
134  buffer_ << std::endl;
135  Log::writeOutput(buffer_.str());
136 }
137 }
static LogLevel getReportingLevel()
Definition: Log.cpp:129
int getRank() const
Definition: MPIProxy.cpp:40
static void setStream(std::shared_ptr< std::ostream > pStream)
Definition: Log.cpp:91
static void setReportingLevel(LogLevel level)
Definition: Log.cpp:122
std::ostringstream & writeReport(LogLevel level=logINFO)
Definition: Log.cpp:105
int getSize() const
Definition: MPIProxy.cpp:44
virtual ~Log()
Definition: Log.cpp:133
static std::shared_ptr< std::ostream > getStream()
Definition: Log.cpp:87