UniSet  2.24.2
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 #include <iostream>
19 #include <string>
20 #include <sigc++/sigc++.h>
21 #include <vector>
22 #include "Debug.h"
23 
61 class DebugStream : public std::ostream
62 {
63  public:
65  explicit DebugStream(Debug::type t = Debug::NONE, Debug::verbosity v = 0);
66 
68  explicit
69  DebugStream(char const* f, Debug::type t = Debug::NONE, bool truncate = false );
70 
72  virtual ~DebugStream();
73 
74  typedef sigc::signal<void, const std::string&> StreamEvent_Signal;
75  StreamEvent_Signal signal_stream_event();
76 
78  void level(Debug::type t) noexcept
79  {
80  dt = Debug::type(t & Debug::ANY);
81  }
82 
84  Debug::type level() const noexcept
85  {
86  return dt;
87  }
88 
90  void addLevel(Debug::type t) noexcept
91  {
92  dt = Debug::type(dt | t);
93  }
94 
96  void delLevel(Debug::type t) noexcept
97  {
98  dt = Debug::type(dt & ~t);
99  }
100 
102  virtual void logFile( const std::string& f, bool truncate = false );
103 
104  inline std::string getLogFile() const noexcept
105  {
106  return fname;
107  }
108 
109  // имя лог файла можно установить отдельно, но не включать запись..
110  inline void setLogFile( const std::string& n ) noexcept
111  {
112  fname = n;
113  }
114 
115  // включена ли запись лог-файла
116  inline bool isOnLogFile() const noexcept
117  {
118  return isWriteLogFile;
119  }
120 
121  // включить запись лог файла
122  inline void onLogFile( bool truncate = false )
123  {
124  logFile(fname, truncate);
125  }
126 
127  // отключить запись логфайла
128  inline void offLogFile() noexcept
129  {
130  logFile("");
131  }
132 
133  // enable print on screen
134  void enableOnScreen();
135 
136  // disable print onscreen
137  void disableOnScreen();
138 
140  inline bool debugging(Debug::type t = Debug::ANY) const noexcept
141  {
142  return (dt & t);
143  }
144 
149  std::ostream& debug(Debug::type t = Debug::ANY) noexcept;
150 
155  std::ostream& operator[](Debug::type t) noexcept
156  {
157  return debug(t);
158  }
159 
163  inline std::ostream& to_end(Debug::type t) noexcept
164  {
165  return this->operator()(t);
166  }
167 
171  std::ostream& operator()(Debug::type t) noexcept;
172 
173  inline void showDateTime(bool s) noexcept
174  {
175  show_datetime = s;
176  }
177 
178  // false = UTC (by default)
179  inline void showLocalTime( bool s ) noexcept
180  {
181  show_localtime = s;
182  }
183 
184  inline void showMilliseconds( bool s ) noexcept
185  {
186  show_msec = s;
187  }
188 
189  inline void showMicroseconds( bool s ) noexcept
190  {
191  show_usec = s;
192  }
193 
194  inline void showLogType(bool s) noexcept
195  {
196  show_logtype = s;
197  }
198 
199  inline void showLabels(bool s) noexcept
200  {
201  show_labels = s;
202  }
203 
204  inline void hideLabelKey(bool s) noexcept
205  {
206  hide_label_key = s;
207  }
208 
209  inline std::ostream& log(Debug::type l) noexcept
210  {
211  return this->operator[](l);
212  }
213 
214  void verbose(Debug::verbosity v) noexcept
215  {
216  verb = v;
217  }
218 
220  Debug::verbosity verbose() const noexcept
221  {
222  return verb;
223  }
224 
225  // example: dlog.V(1)[Debug::INFO] << "some log.." << endl;
226  DebugStream& V( Debug::verbosity v ) noexcept;
227 
228  // labels
229  typedef std::pair<std::string, std::string> Label;
230 
231  void addLabel( const std::string& key, const std::string& value ) noexcept;
232  void delLabel( const std::string& key ) noexcept;
233  void cleanupLabels() noexcept;
234  std::vector<Label> getLabels() noexcept;
235 
236  // -----------------------------------------------------
237  // короткие функции (для удобства)
238  // log.level1() - вывод с датой и временем "date time [LEVEL] ...",
239  // если вывод даты и времени не выключен при помощи showDateTime(false)
240  // if( log.is_level1() ) - проверка включён ли лог.."
241 
242 #define DMANIP(FNAME,LEVEL) \
243  inline std::ostream& FNAME( bool showdatetime=true ) noexcept \
244  {\
245  if( showdatetime )\
246  return operator[](Debug::LEVEL); \
247  return operator()(Debug::LEVEL); \
248  } \
249  \
250  inline bool is_##FNAME() const noexcept\
251  { return debugging(Debug::LEVEL); }
252 
253  DMANIP(level1, LEVEL1)
254  DMANIP(level2, LEVEL2)
255  DMANIP(level3, LEVEL3)
256  DMANIP(level4, LEVEL4)
257  DMANIP(level5, LEVEL5)
258  DMANIP(level6, LEVEL6)
259  DMANIP(level7, LEVEL7)
260  DMANIP(level8, LEVEL8)
261  DMANIP(level9, LEVEL9)
262  DMANIP(info, INFO)
263  DMANIP(warn, WARN)
264  DMANIP(crit, CRIT)
265  DMANIP(repository, REPOSITORY)
266  DMANIP(system, SYSTEM)
267  DMANIP(exception, EXCEPTION)
268  DMANIP(any, ANY)
269 #undef DMANIP
270 
271  std::ostream& printDate(Debug::type t, char brk = '/') noexcept;
272  std::ostream& printTime(Debug::type t, char brk = ':') noexcept;
273  std::ostream& printDateTime(Debug::type t) noexcept;
274 
275  std::ostream& pos(int x, int y) noexcept;
276 
277  const DebugStream& operator=(const DebugStream& r);
278 
279  inline void setLogName( const std::string& n ) noexcept
280  {
281  logname = n;
282  }
283 
284  inline std::string getLogName() const noexcept
285  {
286  return logname;
287  }
288 
289  protected:
290  void sbuf_overflow( const std::string& s ) noexcept;
291 
292  // private:
294  Debug::type dt = { Debug::NONE };
296  std::ostream nullstream;
298  struct debugstream_internal;
300  debugstream_internal* internal = { 0 };
301  bool show_datetime = { true };
302  bool show_logtype = { true };
303  bool show_msec = { false };
304  bool show_usec = { false };
305  bool show_localtime = { false };
306  std::string fname = { "" };
307 
308  StreamEvent_Signal s_stream;
309  std::string logname = { "" };
310 
311  bool isWriteLogFile = { false };
312  bool onScreen = { true };
313 
314  Debug::verbosity verb = { 0 };
315  Debug::verbosity vv = { 0 };
316 
317  std::vector<Label> labels;
318  bool show_labels = { true };
319  bool hide_label_key = { false };
320 };
321 
322 // ------------------------------------------------------------------------------------------------
323 #endif
Definition: DebugStream.h:62
DebugStream(Debug::type t=Debug::NONE, Debug::verbosity v=0)
Constructor, sets the debug level to t.
Definition: DebugStream.cc:34
void level(Debug::type t) noexcept
Sets the debug level to t.
Definition: DebugStream.h:78
std::ostream & debug(Debug::type t=Debug::ANY) noexcept
Definition: DebugStream.cc:161
std::ostream & operator()(Debug::type t) noexcept
Definition: DebugStream.cc:192
std::ostream & operator[](Debug::type t) noexcept
Definition: DebugStream.h:155
Debug::type level() const noexcept
Returns the current debug level.
Definition: DebugStream.h:84
virtual void logFile(const std::string &f, bool truncate=false)
Sets the debugstreams' logfile to f.
Definition: DebugStream.cc:104
std::ostream & to_end(Debug::type t) noexcept
Definition: DebugStream.h:163
std::ostream nullstream
The no-op stream.
Definition: DebugStream.h:296
bool debugging(Debug::type t=Debug::ANY) const noexcept
Returns true if t is part of the current debug level.
Definition: DebugStream.h:140
Debug::verbosity verbose() const noexcept
Returns the current verbose level.
Definition: DebugStream.h:220
void delLevel(Debug::type t) noexcept
Deletes t from the current debug level.
Definition: DebugStream.h:96
void addLevel(Debug::type t) noexcept
Adds t to the current debug level.
Definition: DebugStream.h:90
Debug::type dt
The current debug level.
Definition: DebugStream.h:294
Definition: Debug.h:30
So that public parts of DebugStream does not need to know about filebuf.
Definition: DebugExtBuf.h:358