UniSet  2.12.1
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  inline void showMilliseconds( bool s ) noexcept
179  {
180  show_msec = s;
181  }
182 
183  inline void showMicroseconds( bool s ) noexcept
184  {
185  show_usec = s;
186  }
187 
188  inline void showLogType(bool s) noexcept
189  {
190  show_logtype = s;
191  }
192 
193  inline void showLabels(bool s) noexcept
194  {
195  show_labels = s;
196  }
197 
198  inline void hideLabelKey(bool s) noexcept
199  {
200  hide_label_key = s;
201  }
202 
203  inline std::ostream& log(Debug::type l) noexcept
204  {
205  return this->operator[](l);
206  }
207 
208  void verbose(Debug::verbosity v) noexcept
209  {
210  verb = v;
211  }
212 
214  Debug::verbosity verbose() const noexcept
215  {
216  return verb;
217  }
218 
219  // example: dlog.V(1)[Debug::INFO] << "some log.." << endl;
220  DebugStream& V( Debug::verbosity v ) noexcept;
221 
222  // labels
223  typedef std::pair<std::string, std::string> Label;
224 
225  void addLabel( const std::string& key, const std::string& value ) noexcept;
226  void delLabel( const std::string& key ) noexcept;
227  void cleanupLabels() noexcept;
228  std::vector<Label> getLabels() noexcept;
229 
230  // -----------------------------------------------------
231  // короткие функции (для удобства)
232  // log.level1() - вывод с датой и временем "date time [LEVEL] ...",
233  // если вывод даты и времени не выключен при помощи showDateTime(false)
234  // if( log.is_level1() ) - проверка включён ли лог.."
235 
236 #define DMANIP(FNAME,LEVEL) \
237  inline std::ostream& FNAME( bool showdatetime=true ) noexcept \
238  {\
239  if( showdatetime )\
240  return operator[](Debug::LEVEL); \
241  return operator()(Debug::LEVEL); \
242  } \
243  \
244  inline bool is_##FNAME() const noexcept\
245  { return debugging(Debug::LEVEL); }
246 
247  DMANIP(level1, LEVEL1)
248  DMANIP(level2, LEVEL2)
249  DMANIP(level3, LEVEL3)
250  DMANIP(level4, LEVEL4)
251  DMANIP(level5, LEVEL5)
252  DMANIP(level6, LEVEL6)
253  DMANIP(level7, LEVEL7)
254  DMANIP(level8, LEVEL8)
255  DMANIP(level9, LEVEL9)
256  DMANIP(info, INFO)
257  DMANIP(warn, WARN)
258  DMANIP(crit, CRIT)
259  DMANIP(repository, REPOSITORY)
260  DMANIP(system, SYSTEM)
261  DMANIP(exception, EXCEPTION)
262  DMANIP(any, ANY)
263 #undef DMANIP
264 
265  std::ostream& printDate(Debug::type t, char brk = '/') noexcept;
266  std::ostream& printTime(Debug::type t, char brk = ':') noexcept;
267  std::ostream& printDateTime(Debug::type t) noexcept;
268 
269  std::ostream& pos(int x, int y) noexcept;
270 
271  const DebugStream& operator=(const DebugStream& r);
272 
273  inline void setLogName( const std::string& n ) noexcept
274  {
275  logname = n;
276  }
277 
278  inline std::string getLogName() const noexcept
279  {
280  return logname;
281  }
282 
283  protected:
284  void sbuf_overflow( const std::string& s ) noexcept;
285 
286  // private:
288  Debug::type dt = { Debug::NONE };
290  std::ostream nullstream;
292  struct debugstream_internal;
294  debugstream_internal* internal = { 0 };
295  bool show_datetime = { true };
296  bool show_logtype = { true };
297  bool show_msec = { false };
298  bool show_usec = { false };
299  std::string fname = { "" };
300 
301  StreamEvent_Signal s_stream;
302  std::string logname = { "" };
303 
304  bool isWriteLogFile = { false };
305  bool onScreen = { true };
306 
307  Debug::verbosity verb = { 0 };
308  Debug::verbosity vv = { 0 };
309 
310  std::vector<Label> labels;
311  bool show_labels = { true };
312  bool hide_label_key = { false };
313 };
314 
315 // ------------------------------------------------------------------------------------------------
316 #endif
Definition: DebugStream.h:61
Debug::type dt
The current debug level.
Definition: DebugStream.h:288
bool debugging(Debug::type t=Debug::ANY) const noexcept
Returns true if t is part of the current debug level.
Definition: DebugStream.h:140
std::ostream & operator[](Debug::type t) noexcept
Definition: DebugStream.h:155
Definition: Debug.h:29
std::ostream nullstream
The no-op stream.
Definition: DebugStream.h:290
Debug::verbosity verbose() const noexcept
Returns the current verbose level.
Definition: DebugStream.h:214
void addLevel(Debug::type t) noexcept
Adds t to the current debug level.
Definition: DebugStream.h:90
void delLevel(Debug::type t) noexcept
Deletes t from the current debug level.
Definition: DebugStream.h:96
std::ostream & to_end(Debug::type t) noexcept
Definition: DebugStream.h:163
virtual void logFile(const std::string &f, bool truncate=false)
Sets the debugstreams' logfile to f.
Definition: DebugStream.cc:104
DebugStream(Debug::type t=Debug::NONE, Debug::verbosity v=0)
Constructor, sets the debug level to t.
Definition: DebugStream.cc:34
std::ostream & debug(Debug::type t=Debug::ANY) noexcept
Definition: DebugStream.cc:161
std::ostream & operator()(Debug::type t) noexcept
Definition: DebugStream.cc:192
void level(Debug::type t) noexcept
Sets the debug level to t.
Definition: DebugStream.h:78
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:84