DebConfig.hh 2.41 KB
Newer Older
1
// (C) Copyright 2020 by Autodesk, Inc.
2
3
4
5
6

#ifndef BASE_DEBCONFIG_HH_INCLUDED
#define BASE_DEBCONFIG_HH_INCLUDED
#ifdef DEB_ON

Max Lyon's avatar
Max Lyon committed
7
#include <Base/Config/BaseDefines.hh>
8
9
#include <string>

10
11
namespace Debug
{
12

13
void print_char_to_cerr(const char _c); //!< print a char to cerr
14

15
/*!
16
Access the global, per-process, configuration options of the Debug system.
17
\todo Make this a per-thread configuration.
18
*/
Max Lyon's avatar
Max Lyon committed
19
class BASEDLLEXPORT Config
20
21
{
public:
22
23
24
25
26
  //! Define the function type to print a character on the console
  typedef void (*print_function)(const char);

public:
  //! Modify the current configuration.
27
  static Config& modify();
28

29
  //! Query the current configuration.
30
  static const Config& query();
31
32

  //! Query the default configuration.
33
  static const Config& defaults();
34

35
public:
36
37
  //! The output level for all code in the absence of a config file.
  int output_level = 5;
38

39
40
  //! The deb out log filename, nullptr disables the debug output log file.
  const char* log_filename = nullptr;
41

42
43
44
  //! Get if the log file output is enabled
  bool logfile() const { return log_filename != nullptr; }

45
46
  //! Function to deb out on the console, nullptr if output disabled.
  print_function console_print = print_char_to_cerr;
47

48
49
50
  //! Get if the console
  bool console() const { return console_print != nullptr; }

51
52
53
public:
  //! The output level for the given filename and function.
  int custom_level(const char* const _flnm, const char* const _fnct) const;
54

55
  /*!
56
  Load the configuration file specified either by the environment variable
57
  or the filename if the the environment variable is not set.
58
59
  \todo Document the config format.
  \return true if the configuration file was loaded properly, false otherwise.
60
  */
61
  bool load(const char* const _cnfg_envr, const char* const _cnfg_flnm);
62

63
64
private:
  class LevelFilterMap;
65
66

private:
67
  LevelFilterMap* lvl_fltrs_ = nullptr;
68
69
70
71
72
73
74
75
76
77
78
79
80

private:
  //! Private constructor
  Config();

  //! Private destructor
  ~Config();

  //! Disable copy
  Config(const Config&);

  //! Disable assignment
  Config& operator=(const Config&);
81
};
82

83
84
85
86
87
88
89
90
/// Helper struct to locally set the global output level and reset it when leaving the scope
struct ScopedOutputLevel
{
  ScopedOutputLevel(int level) : lvl_bfre_(Config::query().output_level) { Config::modify().output_level = level; }
  ~ScopedOutputLevel() { Config::modify().output_level = lvl_bfre_; }
  int lvl_bfre_;
};

91
}; // namespace Debug
92

93
94
#endif // DEB_ON
#endif // BASE_DEBCONFIG_HH_INCLUDED