]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
try to avoid initailizaton problems with global debugstream variable.
[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         basic_debugstream()
48                 : std::basic_ostream<charT, traits>(0), dt(debug::NONE)
49         {}
50
51         /// Constructor, sets the debug level to t.
52         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
53                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE)
54         {}
55
56         /// Sets the debug level to t.
57         void level(Type t) {
58                 dt = t;
59         }
60
61         /// Returns the current debug level.
62         Type level() const {
63                 return dt;
64         }
65
66         /// Returns true if t is part of the current debug level.
67         bool debugging(Type t = debug::ANY) const
68         {
69                 if (debug::match(dt, t)) return true;
70                 return false;
71         }
72
73         /** Returns the no-op stream if t is not part of the
74             current debug level otherwise the real debug stream
75             is used.
76             Use: dbgstream[Debug::INFO] << "Info!\n";
77         */
78         std::basic_ostream<charT, traits> & operator[](Type t) {
79                 if (debug::match(dt, t))
80                         return *this;
81                 return nullstream;
82         }
83 private:
84         /// The current debug level
85         Type dt;
86         /// The no-op stream.
87         boost::basic_onullstream<charT, traits> nullstream;
88 };
89
90 typedef basic_debugstream<debug_trait> debugstream;
91
92 #endif