DebOut.hh 5.87 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 REFORM_DEBOUT_HH_INCLUDED
#define REFORM_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_enter_header_func
20
21
22
23
24
25
26
27
#define DEB_only(CC)
#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) {}
28

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

32
#else // DEB_ON
33
34

#include <string>
35
#include <vector>
36

37
#define DEB_module(MODULE) static const char* FILE_MODULE_NAME = MODULE;
38

39
/* TODO: This should use an atomic thread safe static int
40
*/
41
#define DEB_enter_func static int deb_enter_count = 0; \
42
43
44
45
  Debug::Enter deb(__FUNCTION__, deb_enter_count++, FILE_MODULE_NAME);

#define DEB_enter_header_func static int deb_enter_count = 0; \
  Debug::Enter deb(__FUNCTION__, deb_enter_count++, "header");
46

47
#define DEB_only(CC)  CC
48

49
#define DEB_out(LL, AA) DEB_out_if(true, LL, AA)
50

51
52
#define DEB_out_if(CC, LL, AA) { if (deb.permission(LL) && (CC)) \
  { deb.stream() << AA << deb.end(); } }
53

54
#define DEB_line(LL, AA) DEB_line_if(true, LL, AA)
55

56
57
#define DEB_line_if(CC, LL, AA) { if (deb.permission(LL) && (CC)) \
  { deb.stream() << AA << deb.end_lf(); } }
58

59
#define DEB_warning(LL, AA) DEB_warning_if(true, LL, AA)
60

61
62
#define DEB_warning_if(CC, LL, AA) { if (deb.permission(LL, 1) && (CC)) \
  { deb.stream(1) << "WARNING: " << AA << deb.end_lf(); } }
63

64
#define DEB_error(AA) DEB_error_if(true, AA)
65

66
67
#define DEB_error_if(CC, AA) { if (deb.permission(0, 1) && (CC)) \
  { deb.stream(2) << "ERROR: " << AA << deb.end_err(); } }
68

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

76
namespace Debug {
77

78
class Stream;
79

80
class Command
81
82
83
84
{
public:
  enum CommandType
  {
85
86
87
    END     = 0x01,
    END_LF  = 0x03,
    END_ERR = 0x07
88
  };
89

90
  CommandType com_;
91

92
93
94
95
  CommandType com() const
  {
    return com_;
  }
96
97

  Command(CommandType _com) : com_(_com) {}
98
99
100
};


101
// Class used to maintain Stream's CallStack
102
103
// This does not currently contain a pointer to the file streamer used
// but might do in future.
104
class Enter
105
106
{
public:
107
  int id_;       /*!< Unique identifier for this Enter (used in ANCHORS) */
108
109
  int count_;    /*!< deb_enter_count for this function. */
  int deb_outs_; /*!< Number of DEB_outs encountered within this function
110
111
                    determining whether a given DEB_out should include or omit
                    a call stack or exit trace. */
112
113
  int deb_lines_; /*!< Number of call stack indents including this call. */
  const char* module_;  /*!< Module name for this DEB_enter. */
114

115
116
117
  Enter(const char* const _funcname, const int _count, const char* const _module);
  ~Enter();
  Stream& stream(const int _warn = 0, const bool _print = true);
118
  int permission(const int _lev, const int _warn = 0);
119

120
  Command end()
121
  {
122
    return Command(Command::CommandType::END);
123
  }
124
  Command end_lf()
125
  {
126
    return Command(Command::CommandType::END_LF);
127
  }
128
  Command end_err()
129
  {
130
    return Command(Command::CommandType::END_ERR);
131
132
  }
};
133

134
class File;
135

136
class Stream
137
138
139
140
{
public:
  enum StreamType
  {
141
142
143
144
    APPEND   = 0x01,
    HTML     = 0x02,
    RETAIN   = 0x04,
    KEEP_OPEN = 0x08
145
146
147
#ifdef DEB_SHOW_ON
                , PRIMARY   = 0x10
#endif
148
149
  };

150
private:
151
  File* dfile_;
152
public:
153
  File* dfile() const
154
  {
155
    return this ? dfile_ : nullptr;
156
157
  }
  //! Constructor.
158
  Stream(
159
    const char* _file_name = nullptr,   //!< [in] File name if file based.
160
    const StreamType _type = APPEND    //!< [in] bitsfield enum type identifier
161
  ) ;
162
  ~Stream();
163

164
  const char* string_out() const;
165

166
167
168
169
170
  Stream& print(const int);
  Stream& print(const double);
  Stream& print(const char* const, bool fork = true);
  Stream& print(const char);
  Stream& print(const Command&);
171

172
173
  //! Get the currently active Stream
  static Stream& get_global(int _warn = 0);
174

175
176
private:
  // inhibit copy
Ian Bell's avatar
Ian Bell committed
177
178
  Stream(const Stream&);
  Stream& operator=(const Stream&);
179
180
};

181
182
Stream& operator<<(Stream& _ds, const int i);
Stream& operator<<(Stream& _ds, const double d);
183

184
185
186
Stream& operator<<(Stream& _ds, const size_t i);
Stream& operator<<(Stream& _ds, const unsigned int i);
Stream& operator<<(Stream& _ds, const float d);
187

188
189
190
Stream& operator<<(Stream& _ds, const char c);
Stream& operator<<(Stream& _ds, const char* const s);
Stream& operator<<(Stream& _ds, const std::string& s);
191

192
Stream& operator<<(Stream& _ds, const Command& co);
193

194
// Stream operator for std::vector<>
195
template< typename ElementT>
196
Stream& operator<<(Stream& _ds, const std::vector<ElementT>& _vec)
197
{
198
199
200
201
202
  _ds << "[ ";
  for (const auto el : _vec)
    _ds << el << " ";
  _ds << "]";
  return _ds;
203
204
}

205
// Stream operator for fixed size arrays
206
template <typename ElementT, size_t _el_nmbr>
207
Stream& operator<<(Stream& _ds, const ElementT(&_arr)[_el_nmbr])
208
209
210
211
212
213
214
{
  _ds << "[ ";
  for (const auto el : _arr)
    _ds << el << " ";
  _ds << "]";
  return _ds;
}
215

216
217
218
219
220
221
222
223
// Stream std::pair<>
template <typename T0, typename T1>
Stream& operator<<(Stream& _ds, const std::pair<T0, T1>& _pair)
{
  _ds << "(" << _pair.first << ", " << _pair.second << ")";
  return _ds;
}

224
}//namespace
225
226
227

#endif // DEB_out_INCLUDED

228
#endif//REFORM_DEBOUT_HH_INCLUDED