]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
In insets:
[lyx.git] / src / support / debugstream.h
1 // -*- C++ -*-
2 /**
3  * \file debugstream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DEBUG_STREAM_HPP
13 #define DEBUG_STREAM_HPP
14
15 #include <iostream>
16
17 #include <boost/test/detail/nullstream.hpp>
18
19 struct debug_trait {
20         enum type {
21                 NONE   = 0,
22                 EMERG  = 1,
23                 ALERT  = 2,
24                 CRIT   = 3,
25                 ERR    = 4,
26                 WARN   = 5,
27                 NOTICE = 6,
28                 INFO   = 7,
29                 DEBUG  = 8,
30                 ANY = 0xffffff
31         };
32
33         static bool match(type a, type b) {
34                 return (b <= a || (b == ANY && a > NONE));
35         }
36 };
37
38
39 template <class dtrait,
40           class charT = char,
41           class traits = std::char_traits<charT> >
42 class basic_debugstream : public std::basic_ostream<charT, traits> {
43 public:
44         typedef dtrait debug;
45         typedef typename debug::type Type;
46
47         /// Constructor, sets the debug level to t.
48         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
49                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE)
50         {}
51
52         /// Sets the debug level to t.
53         void level(Type t) {
54                 dt = t;
55         }
56
57         /// Returns the current debug level.
58         Type level() const {
59                 return dt;
60         }
61
62         /// Returns true if t is part of the current debug level.
63         bool debugging(Type t = debug::ANY) const
64         {
65                 if (debug::match(dt, t)) return true;
66                 return false;
67         }
68
69         /** Returns the no-op stream if t is not part of the
70             current debug level otherwise the real debug stream
71             is used.
72             Use: dbgstream[Debug::INFO] << "Info!\n";
73         */
74         std::basic_ostream<charT, traits> & operator[](Type t) {
75                 if (debug::match(dt, t))
76                         return *this;
77                 return nullstream;
78         }
79 private:
80         /// The current debug level
81         Type dt;
82         /// The no-op stream.
83         boost::basic_onullstream<charT, traits> nullstream;
84 };
85
86 typedef basic_debugstream<debug_trait> debugstream;
87
88 #endif