DebFile.cc 3.27 KB
Newer Older
1
// (C) Copyright 2019 by Autodesk, Inc.
2
3
4
5

#ifdef DEB_ON
#include "DebFile.hh"
#include "DebCallStack.hh"
6

7
#include "Base/Utils/Environment.hh"
8
#include "Base/Utils/OStringStream.hh"
9
10
11
12
13

#include <string>
#include <fstream>
#include <cstring>

14
15
namespace Debug
{
16

17
File::~File()
18
{
19
20
21
  print_footer();
  flush();
  delete bffr_; // if flush failed to delete it for some reason
22
23
}

24
void File::print(const char _c, const bool _cnsl)
25
{
26
27
28
29
30
31
32
33
34
35
36
37
  if (_cnsl && cnsl_prnt_ != nullptr)
    (*cnsl_prnt_)(_c); // print on the console
  if (no_log()) 
    return; // no need to modify the buffer if logging is disabled
  if (bffr_ == nullptr)
    bffr_ = new std::string;
  if (line_strt_)
  {
    line_strt_ = false;
    (*bffr_) += ' ';
  }
  bffr_->append(&_c, 1);
38
  if (_c == ENDL)
39
40
    line_strt_ = true;
}
41

42
void File::line_break(const bool _cnsl) { print(ENDL, _cnsl); }
43

44
45
void File::print(const std::string& _s)
{
46
47
  for (const auto c : _s)
    print(c);
48
}
49

50
51
52
53
54
55
56
57
void File::print(const char* const _s)
{
  if (_s == nullptr)
    return;
  auto c = _s[0];
  for (int i = 0; c != '\0'; c = _s[++i])
    print(c);
}
58

59
60
void File::print(const size_t _i)
{
61
62
63
  Base::OStringStream strm;
  strm.print(_i);
  print(strm.str);
64
}
65

66
67
void File::print(const int _i)
{
68
69
70
  Base::OStringStream strm;
  strm.print(_i);
  print(strm.str);
71
}
72

73
74
75
76
77
78
void File::print(float _f)
{
  Base::OStringStream strm;
  strm.print(_f);
  print(strm.str);
}
79

80
81
void File::print(double _d)
{
82
83
84
  Base::OStringStream strm;
  strm.print(_d);
  print(strm.str);
85
}
86

87
88
89
90
91
92
93
// Append current asctime to given string
void File::print_time() 
{ 
  char bffr[256]; // avoid allocations as this is used on shutdown
  System::Environment::time(sizeof(bffr), bffr);
  print(bffr); 
}
94

95
96
97
void File::print_header()
{
  if (log())
98
  {
99
100
    print(flnm_);
    print(" opened ");
101
  }
102
103
104
105
  print_time();
  print("[ Build: " __TIME__ " " __DATE__ "] ");
  line_break();
}
106

107
108
109
110
void File::print_footer()
{
  line_break();
  if (log())
111
  {
112
    print(flnm_);
113
  }
114
115
116
117
  print(" Closed: ");
  print_time();
  line_break();
}
118
119


120
121
122
123
void File::flush()
{
  if (no_log())
    return;
124

125
126
127
128
  std::fstream file_stream(flnm_, std::fstream::out |
                  (flushed() ? std::fstream::app : std::fstream::trunc));
  if (!file_stream.is_open()) // failed opening the file?
    return;
129

130
131
  if (!flushed())
    print_header();
132

133
134
135
  file_stream << *bffr_;
  delete bffr_;
  bffr_ = nullptr;
136

137
138
  ++flush_nmbr_;
}
139

140

141
142
143
144
145
void File::enter(const Enter* const _entr)
{
  if (log()) // log enabled?
    CallStack::modify().push(_entr);// call stacks are printed only in the log
}
146

147
148
149
150
void File::start()
{
  if (no_log()) // log disabled?
    return;
151

152
153
154
  // First DEB_out in this function so output call-stack, and flush.
  if (!line_strt_)
    line_break(false); // make sure we start on a new line with this
155

156
157
158
  // lead in for the debug output file only
  print('*', false); 
  print('>', false); 
159
  CallStack::query().append(*bffr_);
160

161
162
163
  line_break(false); // make sure we start on a new line after this
  flush();
}
164

165
166
167
168
169
void File::exit() 
{ 
  if (log())
    CallStack::modify().pop();
}
170
171
172
173
174
175

//////////////////////////////////////////////////////////////////////////

File& File::modify()
{
  // TODO: Thread-local storage, each (per thread) file in a separate  folder
176
  static File glbl_file;
177
178
179
  return glbl_file;
}

180
181
const File& File::query() { return modify(); }

182
} // namespace Debug
183
184

#endif // DEB_ON