UniSet  2.8.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@etersoft.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 
163  // enable print on screen
164  void enableOnScreen();
165 
166  // disable print onscreen
167  void disableOnScreen();
168 
170  inline bool debugging(Debug::type t = Debug::ANY) const noexcept
171  {
172  return (dt & t);
173  }
174 
179  std::ostream& debug(Debug::type t = Debug::ANY) noexcept;
180  // if (dt & t) return *this;
181  // return nullstream;
182  // }
183 
184 
189  std::ostream& operator[](Debug::type t) noexcept
190  {
191  return debug(t);
192  }
193 
197  inline std::ostream& to_end(Debug::type t) noexcept
198  {
199  return this->operator()(t);
200  }
201 
205  std::ostream& operator()(Debug::type t) noexcept;
206 
207  inline void showDateTime(bool s) noexcept
208  {
209  show_datetime = s;
210  }
211 
212  inline void showMilliseconds( bool s ) noexcept
213  {
214  show_msec = s;
215  }
216 
217  inline void showMicroseconds( bool s ) noexcept
218  {
219  show_usec = s;
220  }
221 
222  inline void showLogType(bool s) noexcept
223  {
224  show_logtype = s;
225  }
226 
227  inline std::ostream& log(Debug::type l) noexcept
228  {
229  return this->operator[](l);
230  }
231 
232  // короткие функции (для удобства)
233  // log.level1() - вывод с датой и временем "date time [LEVEL] ...",
234  // если вывод даты и времени не выключен при помощи showDateTime(false)
235  // if( log.is_level1() ) - проверка включён ли лог.."
236 
237 #define DMANIP(FNAME,LEVEL) \
238  inline std::ostream& FNAME( bool showdatetime=true ) noexcept \
239  {\
240  if( showdatetime )\
241  return operator[](Debug::LEVEL); \
242  return operator()(Debug::LEVEL); \
243  } \
244  \
245  inline bool is_##FNAME() const noexcept\
246  { return debugging(Debug::LEVEL); }
247 
248  DMANIP(level1, LEVEL1)
249  DMANIP(level2, LEVEL2)
250  DMANIP(level3, LEVEL3)
251  DMANIP(level4, LEVEL4)
252  DMANIP(level5, LEVEL5)
253  DMANIP(level6, LEVEL6)
254  DMANIP(level7, LEVEL7)
255  DMANIP(level8, LEVEL8)
256  DMANIP(level9, LEVEL9)
257  DMANIP(info, INFO)
258  DMANIP(warn, WARN)
259  DMANIP(crit, CRIT)
260  DMANIP(repository, REPOSITORY)
261  DMANIP(system, SYSTEM)
262  DMANIP(exception, EXCEPTION)
263  DMANIP(any, ANY)
264 #undef DMANIP
265 
266  std::ostream& printDate(Debug::type t, char brk = '/') noexcept;
267  std::ostream& printTime(Debug::type t, char brk = ':') noexcept;
268  std::ostream& printDateTime(Debug::type t) noexcept;
269 
270  std::ostream& pos(int x, int y) noexcept;
271 
272  const DebugStream& operator=(const DebugStream& r);
273 
274  inline void setLogName( const std::string& n ) noexcept
275  {
276  logname = n;
277  }
278 
279  inline std::string getLogName() const noexcept
280  {
281  return logname;
282  }
283 
284  protected:
285  void sbuf_overflow( const std::string& s ) noexcept;
286 
287  // private:
289  Debug::type dt = { Debug::NONE };
291  std::ostream nullstream;
293  struct debugstream_internal;
295  debugstream_internal* internal = { 0 };
296  bool show_datetime = { true };
297  bool show_logtype = { true };
298  bool show_msec = { false };
299  bool show_usec = { false };
300  std::string fname = { "" };
301 
302  StreamEvent_Signal s_stream;
303  std::string logname = { "" };
304 
305  bool isWriteLogFile = { false };
306  bool onScreen = { true };
307 };
308 // ------------------------------------------------------------------------------------------------
309 #endif
Definition: DebugStream.h:91
Debug::type dt
The current debug level.
Definition: DebugStream.h:289
bool debugging(Debug::type t=Debug::ANY) const noexcept
Returns true if t is part of the current debug level.
Definition: DebugStream.h:170
std::ostream & operator[](Debug::type t) noexcept
Definition: DebugStream.h:189
Definition: Debug.h:37
std::ostream nullstream
The no-op stream.
Definition: DebugStream.h:291
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:197
virtual void logFile(const std::string &f, bool truncate=false)
Sets the debugstreams' logfile to f.
Definition: DebugStream.cc:115
static Debug::type value(std::string const &val)
Definition: Debug.cc:67
std::ostream & debug(Debug::type t=Debug::ANY) noexcept
Definition: DebugStream.cc:172
std::ostream & operator()(Debug::type t) noexcept
Definition: DebugStream.cc:190
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
DebugStream(Debug::type t=Debug::NONE)
Constructor, sets the debug level to t.
Definition: DebugStream.cc:47