]> git.lyx.org Git - lyx.git/blob - src/support/debugstream.h
* lyxfunctional.h: delete compare_memfun and helper classes
[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 #ifdef DEBUG
20 # define TEMPORARY_DEBUG_MACRO DEBUG
21 # undef DEBUG
22 #endif
23
24 struct debug_trait {
25         enum type {
26                 NONE   = 0,
27                 EMERG  = 1,
28                 ALERT  = 2,
29                 CRIT   = 3,
30                 ERR    = 4,
31                 WARN   = 5,
32                 NOTICE = 6,
33                 INFO   = 7,
34                 DEBUG  = 8,
35                 ANY = 0xffffff
36         };
37
38         static bool match(type a, type b) {
39                 return (b <= a || (b == ANY && a > NONE));
40         }
41 };
42
43 #ifdef TEMPORARY_DEBUG_MACRO
44 # define DEBUG TEMPORARY_DEBUG_MACRO
45 # undef TEMPORARY_DEBUG_MACRO
46 #endif
47
48
49 template <class dtrait,
50           class charT = char,
51           class traits = std::char_traits<charT> >
52 class basic_debugstream : public std::basic_ostream<charT, traits> {
53 public:
54         typedef dtrait debug;
55         typedef typename debug::type Type;
56
57         basic_debugstream()
58                 : std::basic_ostream<charT, traits>(0), dt(debug::NONE)
59         {}
60
61         /// Constructor, sets the debug level to t.
62         explicit basic_debugstream(std::basic_streambuf<charT, traits> * buf)
63                 : std::basic_ostream<charT, traits>(buf), dt(debug::NONE)
64         {}
65
66         /// Sets the debug level to t.
67         void level(Type t) {
68                 dt = t;
69         }
70
71         /// Returns the current debug level.
72         Type level() const {
73                 return dt;
74         }
75
76         /// Returns true if t is part of the current debug level.
77         bool debugging(Type t = debug::ANY) const
78         {
79                 if (debug::match(dt, t)) return true;
80                 return false;
81         }
82
83         /** Returns the no-op stream if t is not part of the
84             current debug level otherwise the real debug stream
85             is used.
86             Use: dbgstream[Debug::INFO] << "Info!\n";
87         */
88         std::basic_ostream<charT, traits> & operator[](Type t) {
89                 if (debug::match(dt, t))
90                         return *this;
91                 return nullstream;
92         }
93 private:
94         /// The current debug level
95         Type dt;
96         /// The no-op stream.
97         boost::basic_onullstream<charT, traits> nullstream;
98 };
99
100 typedef basic_debugstream<debug_trait> debugstream;
101
102 #endif