MoorDyn
Log.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, Matt Hall
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
37 #pragma once
38 
39 #include "MoorDynAPI.h"
40 #include <iostream>
41 #include <fstream>
42 
43 namespace moordyn {
44 
49 std::string
50 log_level_name(int level);
51 
58 #define LOGGER(level) \
59  _log->Cout(level) << log_level_name(level) << " " << __FILE__ << ":" \
60  << __LINE__ << " " << __FUNC_NAME__ << "(): "
61 
63 #define LOGDBG _log->Cout(MOORDYN_DBG_LEVEL)
65 #define LOGMSG _log->Cout(MOORDYN_MSG_LEVEL)
67 #define LOGWRN LOGGER(MOORDYN_WRN_LEVEL)
69 #define LOGERR LOGGER(MOORDYN_ERR_LEVEL)
70 
71 class MultiStream;
72 
79 {
80  public:
82  MultiStream();
84  ~MultiStream();
85 
89  inline const char* GetFile() const { return _fpath.c_str(); }
90 
98  void SetFile(const char* file_path);
99 
103  inline void SetFile(bool enable = true) { _fout_enabled = enable; };
104 
108  inline void SetTerminal(std::ostream& stream) { _terminal = &stream; };
109 
112  MultiStream& operator<<(std::ostream& (*pfun)(std::ostream&))
113  {
114  if (_fout_enabled && _fout.is_open())
115  pfun(_fout);
116  pfun(*_terminal);
117  return *this;
118  }
119 
121  std::string _fpath;
123  std::ofstream _fout;
127  std::ostream* _terminal;
128 };
129 
132 template<class T>
134 operator<<(MultiStream& st, T val)
135 {
136  if (st._fout_enabled && st._fout.is_open())
137  st._fout << val;
138  *(st._terminal) << val;
139  return st;
140 };
141 
149 {
150  public:
157  Log(const int verbosity = MOORDYN_MSG_LEVEL,
158  const int log_file_level = MOORDYN_DBG_LEVEL);
159 
162  ~Log();
163 
170  MultiStream& Cout(const int level = MOORDYN_MSG_LEVEL) const;
171 
175  inline int GetVerbosity() const { return _verbosity; }
176 
180  inline void SetVerbosity(const int verbosity) { _verbosity = verbosity; }
181 
185  inline int GetLogLevel() const { return _file_verbosity; }
186 
190  inline void SetLogLevel(const int level) { _file_verbosity = level; }
191 
195  const char* GetFile() const;
196 
206  void SetFile(const char* file_path);
207 
208  private:
210  int _verbosity;
212  int _file_verbosity;
214  MultiStream* _streamer;
215 };
216 
223 {
224  public:
231  LogUser(Log* log = NULL)
232  : _log(log)
233  {
234  }
235 
238  ~LogUser() {}
239 
243  inline void SetLogger(Log* log) { _log = log; }
244 
248  inline Log* GetLogger() const { return _log; }
249 
250  protected:
253 };
254 
255 } // ::moordyn
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A Logging utility.
Definition: Log.hpp:149
void SetVerbosity(const int verbosity)
Set the verbosity level.
Definition: Log.hpp:180
int GetLogLevel() const
Get the log file printing level.
Definition: Log.hpp:185
void SetLogLevel(const int level)
Set the log file printing level.
Definition: Log.hpp:190
int GetVerbosity() const
Get the verbosity level.
Definition: Log.hpp:175
A helper for the entities to use the logger.
Definition: Log.hpp:223
void SetLogger(Log *log)
Set the log handler.
Definition: Log.hpp:243
LogUser(Log *log=NULL)
Constructor.
Definition: Log.hpp:231
Log * GetLogger() const
Get the log handler.
Definition: Log.hpp:248
~LogUser()
Destructor.
Definition: Log.hpp:238
Log * _log
The log handler.
Definition: Log.hpp:252
Streamer able to redirect the output to several substreams.
Definition: Log.hpp:79
~MultiStream()
Destructor.
Definition: Log.cpp:85
MultiStream()
Constructor.
Definition: Log.cpp:78
std::ostream * _terminal
The terminal active streamer.
Definition: Log.hpp:127
const char * GetFile() const
Get the output file path.
Definition: Log.hpp:89
void SetTerminal(std::ostream &stream)
Set the terminal streamer.
Definition: Log.hpp:108
std::ofstream _fout
The output file streamer.
Definition: Log.hpp:123
void SetFile(bool enable=true)
Enable/disable the file printing.
Definition: Log.hpp:103
std::string _fpath
The file path.
Definition: Log.hpp:121
bool _fout_enabled
Flag to know if the file output is enabled.
Definition: Log.hpp:125
void SetFile(const char *file_path)
Set the output file path.
Definition: Log.cpp:92
MultiStream & operator<<(std::ostream &(*pfun)(std::ostream &))
Functionality for std::endl alike operators.
Definition: Log.hpp:112
#define MOORDYN_DBG_LEVEL
Debug message.
Definition: MoorDynAPI.h:126
#define MOORDYN_MSG_LEVEL
Info message.
Definition: MoorDynAPI.h:124
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
std::string log_level_name(int level)
Name the log level.
Definition: Log.cpp:39
MultiStream & operator<<(MultiStream &st, T val)
Streaming to the log file and the terminal.
Definition: Log.hpp:134