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