4d65aa99f5a16ce8672437bf01c8668cce79a5b1
[senf.git] / 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 <memory>
31 #include <boost/type_traits/is_same.hpp>
32 #include <boost/static_assert.hpp>
33 #include "../Console/LazyDirectory.hh"
34
35 ///////////////////////////////ih.p////////////////////////////////////////
36
37 namespace senf {
38 namespace log {
39 namespace detail {
40
41     /** \brief Internal: Target registry */
42     class TargetRegistry
43         : public senf::singleton<TargetRegistry>
44     {
45     public:
46         using senf::singleton<TargetRegistry>::instance;
47
48         void write(StreamBase const & stream, AreaBase const & area, unsigned level, 
49                    std::string const & msg);
50
51         void routed();
52         bool fallbackRouting();
53
54     private:
55         TargetRegistry();
56         
57         void registerTarget(Target * target, std::string const & name);
58         void unregisterTarget(Target * target);
59
60         void consoleAreas(std::ostream & os);
61         void consoleStreams(std::ostream & os);
62
63         typedef std::set<Target *> Targets;
64         Targets targets_;
65
66         bool fallbackRouting_;
67
68         console::LazyDirectory consoleDir_;
69         
70         friend class senf::log::Target;
71         friend class senf::singleton<TargetRegistry>;
72     };
73
74     /** \brief Internal: Write log message */
75     template <class Stream, class Area, class Level>
76     void write(std::string const & msg);
77
78 #ifndef DOXYGEN
79
80     // This code takes the routing target template arguments in any order and sorts them 
81     // by type (Stream, Area and Level).
82
83     senf::mpl::rv<0u> RouteParameterCheck_(...);
84     senf::mpl::rv<1u> RouteParameterCheck_(StreamBase *);
85     senf::mpl::rv<2u> RouteParameterCheck_(AreaBase *);
86     template <class T> senf::mpl::rv<3u> RouteParameterCheck_(T*, typename T::SENFLogArea * = 0);
87     senf::mpl::rv<4u> RouteParameterCheck_(LevelBase *);
88
89     // For g++ 4.0 (at least) we need to provide the fully scoped name for this default value.
90     // no idea why. It works without the scope in 4.1
91     template < class T, class A2, class A1,
92                unsigned type = SENF_MPL_RV( senf::log::detail::RouteParameterCheck_(static_cast<T*>(0)) ) >
93     struct RouteParameters
94     {};
95
96     template <class A2, class A1>
97     struct RouteParameters<mpl::nil,A2,A1,0u>
98         : public RouteParameters<A2,A1,mpl::nil>
99     {};
100
101     struct NilLevel {
102         static unsigned const value = NONE::value;
103     };
104
105     template <>
106     struct RouteParameters<mpl::nil,mpl::nil,mpl::nil,0u>
107     {
108         typedef mpl::nil Stream;
109         typedef mpl::nil Area;
110         typedef NilLevel Level;
111     };
112
113     template <class T, class A2, class A1>
114     struct RouteParameters<T,A2,A1,1u>
115         : public RouteParameters<A2,A1,mpl::nil>
116     {
117         typedef RouteParameters<A2,A1,mpl::nil> base;
118         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Stream, mpl::nil>::value ));
119         typedef T Stream;
120     };
121
122     template <class T, class A2, class A1>
123     struct RouteParameters<T,A2,A1,2u>
124         : public RouteParameters<A2,A1,mpl::nil>
125     {
126         typedef RouteParameters<A2,A1,mpl::nil> base;
127         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
128         typedef T Area;
129     };
130
131     template <class T, class A2, class A1>
132     struct RouteParameters<T,A2,A1,3u>
133         : public RouteParameters<A2,A1,mpl::nil>
134     {
135         typedef RouteParameters<A2,A1,mpl::nil> base;
136         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
137         typedef typename T::SENFLogArea Area;
138     };
139
140     template <class T, class A2, class A1>
141     struct RouteParameters<T,A2,A1,4u>
142         : public RouteParameters<A2,A1,mpl::nil>
143     {
144         typedef RouteParameters<A2,A1,mpl::nil> base;
145         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Level, NilLevel>::value ));
146         typedef T Level;
147     };
148
149     template <class T, class RV>
150     struct InstanceP
151     {
152         static RV * value() { return & T::instance(); }
153     };
154
155     template <class RV>
156     struct InstanceP<mpl::nil, RV>
157     {
158         static RV * value() { return 0; }
159     };
160
161 #endif
162
163 }}}
164
165 ///////////////////////////////ih.e////////////////////////////////////////
166 #endif
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: