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.cpp
Go to the documentation of this file.
1 /**
2  * @file Logger.cpp
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 implementation 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, 11:52 (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 #include <string>
34 #include <sstream>
35 
36 #include <Logger/Logger.h>
37 
38 using std::string;
39 
40 //------------------------------------------------------------------------------------------------//
41 //--------------------------------------- Public methods -----------------------------------------//
42 //------------------------------------------------------------------------------------------------//
43 
44 /// static declaration of the LogLevel private field
46 
47 
48 /**
49  * Initialise or change logging level.
50  *
51  * @param [in] actualLogLevel - Log level for the logger
52  */
53 void TLogger::SetLevel(const TLogLevel actualLogLevel)
54 {
55  logLevel = actualLogLevel;
56 }/// end of SetLevel
57 //--------------------------------------------------------------------------------------------------
58 
59 
60 /**
61  * Log desired activity.
62  *
63  * @param [in] queryLevel - Log level of the message
64  * @param [in] message - Message to log
65  */
66 void TLogger::Log(const TLogLevel queryLevel,
67  const string& message)
68 {
69  if (queryLevel <= TLogger::logLevel)
70  {
71  std::cout << message;
72  }
73 }// end of Log
74 //--------------------------------------------------------------------------------------------------
75 
76 /**
77  * Log an error.
78  *
79  * @param [in] errorMessage - Error message to be printed out.
80  */
81 void TLogger::Error(const string& errorMessage)
82 {
83  std::cerr << ERR_FMT_HEAD;
84  std::cerr << errorMessage;
85  std::cerr << ERR_FMT_TAIL;
86 }// end of Error
87 //--------------------------------------------------------------------------------------------------
88 
89 /**
90  * Log an error and terminate the execution.
91  *
92  * @param [in] errorMessage - error message to be printed to stderr
93  */
94 void TLogger::ErrorAndTerminate(const string& errorMessage)
95 {
96  std::cerr << ERR_FMT_HEAD;
97  std::cerr << errorMessage;
98  std::cerr << ERR_FMT_TAIL;
99 
100  exit(EXIT_FAILURE);
101 }// end of ErrorAndTerminate
102 //--------------------------------------------------------------------------------------------------
103 
104 /**
105  * Flush logger, output messages only.
106  *
107  * @param [in] queryLevel - Log level of the flush
108  */
109 void TLogger::Flush(const TLogLevel queryLevel)
110 {
111  if (queryLevel <= TLogger::logLevel)
112  {
113  std::cout.flush();
114  }
115 }// end of Flush
116 //--------------------------------------------------------------------------------------------------
117 
118 
119 /**
120  * Wrap the line based on delimiters and align it with the rest of the logger output.
121  *
122  * @param [in] inputString - Input string
123  * @param [in] delimiters - String of delimiters, every char is a delimiter
124  * @param [in] indentation - Indentation from the beginning
125  * @param [in] lineSize - Line size
126  * @return Wrapped string
127  *
128  * @note The string must not contain tabulator and end-of-line characters.
129  */
130 string TLogger::WordWrapString(const string& inputString,
131  const string& delimiters,
132  const int indentation,
133  const int lineSize)
134 {
135  std::istringstream textStream(inputString);
136  string wrappedText;
137  string word;
138  string indentationString = OUT_FMT_VERTICAL_LINE;
139 
140 
141  // create indentation
142  for (int i = 0; i < indentation - 1; i++)
143  {
144  indentationString += ' ';
145  }
146 
147  wrappedText += OUT_FMT_VERTICAL_LINE + " ";
148  int spaceLeft = lineSize - 2;
149 
150  // until the text is empty
151  while (textStream.good())
152  {
153  word = GetWord(textStream, delimiters);
154  if (spaceLeft < (int) word.length() + 3)
155  { // fill the end of the line
156  for ( ; spaceLeft > 2; spaceLeft--)
157  {
158  wrappedText += " ";
159  }
160  wrappedText += " " + OUT_FMT_VERTICAL_LINE + "\n" + indentationString + word;
161  spaceLeft = lineSize - (word.length() + indentation);
162  }
163  else
164  {
165  // add the word at the same line
166  wrappedText += word;
167  spaceLeft -= word.length();
168 
169  char c;
170  if (textStream.get(c).good())
171  {
172  wrappedText += c;
173  spaceLeft--;
174  }
175  }
176  }
177 
178  // fill the last line
179  for ( ; spaceLeft > 2; spaceLeft--)
180  {
181  wrappedText += " ";
182  }
183  wrappedText += " "+ OUT_FMT_VERTICAL_LINE + "\n";
184 
185  return wrappedText;
186 }// end of WordWrapFileName
187 //--------------------------------------------------------------------------------------------------
188 
189 //------------------------------------------------------------------------------------------------//
190 //--------------------------------------- Private methods ----------------------------------------//
191 //------------------------------------------------------------------------------------------------//
192 
193 /**
194  * Extract a word from a string stream based on delimiters.
195  *
196  * @param [in,out] textStream - Input text stream
197  * @param [in] delimiters - List of delimiters as a single string
198  * @return A word firm the string
199  */
200 string TLogger::GetWord(std::istringstream& textStream,
201  const string& delimiters)
202 {
203  string word = "";
204  char c;
205 
206  while (textStream.get(c))
207  {
208  if (delimiters.find(c) != string::npos)
209  {
210  textStream.unget();
211  break;
212  }
213  word += c;
214  }
215 
216  return word;
217 }// end of GetWord
218 //--------------------------------------------------------------------------------------------------
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 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
static std::string GetWord(std::istringstream &textStream, const std::string &delimiters)
Extract a word (string between two delimiters)
Definition: Logger.cpp:200
The header file containing a class responsible for printing out info and error messages (stdout...
TLogLevel
Log level of the message.
Definition: Logger.h:60
static void Flush(const TLogLevel queryLevel)
Flush output messages.
Definition: Logger.cpp:109
TOutputMessage OUT_FMT_VERTICAL_LINE
Output message - vertical line.
TErrorMessage ERR_FMT_HEAD
Error message header.
TErrorMessage ERR_FMT_TAIL
Error message tailer.
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