reportsystem.h
Go to the documentation of this file.
1 /* *****************************************************************************
2 
3  trimAl v2.0: a tool for automated alignment trimming in large-scale
4  phylogenetics analyses.
5 
6  readAl v2.0: a tool for automated alignment conversion among different
7  formats.
8 
9  2009-2019
10  Fernandez-Rodriguez V. (victor.fernandez@bsc.es)
11  Capella-Gutierrez S. (salvador.capella@bsc.es)
12  Gabaldon, T. (tgabaldon@crg.es)
13 
14  This file is part of trimAl/readAl.
15 
16  trimAl/readAl are free software: you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation, the last available version.
19 
20  trimAl/readAl are distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with trimAl/readAl. If not, see <http://www.gnu.org/licenses/>.
27 
28 ***************************************************************************** */
29 
30 #ifndef VERBOSEMANAGER_H
31 #define VERBOSEMANAGER_H
32 
33 #include <iostream>
34 #include <cstring>
35 #include <iomanip>
36 #include <vector>
37 #include <array>
38 #include <map>
39 
40 
41 /**
42  * \brief VerboseLevel used to report messages.
43  */
45 
46  /// 1 = Info, warning and error messages
47  INFO = 1,
48  /// 2 = Error and warning messages
49  WARNING = 2,
50  /// 3 = Only error messages
51  ERROR = 3,
52  /// 4 = No output messages
53  NONE = 4,
54 };
55 
56 enum ErrorCode {
57 
59 
61 
63 
65 
67 
70 
73 
76 
79 
82 
85 
88 
91 
94 
97 
100 
103 
106 
109 
111 
113 
118 
120 
123 
128 
131 
133 
135 
137 
139 
141 
143 
145 
147 
149 
151 
153 
154 
155 
157 
159 
161 
163 
165 
167 
169 
171 
173 
175 
177 
179 
181 
183 
185 
187 
189 
191 
193 
195 
197 
199 
201 
203 
205 
207 
209 
211 
213 
215 
217 
219 
221 
226 
228 
230 
232  MultipleInputs = 97, // Not in use
233 
235 
237 
238 
240 
243 
246 
248 
250 
255 
257 
258 
261 
262 
264 };
265 
268 
270 
272 
274 
276 
278 
280 
282 
284 
286 
287 
289 };
290 
291 enum InfoCode {
293 
295 
297 
299 
300 
302 };
303 
304 /// \brief Internal classes to handle reporting to user in several ways.\n
305 /// The reporting system is made so a developer that wants to use the system
306 /// doesn't need to enter to the namespace, but use the global variable #debug
307 namespace reporting {
308 
309 /**
310  \brief Internal class used by reporting::reportManager \n
311  This class serves as a proxy for message reporting. \n
312  It will only output to std::cout if
313  reporting::reportWrapper::CanReport it's true
314  */
316  /// \brief Variable that specifies if the object is able to output to cout or not.
317  bool CanReport;
318 
319  public:
320 
321  /// \brief Constructor
322  explicit reportWrapper(bool CanReport) : CanReport{CanReport} { }
323 
324  /// \brief Overloaded operator that allows to use this object as
325  /// it was an iostream, as cout.
326  template <typename T>
327  reportWrapper &operator<<(const T &a) {
328  if (CanReport)
329  std::cout<<a;
330  return *this;
331  }
332 
333  /// \brief Overloaded operator that allows to use this object as
334  /// it was an iostream, as cout.
335  reportWrapper &operator<<(std::ostream& (*pf) (std::ostream&)) {
336  if (CanReport)
337  std::cout<<pf;
338  return *this;
339  }
340 };
341 
342 /**
343  \brief Class that allows us to centralize all the reporting messages that
344  should be used to inform the user of Errors, Warnings and Info.\n
345  The object can also be used as a substitute to cout for temporal messages using
346  the << overloaded operator or the \link reportManager::log log \endlink method,
347  and the messages will be behind the IsDebug variable,
348  which should be set to false in Release.\n
349  This allows us to protect the user from receiving Debug/Developing messages
350  that shouldn't bother them, in case any of them is not removed by error.
351  \warning <b> THIS CLASS SHOULDN'T BE USED DIRECTLY </b> \n
352  Use instanced global variable \link debug debug \endlink instead.
353  */
355 {
356 private:
357 
358  /** \brief Object that will be returned by
359  \link reporting::reportManager::log log \endlink
360  if it allows to output the message
361  */
363  /** \brief Object that will be returned by
364  * \link reporting::reportManager::log log \endlink
365  * if it doesn't allow to output the message
366  * */
368 
369  static const std::map<InfoCode, const char *> InfoMessages ;
370  static const std::map<WarningCode, const char *> WarningMessages ;
371  static const std::map<ErrorCode, const char *> ErrorMessages ;
372 
373 public:
374  /** \brief Level of Verbosity.\n
375  * The report system won't output messages
376  * that are lower than the current level */
378 
379  /** \brief Protection variable.
380  This variable should be set to true while in development,
381  and false on Release. */
382  bool IsDebug = false;
383 
384  /**
385  \brief Method to print all Info, Warning and Error codes
386  and their respective message.\n
387  This method is useful to check whether all codes
388  have a message linked to them.
389  It will use the current VerboseLevel of the object,
390  which defaults to ERROR level.
391  */
392  void PrintCodesAndMessages();
393 
394  /**
395  \brief Method to report an Error. \n
396  It will be displayed if
397  \link reporting::reportManager::Level Level \endlink
398  is equal or higher to VerboseLevel::ERROR
399  \param message
400  Code to report.
401  \param vars
402  Array of strings that will replace the '[tags]' in the message.\n
403  The number of elements in the array must be the same as
404  [tag] occurrences on the message.\n
405  <b> The method will take care of destroying the array </b>
406  */
407  void report(ErrorCode message, std::string * vars = nullptr);
408 
409  /**
410  \brief Method to report an Error.\n
411  It will be displayed if
412  \link reporting::reportManager::Level Level \endlink
413  is equal or higher to VerboseLevel::ERROR
414  \param message
415  Code to report.
416  \param vars
417  Array of chars that will replace the first occurrence of
418  '[tags]' in the message. \n
419  <b> The method wont take care of destroying the array </b>\n
420  This allows to reuse the parameter passed.
421  */
422  void report(ErrorCode message, const char *vars);
423  /**
424  \brief Method to report a Warning. \n
425  It will be displayed if
426  \link reporting::reportManager::Level Level \endlink
427  is equal or higher to VerboseLevel::WARNING
428  \param message
429  Code to report.
430  \param vars
431  Array of strings that will replace the '[tags]' in the message.\n
432  The number of elements in the array must be the same as
433  [tag] occurrences on the message.\n
434  <b> The method will take care of destroying the array </b>
435  */
436  void report(WarningCode message, std::string * vars = nullptr);
437  /**
438  \brief Method to report a Warning.\n
439  It will be displayed if
440  \link reporting::reportManager::Level Level \endlink
441  is equal or higher to VerboseLevel::WARNING
442  \param message
443  Code to report.
444  \param vars
445  Array of chars that will replace the first occurrence of
446  '[tags]' in the message. \n
447  <b> The method wont take care of destroying the array </b>\n
448  This allows to reuse the parameter passed.
449  */
450  void report(WarningCode message, const char *vars);
451  /**
452  \brief Method to report an Info message. \n
453  It will be displayed if
454  \link reporting::reportManager::Level Level \endlink
455  is equal or higher to VerboseLevel::INFO
456  \param message
457  Code to report.
458  \param vars
459  Array of strings that will replace the '[tags]' in the message.\n
460  The number of elements in the array must be the same as
461  [tag] occurrences on the message.\n
462  <b> The method will take care of destroying the array </b>
463  */
464  void report(InfoCode message, std::string * vars = nullptr);
465  /**
466  \brief Method to report an Info message.\n
467  It will be displayed if
468  \link reporting::reportManager::Level Level \endlink
469  is equal or higher to VerboseLevel::WARNING
470  \param message
471  Code to report.
472  \param vars
473  Array of chars that will replace the first occurrence of
474  '[tags]' in the message. \n
475  <b> The method wont take care of destroying the array </b>\n
476  This allows to reuse the parameter passed.
477  */
478  void report(InfoCode message, const char *vars);
479 
480  /**
481  \brief Method to output a message behind two checks:
482  \link reporting::reportManager::IsDebug IsDebug \endlink
483  and VerboseLevel passed.\n
484  If \link reporting::reportManager::IsDebug IsDebug \endlink is false,
485  it will ignore the message returning the
486  \link reporting::reportManager::cantReport cantReport \endlink,
487  otherwise, VerboseLevel level will be checked.
488  \param level
489  Level to use for this message.\n
490  If \link reporting::reportManager::Level Level \endlink is higher
491  than this argument, message will be ignored,
492  otherwise, message will be outputted to cout.
493  */
495  {
496  if (!IsDebug) return cantReport;
497 
498  if (level >= Level)
499  return canReport;
500  else
501  return cantReport;
502  }
503 
504  /**
505  \brief Overloaded operator that allows us to debug some messages behind
506  \link reporting::reportManager::IsDebug IsDebug \endlink
507  \param a
508  Message to be outputed if
509  \link reporting::reportManager::IsDebug IsDebug \endlink
510  */
511  template <typename T>
512  reportManager &operator<<(const T &a) {
513  if (IsDebug)
514  std::cout<<a;
515  return *this;
516  }
517 
518  /**
519  \brief Overloaded operator that allows us to debug some messages behind
520  \link reporting::reportManager::IsDebug IsDebug \endlink
521  \param pf
522  Object that overloads the '<<' operator
523  */
524  reportManager &operator<<(std::ostream& (*pf) (std::ostream&)) {
525  if (IsDebug)
526  std::cout<<pf;
527  return *this;
528  }
529 };
530 
531 }
532 /** \brief <b> This instance is the one that should be used</b> \n
533  * It's use is similar to a singleton, without the need to obtain
534  * the instance every time it's needed.\n
535  * Instead, it's a global instance of the reporting::reportManager \n
536  * This allows us to have the '<<' operator overloaded,
537  * as it can't be statically overloaded.
538  \relates reporting::reportManager
539  */
541 
542 #endif // VERBOSEMANAGER_H
static const std::map< WarningCode, const char * > WarningMessages
Definition: reportsystem.h:370
Class that allows us to centralize all the reporting messages that should be used to inform the user ...
Definition: reportsystem.h:354
void report(WarningCode message, std::string *vars=nullptr)
Method to report a Warning. It will be displayed if Level is equal or higher to VerboseLevel::WARNI...
reportWrapper log(VerboseLevel level)
Method to output a message behind two checks: IsDebug and VerboseLevel passed. If IsDebug is false...
Definition: reportsystem.h:494
1 = Info, warning and error messages
Definition: reportsystem.h:47
bool IsDebug
Protection variable. This variable should be set to true while in development, and false on Release...
Definition: reportsystem.h:382
void report(InfoCode message, const char *vars)
Method to report an Info message. It will be displayed if Level is equal or higher to VerboseLevel::...
3 = Only error messages
Definition: reportsystem.h:51
4 = No output messages
Definition: reportsystem.h:53
void report(WarningCode message, const char *vars)
Method to report a Warning. It will be displayed if Level is equal or higher to VerboseLevel::WARNIN...
Internal classes to handle reporting to user in several ways. The reporting system is made so a devel...
Definition: reportsystem.h:307
VerboseLevel Level
Level of Verbosity. The report system won&#39;t output messages that are lower than the current level...
Definition: reportsystem.h:377
reportWrapper(bool CanReport)
Constructor.
Definition: reportsystem.h:322
VerboseLevel
VerboseLevel used to report messages.
Definition: reportsystem.h:44
void PrintCodesAndMessages()
Method to print all Info, Warning and Error codes and their respective message. This method is useful...
static const std::map< InfoCode, const char * > InfoMessages
Definition: reportsystem.h:369
2 = Error and warning messages
Definition: reportsystem.h:49
bool CanReport
Variable that specifies if the object is able to output to cout or not.
Definition: reportsystem.h:317
void report(ErrorCode message, const char *vars)
Method to report an Error. It will be displayed if Level is equal or higher to VerboseLevel::ERROR.
ErrorCode
Definition: reportsystem.h:56
reportWrapper cantReport
Object that will be returned by log if it doesn&#39;t allow to output the message.
Definition: reportsystem.h:367
reporting::reportManager debug
void report(InfoCode message, std::string *vars=nullptr)
Method to report an Info message. It will be displayed if Level is equal or higher to VerboseLevel:...
WarningCode
Definition: reportsystem.h:266
static const std::map< ErrorCode, const char * > ErrorMessages
Definition: reportsystem.h:371
void report(ErrorCode message, std::string *vars=nullptr)
Method to report an Error. It will be displayed if Level is equal or higher to VerboseLevel::ERROR...
reportWrapper canReport
Object that will be returned by log if it allows to output the message.
Definition: reportsystem.h:362
InfoCode
Definition: reportsystem.h:291
Internal class used by reporting::reportManager This class serves as a proxy for message reporting...
Definition: reportsystem.h:315