Utils/Logger: cleaned up some #includes
[senf.git] / senf / Utils / Logger / Target.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Target internal header */
25
26 #ifndef IH_SENF_Utils_Logger_Target_
27 #define IH_SENF_Utils_Logger_Target_ 1
28
29 // Custom includes
30 #include <set>
31 #include <memory>
32 #include <boost/type_traits/is_same.hpp>
33 #include <boost/static_assert.hpp>
34 #include <boost/shared_ptr.hpp>
35 #include <senf/Utils/singleton.hh>
36 #include <senf/Utils/mpl.hh>
37 #include <senf/Utils/Console/LazyDirectory.hh>
38 #include <senf/Utils/Console/Parse.hh>
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43
44     namespace console { class DirectoryNode; }
45
46 namespace log {
47 namespace detail {
48
49     struct LogParameters {
50         StreamBase const * stream;
51         AreaBase const * area;
52         unsigned level;
53         void clear();
54         void setDefaults();
55         static LogParameters defaultParameters();
56     };
57
58     std::ostream & operator<<(std::ostream & os, LogParameters const & pm);
59
60     void senf_console_parse_argument(console::ParseCommandInfo::TokensRange const & tokens,
61                                      LogParameters & out);
62
63     /** \brief Internal: Target registry */
64     class TargetRegistry
65         : public senf::singleton<TargetRegistry>
66     {
67     public:
68         enum Level {
69             VERBOSE = senf::log::VERBOSE::value,
70             NOTICE = senf::log::NOTICE::value,
71             MESSAGE = senf::log::MESSAGE::value,
72             IMPORTANT = senf::log::IMPORTANT::value,
73             CRITICAL = senf::log::CRITICAL::value,
74             FATAL = senf::log::FATAL::value
75         };
76
77         using senf::singleton<TargetRegistry>::instance;
78
79         void write(StreamBase const & stream, AreaBase const & area, unsigned level,
80                    std::string const & msg);
81
82         void routed();
83         bool fallbackRouting();
84
85         senf::console::ScopedDirectory<> & consoleDir();
86
87         void dynamicTarget(std::auto_ptr<Target> target);
88
89     private:
90         TargetRegistry();
91         ~TargetRegistry();
92
93         void registerTarget(Target * target, std::string const & name);
94         void unregisterTarget(Target * target);
95
96         void consoleAreas(std::ostream & os);
97         void consoleStreams(std::ostream & os);
98         void consoleWrite(LogParameters parameters, std::string const & msg);
99         void consoleRemoveTarget(Target * target);
100         boost::shared_ptr<senf::console::DirectoryNode> consoleSelf(std::ostream & os);
101
102         typedef std::set<Target *> Targets;
103         Targets targets_;
104
105         bool fallbackRouting_;
106
107         console::LazyDirectory consoleDir_;
108
109         Targets dynamicTargets_;
110
111         friend class senf::log::Target;
112         friend class senf::singleton<TargetRegistry>;
113     };
114
115     /** \brief Internal: Write log message */
116     template <class Stream, class Area, class Level>
117     void write(std::string const & msg);
118
119 #ifndef DOXYGEN
120
121     // This code takes the routing target template arguments in any order and sorts them
122     // by type (Stream, Area and Level).
123
124     senf::mpl::rv<0u> RouteParameterCheck_(...);
125     senf::mpl::rv<1u> RouteParameterCheck_(StreamBase *);
126     senf::mpl::rv<2u> RouteParameterCheck_(AreaBase *);
127     template <class T> senf::mpl::rv<3u> RouteParameterCheck_(T*, typename T::SENFLogArea * = 0);
128     senf::mpl::rv<4u> RouteParameterCheck_(LevelBase *);
129
130     // For g++ 4.0 (at least) we need to provide the fully scoped name for this default value.
131     // no idea why. It works without the scope in 4.1
132     template < class T, class A2, class A1,
133                unsigned type = SENF_MPL_RV( senf::log::detail::RouteParameterCheck_(static_cast<T*>(0)) ) >
134     struct RouteParameters
135     {};
136
137     template <class A2, class A1>
138     struct RouteParameters<mpl::nil,A2,A1,0u>
139         : public RouteParameters<A2,A1,mpl::nil>
140     {};
141
142     struct NilLevel {
143         static unsigned const value = NONE::value;
144     };
145
146     template <>
147     struct RouteParameters<mpl::nil,mpl::nil,mpl::nil,0u>
148     {
149         typedef mpl::nil Stream;
150         typedef mpl::nil Area;
151         typedef NilLevel Level;
152     };
153
154     template <class T, class A2, class A1>
155     struct RouteParameters<T,A2,A1,1u>
156         : public RouteParameters<A2,A1,mpl::nil>
157     {
158         typedef RouteParameters<A2,A1,mpl::nil> base;
159         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Stream, mpl::nil>::value ));
160         typedef T Stream;
161     };
162
163     template <class T, class A2, class A1>
164     struct RouteParameters<T,A2,A1,2u>
165         : public RouteParameters<A2,A1,mpl::nil>
166     {
167         typedef RouteParameters<A2,A1,mpl::nil> base;
168         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
169         typedef T Area;
170     };
171
172     template <class T, class A2, class A1>
173     struct RouteParameters<T,A2,A1,3u>
174         : public RouteParameters<A2,A1,mpl::nil>
175     {
176         typedef RouteParameters<A2,A1,mpl::nil> base;
177         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
178         typedef typename T::SENFLogArea Area;
179     };
180
181     template <class T, class A2, class A1>
182     struct RouteParameters<T,A2,A1,4u>
183         : public RouteParameters<A2,A1,mpl::nil>
184     {
185         typedef RouteParameters<A2,A1,mpl::nil> base;
186         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Level, NilLevel>::value ));
187         typedef T Level;
188     };
189
190     template <class T, class RV>
191     struct InstanceP
192     {
193         static RV * value() { return & T::instance(); }
194     };
195
196     template <class RV>
197     struct InstanceP<mpl::nil, RV>
198     {
199         static RV * value() { return 0; }
200     };
201
202 #endif
203
204 }}}
205
206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
207 #endif
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // End: