DebOut.hh 5.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// (C) Copyright 2014 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk,  Inc.,  and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of  this information  by  anyone  other  than  authorized
// employees of Autodesk, Inc.  is granted  only under a written
// non-disclosure agreement,  expressly  prescribing  the  scope
// and manner of such use.

11
12
#ifndef BASE_DEBOUT_HH_INCLUDED
#define BASE_DEBOUT_HH_INCLUDED
13

14
// DEB_ON is defined, or not, in CMakeLists.txt for primary project
15
#ifndef DEB_ON
16

17
18
#define DEB_module(SS)
#define DEB_enter_func
19
#define DEB_only(CC)
20
21
#define DEB_exec(LL, AA) {}
#define DEB_exec_if(CC, LL, AA) {}
22
23
24
25
26
27
28
#define DEB_out(LL, AA) {}
#define DEB_out_if(CC, LL, AA) {}
#define DEB_line(LL, AA) {}
#define DEB_line_if(CC, LL, AA) {}
#define DEB_warning(LL, AA) {}
#define DEB_warning_if(CC, LL, AA) {}
#define DEB_mesh_if(CC, LL, FF, MM) {}
29

30
31
#define DEB_error(AA) {}
#define DEB_error_if(CC, AA) {}
32

33
#else // DEB_ON
34

35
36
#include "Base/Utils/IOutputStream.hh"

37
#include <string>
38
#include <vector>
39
#include <array>
40

41
namespace Debug {
42

43
44
typedef unsigned int uint;

45
const int INVALID_LEVEL = -1;
46
47
const char* const ERROR = "ERROR";
const char* const WARNING = "WARNING";
48

49
class Stream;
50

51
// Class used to maintain Stream's CallStack
52
53
// This does not currently contain a pointer to the file streamer used
// but might do in future.
54
class Enter
55
56
{
public:
57
58
59
  const char* flnm_;  //!< File name for this DEB_enter
  int nmbr_; //!< deb_nmbr for this function.
  int lvl_; //!< deb_level for this function.
60
  int id_;       /*!< Unique identifier for this Enter (used in ANCHORS) */
61
  int outs_; /*!< Number of DEB_outs encountered within this function
62
63
                    determining whether a given DEB_out should include or omit
                    a call stack or exit trace. */
64
  int lns_; /*!< Number of call stack indents including this call. */
65

66
  Enter(const char* const _flnm, const char* const _fnct, int& _nmbr, int& _lvl);
67
  ~Enter();
68
69

  //! pass the output on the level or not?
70
  bool pass(const int _lvl) const { return _lvl <= lvl_; }
71

72
  Stream& stream();
73

74
75
76
  Base::Command end() const { return Base::Command::END; }
  Base::Command end_lf() const { return Base::Command::END_LF; }
  Base::Command end_err() const { return Base::Command::END_ERR; }
77
};
78

79
//! This is a private implementation for Stream
80
class File;
81

82
class Stream : public Base::IOutputStream
83
84
{
public:
85
  enum Flags
86
  {
87
88
89
90
    APPEND   = 0x01,
    HTML     = 0x02,
    RETAIN   = 0x04,
    KEEP_OPEN = 0x08
91
92
  };

93
94
public:
  //! Constructor.
95
  Stream(
96
97
98
    const char* _flnm = nullptr,   //!< [in] Filename if file based.
    const uint _flags = APPEND    //!< [in] bit-field type identifier
  );
99
  ~Stream();
100

101
102
  const std::string& string() const;
  const char* c_str() const { return string().c_str(); }
103

104
105
106
107
108
109
  Base::IOutputStream& print(const int);
  Base::IOutputStream& print(const size_t);
  Base::IOutputStream& print(const double);
  Base::IOutputStream& print(const char* const);
  Base::IOutputStream& print(const char);
  Base::IOutputStream& print(const Base::Command&);
110

111
112
private:
  File* dfile_;
113

114
115
private:
  // inhibit copy
Ian Bell's avatar
Ian Bell committed
116
117
  Stream(const Stream&);
  Stream& operator=(const Stream&);
118
119
120
121
122

  File* dfile() const { return dfile_; }

  friend class Enter;
  friend class Controller;
123
124
};

125
126
127
128
129
130
131
extern void warning(const std::string& _wrng, const char* const _fnct, 
  const char* const _file,  const int _line);
extern void error(const std::string& _err, const char* const _fnct, 
  const char* const _file,  const int _line);

}//namespace Debug

132
133
134
135
136
137
138
139
140
#define DEB_module(MODULE)

//TODO: This should use an atomic thread-safe static int(s)
#define DEB_enter_func static int deb_nmbr = 0; \
  static int deb_lvl = Debug::INVALID_LEVEL; \
  Debug::Enter deb(__FILE__, __FUNCTION__, deb_nmbr, deb_lvl);

#define DEB_only(CC) CC

141
142
143
#define DEB_exec(LL, AA) DEB_exec_if(true, LL, AA)
#define DEB_exec_if(CC, LL, AA) { if (deb.pass(LL) && (CC)) { AA; } }

144
145
146
147
148
149
150
151
152
153
#define DEB_out(LL, AA) DEB_out_if(true, LL, AA)
#define DEB_out_if(CC, LL, AA) { if (deb.pass(LL) && (CC)) \
  { deb.stream() << AA << deb.end(); } }

#define DEB_line(LL, AA) DEB_line_if(true, LL, AA)
#define DEB_line_if(CC, LL, AA) { if (deb.pass(LL) && (CC)) \
  { deb.stream() << AA << deb.end_lf(); } }

#define DEB_warning(LL, AA) DEB_warning_if(true, LL, AA)
#define DEB_warning_if(CC, LL, AA) { if (deb.pass(LL) && (CC)) \
154
155
  { Debug::Stream strm; strm << AA; \
    Debug::warning(strm.string(), __FUNCTION__, __FILE__, __LINE__); } }
156

157
158
#define DEB_error(AA) { Debug::Stream strm; strm << AA; \
    Debug::error(strm.string(), __FUNCTION__, __FILE__, __LINE__); }
159
160
161
162
163
164
165
#define DEB_error_if(CC, AA) { if (CC) DEB_error(AA); }

// Stream does not fulfill ostream. If you want to exploit an existing
// ostream streamer to DEB_out a class as text without exploiting any
// numeric processing or custom Stream streamers then use this macro thus
// DEB_out(1, "my_class is " << DEB_os_str(my_c) )
#define DEB_os_str(AA) \
166
  dynamic_cast<std::ostringstream &&>((std::ostringstream() << AA )).str()
167

168
#endif // DEB_ON
169

170
#endif // BASE_DEBOUT_HH_INCLUDED