kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
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 2.16
13  *
14  * @date 30 August 2017, 11:38 (created) \n
15  * 30 August 2017, 11:38 (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 #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
45 Logger::LogLevel Logger::slogLevel = LogLevel::kBasic;
46 
47 
48 /**
49  * Initialise or change logging level.
50  */
51 void Logger::setLevel(const LogLevel actualLogLevel)
52 {
53  slogLevel = actualLogLevel;
54 }// end of setLevel
55 //----------------------------------------------------------------------------------------------------------------------
56 
57 /**
58  * Log desired activity.
59  */
60 void Logger::log(const LogLevel queryLevel,
61  const string& message)
62 {
63  if (queryLevel <= Logger::slogLevel)
64  {
65  std::cout << message;
66  }
67 }// end of log
68 //----------------------------------------------------------------------------------------------------------------------
69 
70 /**
71  * Log an error.
72  */
73 void Logger::error(const string& errorMessage)
74 {
75  std::cerr << kErrFmtHead;
76  std::cerr << errorMessage;
77  std::cerr << kErrFmtTail;
78 }// end of error
79 //----------------------------------------------------------------------------------------------------------------------
80 
81 /**
82  * Log an error and terminate the execution.
83  */
84 void Logger::errorAndTerminate(const string& errorMessage)
85 {
86  std::cerr << kErrFmtHead;
87  std::cerr << errorMessage;
88  std::cerr << kErrFmtTail;
89 
90  exit(EXIT_FAILURE);
91 }// end of errorAndTerminate
92 //----------------------------------------------------------------------------------------------------------------------
93 
94 /**
95  * Flush logger, output messages only.
96  */
97 void Logger::flush(const LogLevel queryLevel)
98 {
99  if (queryLevel <= Logger::slogLevel)
100  {
101  std::cout.flush();
102  }
103 }// end of flush
104 //----------------------------------------------------------------------------------------------------------------------
105 
106 
107 /**
108  * Wrap the line based on delimiters and align it with the rest of the logger output.
109  *
110  */
111 string Logger::wordWrapString(const string& inputString,
112  const string& delimiters,
113  const int indentation,
114  const int lineSize)
115 {
116  std::istringstream textStream(inputString);
117  string wrappedText;
118  string word;
119  string indentationString = kOutFmtVerticalLine;
120 
121 
122  // create indentation
123  for (int i = 0; i < indentation - 1; i++)
124  {
125  indentationString += ' ';
126  }
127 
128  wrappedText += kOutFmtVerticalLine + " ";
129  int spaceLeft = lineSize - 2;
130 
131  // until the text is empty
132  while (textStream.good())
133  {
134  word = getWord(textStream, delimiters);
135  if (spaceLeft < static_cast<int>(word.length()) + 3)
136  { // fill the end of the line
137  for ( ; spaceLeft > 2; spaceLeft--)
138  {
139  wrappedText += " ";
140  }
141  wrappedText += " " + kOutFmtVerticalLine + "\n" + indentationString + word;
142  spaceLeft = lineSize - (static_cast<int>(word.length()) + indentation);
143  }
144  else
145  {
146  // add the word at the same line
147  wrappedText += word;
148  spaceLeft -= static_cast<int>(word.length());
149 
150  char c;
151  if (textStream.get(c).good())
152  {
153  wrappedText += c;
154  spaceLeft--;
155  }
156  }
157  }
158 
159  // fill the last line
160  for ( ; spaceLeft > 2; spaceLeft--)
161  {
162  wrappedText += " ";
163  }
164  wrappedText += " "+ kOutFmtVerticalLine + "\n";
165 
166  return wrappedText;
167 }// end of wordWrapFileName
168 //----------------------------------------------------------------------------------------------------------------------
169 
170 //--------------------------------------------------------------------------------------------------------------------//
171 //------------------------------------------------- Private methods --------------------------------------------------//
172 //--------------------------------------------------------------------------------------------------------------------//
173 
174 /**
175  * Extract a word from a string stream based on delimiters.
176  */
177 string Logger::getWord(std::istringstream& textStream,
178  const string& delimiters)
179 {
180  string word = "";
181  char c;
182 
183  while (textStream.get(c))
184  {
185  if (delimiters.find(c) != string::npos)
186  {
187  textStream.unget();
188  break;
189  }
190  word += c;
191  }
192 
193  return word;
194 }// end of getWord
195 //----------------------------------------------------------------------------------------------------------------------
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
OutputMessage kOutFmtVerticalLine
Output message - vertical line.
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 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
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
The header file containing a class responsible for printing out info and error messages (stdout...
ErrorMessage kErrFmtTail
Error message tailer.
ErrorMessage kErrFmtHead
Error message header.
static void error(const std::string &errorMessage)
Log an error.
Definition: Logger.cpp:73