UniSet  2.7.0
DebugStream.h
1 // -*- C++ -*-
2 
3 // Created by Lars Gullik BjЬnnes
4 // Copyright 1999 Lars Gullik BjЬnnes (larsbj@lyx.org)
5 // Released into the public domain.
6 
7 // Implemented and tested on g++ 2.7.2.3
8 
9 // Primarily developed for use in the LyX Project http://www.lyx.org/
10 // but should be adaptable to any project.
11 
12 // (c) 2002 adapted for UniSet by Lav, GNU LGPL license
13 // Modify for UniSet by pv@eterspft.ru, GNU LGPL license
14 
15 #ifndef DEBUGSTREAM_H
16 #define DEBUGSTREAM_H
17 
18 //#ifdef __GNUG__
19 //#pragma interface
20 //#endif
21 
22 #include <iostream>
23 #include <string>
24 #include <sigc++/sigc++.h>
25 #include "Debug.h"
26 
27 #ifdef TEST_DEBUGSTREAM
28 #include <string>
29 struct Debug
30 {
31  enum type
32  {
33  NONE = 0,
34  INFO = (1 << 0), // 1
35  WARN = (1 << 1), // 2
36  CRIT = (1 << 2) // 4
37  };
38  static const type ANY = type(INFO | WARN | CRIT);
39  static Debug::type value(std::string const& val)
40  {
41  if (val == "NONE") return Debug::NONE;
42 
43  if (val == "INFO") return Debug::INFO;
44 
45  if (val == "WARN") return Debug::WARN;
46 
47  if (val == "CRIT") return Debug::CRIT;
48 
49  return Debug::NONE;
50  }
51 };
52 #endif
53 
91 class DebugStream : public std::ostream
92 {
93  public:
95  explicit DebugStream(Debug::type t = Debug::NONE);
96 
98  explicit
99  DebugStream(char const* f, Debug::type t = Debug::NONE, bool truncate = false );
100 
102  virtual ~DebugStream();
103 
104  typedef sigc::signal<void, const std::string&> StreamEvent_Signal;
105  StreamEvent_Signal signal_stream_event();
106 
108  void level(Debug::type t) noexcept
109  {
110  dt = Debug::type(t & Debug::ANY);
111  }
112 
114  Debug::type level() const noexcept
115  {
116  return dt;
117  }
118 
120  void addLevel(Debug::type t) noexcept
121  {
122  dt = Debug::type(dt | t);
123  }
124 
126  void delLevel(Debug::type t) noexcept
127  {
128  dt = Debug::type(dt & ~t);
129  }
130 
132  virtual void logFile( const std::string& f, bool truncate = false );
133 
134  inline std::string getLogFile() const noexcept
135  {
136  return fname;
137  }
138 
139  // имя лог файла можно установить отдельно, но не вклчать запись..
140  inline void setLogFile( const std::string& n ) noexcept
141  {
142  fname = n;
143  }
144 
145  // включена ли запись лог-файла
146  inline bool isOnLogFile() const noexcept
147  {
148  return isWriteLogFile;
149  }
150 
151  // включить запись лог файла
152  inline void onLogFile( bool truncate = false )
153  {
154  logFile(fname, truncate);
155  }
156 
157  // отключить запись логфайла
158  inline void offLogFile() noexcept
159  {
160  logFile("");
161  }
162 
164  inline bool debugging(Debug::type t = Debug::ANY) const noexcept
165  {
166  return (dt & t);
167  }
168 
173  std::ostream& debug(Debug::type t = Debug::ANY) noexcept;
174  // if (dt & t) return *this;
175  // return nullstream;
176  // }
177 
178 
183  std::ostream& operator[](Debug::type t) noexcept
184  {
185  return debug(t);
186  }
187 
191  inline std::ostream& to_end(Debug::type t) noexcept
192  {
193  return this->operator()(t);
194  }
195 
199  std::ostream& operator()(Debug::type t) noexcept;
200 
201  inline void showDateTime(bool s) noexcept
202  {
203  show_datetime = s;
204  }
205 
206  inline void showMilliseconds( bool s ) noexcept
207  {
208  show_msec = s;
209  }
210 
211  inline void showMicroseconds( bool s ) noexcept
212  {
213  show_usec = s;
214  }
215 
216  inline void showLogType(bool s) noexcept
217  {
218  show_logtype = s;
219  }
220 
221  inline std::ostream& log(Debug::type l) noexcept
222  {
223  return this->operator[](l);
224  }
225 
226  // короткие функции (для удобства)
227  // log.level1() - вывод с датой и временем "date time [LEVEL] ...",
228  // если вывод даты и времени не выключен при помощи showDateTime(false)
229  // if( log.is_level1() ) - проверка включён ли лог.."
230 
231 #define DMANIP(FNAME,LEVEL) \
232  inline std::ostream& FNAME( bool showdatetime=true ) noexcept \
233  {\
234  if( showdatetime )\
235  return operator[](Debug::LEVEL); \
236  return operator()(Debug::LEVEL); \
237  } \
238  \
239  inline bool is_##FNAME() const noexcept\
240  { return debugging(Debug::LEVEL); }
241 
242  DMANIP(level1, LEVEL1)
243  DMANIP(level2, LEVEL2)
244  DMANIP(level3, LEVEL3)
245  DMANIP(level4, LEVEL4)
246  DMANIP(level5, LEVEL5)
247  DMANIP(level6, LEVEL6)
248  DMANIP(level7, LEVEL7)
249  DMANIP(level8, LEVEL8)
250  DMANIP(level9, LEVEL9)
251  DMANIP(info, INFO)
252  DMANIP(warn, WARN)
253  DMANIP(crit, CRIT)
254  DMANIP(repository, REPOSITORY)
255  DMANIP(system, SYSTEM)
256  DMANIP(exception, EXCEPTION)
257  DMANIP(any, ANY)
258 #undef DMANIP
259 
260  std::ostream& printDate(Debug::type t, char brk = '/') noexcept;
261  std::ostream& printTime(Debug::type t, char brk = ':') noexcept;
262  std::ostream& printDateTime(Debug::type t) noexcept;
263 
264  std::ostream& pos(int x, int y) noexcept;
265 
266  const DebugStream& operator=(const DebugStream& r);
267 
268  inline void setLogName( const std::string& n ) noexcept
269  {
270  logname = n;
271  }
272 
273  inline std::string getLogName() const noexcept
274  {
275  return logname;
276  }
277 
278  protected:
279  void sbuf_overflow( const std::string& s ) noexcept;
280 
281  // private:
283  Debug::type dt = { Debug::NONE };
285  std::ostream nullstream;
287  struct debugstream_internal;
289  debugstream_internal* internal = { 0 };
290  bool show_datetime = { true };
291  bool show_logtype = { true };
292  bool show_msec = { false };
293  bool show_usec = { false };
294  std::string fname = { "" };
295 
296  StreamEvent_Signal s_stream;
297  std::string logname = { "" };
298 
299  bool isWriteLogFile = { false };
300 };
301 // ------------------------------------------------------------------------------------------------
302 #endif
Definition: DebugStream.h:91
bool debugging(Debug::type t=Debug::ANY) const noexcept
Returns true if t is part of the current debug level.
Definition: DebugStream.h:164
std::ostream & operator[](Debug::type t) noexcept
Definition: DebugStream.h:183
Definition: Debug.h:37
std::ostream nullstream
The no-op stream.
Definition: DebugStream.h:285
void addLevel(Debug::type t) noexcept
Adds t to the current debug level.
Definition: DebugStream.h:120
void delLevel(Debug::type t) noexcept
Deletes t from the current debug level.
Definition: DebugStream.h:126
std::ostream & to_end(Debug::type t) noexcept
Definition: DebugStream.h:191
static Debug::type value(std::string const &val)
Definition: Debug.cc:67
void level(Debug::type t) noexcept
Sets the debug level to t.
Definition: DebugStream.h:108
So that public parts of DebugStream does not need to know about filebuf.
Definition: DebugExtBuf.h:357
Debug::type level() const noexcept
Returns the current debug level.
Definition: DebugStream.h:114