kspaceFirstOrder3D-CUDA  1.1
The CUDA/C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
10  * info and error messages (stdout, and stderr).
11  *
12  * @version kspaceFirstOrder3D 3.4
13  *
14  * @date 19 April 2016, 12:52 (created) \n
15  * 10 August 2016, 16:44 (revised)
16  *
17  * @section License
18  * This file is part of the C++ extension of the k-Wave Toolbox
19  * (http://www.k-wave.org).\n Copyright (C) 2016 Jiri Jaros and Bradley Treeby.
20  *
21  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify
22  * it under the terms of the GNU Lesser General Public License as published by the Free Software
23  * Foundation, either version 3 of the 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
26  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
27  * General Public License for 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/.
31  */
32 
33 
34 #ifndef TLOGGER_H
35 #define TLOGGER_H
36 
37 #include <memory>
38 #include <iostream>
39 #include <cuda_runtime.h>
40 
41 #include <Logger/OutputMessages.h>
42 #include <Logger/ErrorMessages.h>
43 
44 /**
45  * @class TLogger
46  * @brief Static class implementing the user interface by info messages.
47  * @details StaticClass used for printing out info and error message based on the
48  * verbose level. This is a static class.
49  */
50 class TLogger
51 {
52  public:
53 
54  /**
55  * @enum TLogLevel
56  * @brief Log level of the message.
57  * @details A enum to specify at which log level the message should be displayed, or the level
58  * set.
59  */
60  enum TLogLevel
61  {
62  /// Basic (default) level of verbosity
63  BASIC = 0,
64  /// Advanced level of verbosity
65  ADVANCED = 1,
66  /// Full level of verbosity
67  FULL = 2,
68  };
69 
70 
71  /// Set the log level.
72  static void SetLevel(const TLogLevel actualLogLevel);
73  /// Get the log level.
74  static TLogLevel GetLevel() {return logLevel;};
75 
76  /**
77  * @brief Log desired activity for a given log level, version with string format.
78  * @details Log desired activity and format it using format message.
79  *
80  * @param [in] queryLevel - What level to use
81  * @param [in] format - Format string
82  * @param [in] args - Arguments, std::string is not accepted
83  */
84  template<typename... Args>
85  static void Log(const TLogLevel queryLevel,
86  const std::string& format,
87  Args ... args)
88  {
89  if (queryLevel <= TLogger::logLevel)
90  {
91  std::cout << FormatMessage(format, args ...);
92  }
93  }// end of Log
94 
95 
96  /// Log desired activity for a given log level.
97  static void Log(const TLogLevel queryLevel,
98  const std::string& message);
99 
100  /// Log an error.
101  static void Error(const std::string& errorMessage);
102 
103  /// Log an error and terminate the execution
104  static void ErrorAndTerminate(const std::string& errorMessage);
105 
106  /// Flush output messages.
107  static void Flush(const TLogLevel queryLevel);
108 
109  /// Wrap the line based on logger conventions
110  static std::string WordWrapString(const std::string& inputString,
111  const std::string& delimiters,
112  const int indentation = 0,
113  const int lineSize = 65);
114 
115  /**
116  * @brief C++-11 replacement for sprintf that works with std::string instead of char *
117  * @details The routine was proposed at
118  * http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
119  * and should work with both Linux and VS 2013.
120  * However it still does not support string in formated arguments
121  * @param [in] format - Format string
122  * @param [in] args - Arguments, std::string is not accepted
123  * @return formated string
124  */
125  template<typename ... Args>
126  static std::string FormatMessage(const std::string& format, Args ... args)
127  {
128  // Windows build
129  #ifdef __linux__
130  /// when the size is 0, the routine returns the size of the formated string
131  size_t size = snprintf(nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
132 
133  std::unique_ptr<char[]> buf( new char[ size ] );
134  snprintf(buf.get(), size, format.c_str(), args ... );
135  return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
136  #endif
137 
138  // Windows build
139  #ifdef _WIN64
140  size_t size = _snprintf (nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
141 
142  std::unique_ptr<char[]> buf( new char[ size ] );
143  _snprintf (buf.get(), size, format.c_str(), args ... );
144  return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
145  #endif
146  }
147  private:
148  /// Default constructor is not allowed, static class
149  TLogger();
150  /// Copy constructor is not allowed, static class
151  TLogger(const TLogger& orig);
152  /// Destructor is not allowed, static class
153  ~TLogger();
154 
155  /// Log level of the logger
157 
158  private:
159  /// Extract a word (string between two delimiters)
160  static std::string GetWord(std::istringstream& textStream,
161  const std::string& delimiters);
162 
163 }; // TLogger
164 
165 
166 
167 //------------------------------------------------------------------------------------------------//
168 //------------------------------------ Routines --------------------------------------------------//
169 //------------------------------------------------------------------------------------------------//
170 
171 /**
172  *@brief Checks CUDA errors, create an error message and throw an exception.
173  *@details Checks CUDA errors, create an error message and throw an exception. The template
174  * parameter should be set to true for the whole code when debugging kernel related errors.
175  * Setting it to true for production run will cause IO sampling and storing not to be overlapped.
176  *
177  * @param [in] errorCode - Error produced by a cuda routine
178  * @param [in] routineName - Function where the error happened
179  * @param [in] fileName - File where the error happened
180  * @param [in] lineNumber - Line where the error happened
181  */
182 template <bool forceSynchronisation = false>
183 inline void CheckErrors(const cudaError_t errorCode,
184  const char* routineName,
185  const char* fileName,
186  const int lineNumber)
187 {
188  if (forceSynchronisation)
189  {
190  cudaDeviceSynchronize();
191  }
192 
193  if (errorCode != cudaSuccess)
194  {
195  // Throw exception
196  throw std::runtime_error(TLogger::FormatMessage(ERR_FMT_GPU_ERROR,
197  cudaGetErrorString(errorCode),
198  routineName,
199  fileName,
200  lineNumber));
201  }
202 }// end of cudaCheckErrors
203 //--------------------------------------------------------------------------------------------------
204 
205 /**
206  * @brief Macro checking cuda errors and printing the file name and line. Inspired by CUDA common
207  * checking routines.
208  */
209 #define checkCudaErrors(val) CheckErrors ( (val), #val, __FILE__, __LINE__ )
210 
211 
212 #endif /* TLOGGER_H */
213 
Static class implementing the user interface by info messages.
Definition: Logger.h:50
static std::string WordWrapString(const std::string &inputString, const std::string &delimiters, const int indentation=0, const int lineSize=65)
Wrap the line based on logger conventions.
Definition: Logger.cpp:130
static TLogLevel logLevel
Log level of the logger.
Definition: Logger.h:156
static TLogLevel GetLevel()
Get the log level.
Definition: Logger.h:74
TErrorMessage ERR_FMT_GPU_ERROR
CUDAParameters error message.
static void SetLevel(const TLogLevel actualLogLevel)
Set the log level.
Definition: Logger.cpp:53
static void Error(const std::string &errorMessage)
Log an error.
Definition: Logger.cpp:81
static void ErrorAndTerminate(const std::string &errorMessage)
Log an error and terminate the execution.
Definition: Logger.cpp:94
TLogger()
Default constructor is not allowed, static class.
Full level of verbosity.
Definition: Logger.h:67
static std::string GetWord(std::istringstream &textStream, const std::string &delimiters)
Extract a word (string between two delimiters)
Definition: Logger.cpp:200
TLogLevel
Log level of the message.
Definition: Logger.h:60
static void Flush(const TLogLevel queryLevel)
Flush output messages.
Definition: Logger.cpp:109
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.
Basic (default) level of verbosity.
Definition: Logger.h:63
void CheckErrors(const cudaError_t errorCode, const char *routineName, const char *fileName, const int lineNumber)
Checks CUDA errors, create an error message and throw an exception.
Definition: Logger.h:183
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:126
Advanced level of verbosity.
Definition: Logger.h:65
static void Log(const TLogLevel queryLevel, const std::string &format, Args...args)
Log desired activity for a given log level, version with string format.
Definition: Logger.h:85
~TLogger()
Destructor is not allowed, static class.