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