kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
Logger.h
Go to the documentation of this file.
1 /**
2  * @file Logger.h
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The header file containing a class responsible for printing out info and error messages
10  * (stdout, and stderr).
11  *
12  * @version kspaceFirstOrder3D 2.16
13  *
14  * @date 30 April 2017, 11:03 (created) \n
15  * 30 August 2017, 11:03 (revised)
16  *
17  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
18  *
19  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
20  *
21  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
22  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
23  * License, or (at your option) any later version.
24  *
25  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
26  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
27  * more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
30  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
31  */
32 
33 
34 #ifndef LOGGER_H
35 #define LOGGER_H
36 
37 #include <memory>
38 #include <iostream>
39 
40 #include <Logger/OutputMessages.h>
41 #include <Logger/ErrorMessages.h>
42 
43 /**
44  * @class Logger
45  * @brief Static class implementing the user interface by info messages.
46  * @details StaticClass used for printing out info and error message based on the verbose level. \n
47  * This is a static class.
48  */
49 class Logger
50 {
51  public:
52 
53  /**
54  * @enum LogLevel
55  * @brief Log level of the message.
56  * @details A enum to specify at which log level the message should be displayed, or the level setd.
57  */
58  enum class LogLevel
59  {
60  /// Basic (default) level of verbosity
61  kBasic = 0,
62  /// Advanced level of verbosity
63  kAdvanced = 1,
64  /// Full level of verbosity
65  kFull = 2,
66  };
67 
68  /// Default constructor is not allowed, static class.
69  Logger() = delete;
70  /// Copy constructor is not allowed, static class.
71  Logger(const Logger&) = delete;
72  /// Destructor is not allowed, static class.
73  ~Logger() = delete;
74 
75  /// Operator= is not allowed, static class.
76  Logger& operator=(const Logger&) = delete;
77 
78  /**
79  * @brief Set the log level.
80  * @param [in] actualLogLevel - Log level for the logger.
81  */
82  static void setLevel(const LogLevel actualLogLevel);
83  /**
84  * Get current log level.
85  * @return Current log level.
86  */
87  static LogLevel getLevel() {return slogLevel;};
88 
89  /**
90  * @brief Log desired activity for a given log level, version with string format.
91  *
92  * @param [in] queryLevel - What level to use.
93  * @param [in] format - Format string.
94  * @param [in] args - Arguments, std::string is not accepted.
95  */
96  template<typename... Args>
97  static void log(const LogLevel queryLevel,
98  const std::string& format,
99  Args ... args)
100  {
101  if (queryLevel <= Logger::slogLevel)
102  {
103  std::cout << formatMessage(format, args ...);
104  }
105  }// end of log
106 
107  /**
108  * @brief Log desired activity for a given log level.
109  * @param [in] queryLevel - Log level of the message.
110  * @param [in] message - Message to log.
111  */
112  static void log(const LogLevel queryLevel,
113  const std::string& message);
114  /**
115  * @brief Log an error.
116  * @param [in] errorMessage - Error message to be printed out.
117  */
118  static void error(const std::string& errorMessage);
119  /**
120  * @brief Log an error and terminate the execution.
121  * @param [in] errorMessage - error message to be printed to stderr.
122  */
123  static void errorAndTerminate(const std::string& errorMessage);
124  /**
125  * @brief Flush output messages.
126  * @param [in] queryLevel - Log level of the flush.
127  */
128  static void flush(const LogLevel queryLevel);
129 
130  /**
131  * Wrap the line based on delimiters and align it with the rest of the logger output.
132  *
133  * @param [in] inputString - Input string.
134  * @param [in] delimiters - String of delimiters, every char is a delimiter.
135  * @param [in] indentation - Indentation from the beginning.
136  * @param [in] lineSize - Line size.
137  * @return Wrapped string.
138  *
139  * @note The string must not contain tabulator and end-of-line characters.
140  */
141  static std::string wordWrapString(const std::string& inputString,
142  const std::string& delimiters,
143  const int indentation = 0,
144  const int lineSize = 65);
145 
146  /**
147  * @brief C++-11 replacement for sprintf that works with std::string instead of char*
148  * @details The routine was proposed at
149  * http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
150  * and should work with both Linux and VS 2015.
151  * However it still does not support string in formated arguments
152  * @param [in] format - Format string.
153  * @param [in] args - Arguments, std::string is not accepted.
154  * @return Formated string.
155  */
156  template<typename ... Args>
157  static std::string formatMessage(const std::string& format,
158  Args ... args)
159  {
160  // when the size is 0, the routine returns the size of the formated string
161  size_t size = snprintf(nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
162 
163  std::unique_ptr<char[]> buf(new char[size]);
164  snprintf(buf.get(), size, format.c_str(), args ... );
165  return std::string(buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
166  }
167 
168  private:
169  /**
170  * @brief Extract a word from a string stream based on delimiters.
171  *
172  * @param [in,out] textStream - Input text stream.
173  * @param [in] delimiters - List of delimiters as a single string.
174  * @return A word from the string.
175  */
176  static std::string getWord(std::istringstream& textStream,
177  const std::string& delimiters);
178 
179 
180  /// Log level of the logger
182 
183 }; // Logger
184 //----------------------------------------------------------------------------------------------------------------------
185 
186 #endif /* LOGGER_H */
187 
static void setLevel(const LogLevel actualLogLevel)
Set the log level.
Definition: Logger.cpp:51
static LogLevel slogLevel
Log level of the logger.
Definition: Logger.h:181
static std::string getWord(std::istringstream &textStream, const std::string &delimiters)
Extract a word from a string stream based on delimiters.
Definition: Logger.cpp:177
static void log(const LogLevel queryLevel, const std::string &format, Args ... args)
Log desired activity for a given log level, version with string format.
Definition: Logger.h:97
Static class implementing the user interface by info messages.
Definition: Logger.h:49
Advanced level of verbosity.
static std::string wordWrapString(const std::string &inputString, const std::string &delimiters, const int indentation=0, const int lineSize=65)
Definition: Logger.cpp:111
static void flush(const LogLevel queryLevel)
Flush output messages.
Definition: Logger.cpp:97
~Logger()=delete
Destructor is not allowed, static class.
static void errorAndTerminate(const std::string &errorMessage)
Log an error and terminate the execution.
Definition: Logger.cpp:84
LogLevel
Log level of the message.
Definition: Logger.h:58
static std::string formatMessage(const std::string &format, Args ... args)
C++-11 replacement for sprintf that works with std::string instead of char*.
Definition: Logger.h:157
The header file containing routines for error messages and error messages common for both linux and w...
The header file including output messages based on the operating system.
Full level of verbosity.
static LogLevel getLevel()
Definition: Logger.h:87
Basic (default) level of verbosity.
Logger & operator=(const Logger &)=delete
Operator= is not allowed, static class.
static void error(const std::string &errorMessage)
Log an error.
Definition: Logger.cpp:73
Logger()=delete
Default constructor is not allowed, static class.