switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Logger / Target.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Target internal header */
30
31 #ifndef IH_SENF_Utils_Logger_Target_
32 #define IH_SENF_Utils_Logger_Target_ 1
33
34 // Custom includes
35 #include <set>
36 #include <memory>
37 #include <boost/type_traits/is_same.hpp>
38 #include <boost/static_assert.hpp>
39 #include <boost/shared_ptr.hpp>
40 #include <senf/Utils/singleton.hh>
41 #include <senf/Utils/mpl.hh>
42 #include <senf/Utils/Console/LazyDirectory.hh>
43 #include <senf/Utils/Console/Parse.hh>
44
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 namespace senf {
48
49     namespace console { class DirectoryNode; }
50
51 namespace log {
52 namespace detail {
53
54     struct LogParameters {
55         StreamBase const * stream;
56         AreaBase const * area;
57         unsigned level;
58         void clear();
59         void setDefaults();
60         static LogParameters defaultParameters();
61     };
62
63     std::ostream & operator<<(std::ostream & os, LogParameters const & pm);
64
65     void senf_console_parse_argument(console::ParseCommandInfo::TokensRange const & tokens,
66                                      LogParameters & out);
67
68     /** \brief Internal: Target registry */
69     class TargetRegistry
70         : public senf::singleton<TargetRegistry>
71     {
72     public:
73         enum Level {
74             VERBOSE = senf::log::VERBOSE::value,
75             NOTICE = senf::log::NOTICE::value,
76             MESSAGE = senf::log::MESSAGE::value,
77             IMPORTANT = senf::log::IMPORTANT::value,
78             CRITICAL = senf::log::CRITICAL::value,
79             FATAL = senf::log::FATAL::value
80         };
81
82         using senf::singleton<TargetRegistry>::instance;
83
84         void write(StreamBase const & stream, AreaBase const & area, unsigned level,
85                    std::string const & msg);
86
87         void routed();
88         bool fallbackRouting();
89
90         senf::console::ScopedDirectory<> & consoleDir();
91
92         void dynamicTarget(std::auto_ptr<Target> target);
93
94     private:
95         TargetRegistry();
96         ~TargetRegistry();
97
98         void registerTarget(Target * target, std::string const & name);
99         void unregisterTarget(Target * target);
100
101         void consoleAreas(std::ostream & os);
102         void consoleStreams(std::ostream & os);
103         void consoleWrite(LogParameters parameters, std::string const & msg);
104         void consoleRemoveTarget(Target * target);
105         boost::shared_ptr<senf::console::DirectoryNode> consoleSelf(std::ostream & os);
106
107         typedef std::set<Target *> Targets;
108         Targets targets_;
109
110         bool fallbackRouting_;
111
112         console::LazyDirectory consoleDir_;
113
114         Targets dynamicTargets_;
115
116         friend class senf::log::Target;
117         friend class senf::singleton<TargetRegistry>;
118     };
119
120     /** \brief Internal: Write log message */
121     template <class Stream, class Area, class Level>
122     void write(std::string const & msg);
123
124 #ifndef DOXYGEN
125
126     // This code takes the routing target template arguments in any order and sorts them
127     // by type (Stream, Area and Level).
128
129     senf::mpl::rv<0u> RouteParameterCheck_(...);
130     senf::mpl::rv<1u> RouteParameterCheck_(StreamBase *);
131     senf::mpl::rv<2u> RouteParameterCheck_(AreaBase *);
132     template <class T> senf::mpl::rv<3u> RouteParameterCheck_(T*, typename T::SENFLogArea * = 0);
133     senf::mpl::rv<4u> RouteParameterCheck_(LevelBase *);
134
135     // For g++ 4.0 (at least) we need to provide the fully scoped name for this default value.
136     // no idea why. It works without the scope in 4.1
137     template < class T, class A2, class A1,
138                unsigned type = SENF_MPL_RV( senf::log::detail::RouteParameterCheck_(static_cast<T*>(0)) ) >
139     struct RouteParameters
140     {};
141
142     template <class A2, class A1>
143     struct RouteParameters<mpl::nil,A2,A1,0u>
144         : public RouteParameters<A2,A1,mpl::nil>
145     {};
146
147     struct NilLevel {
148         static unsigned const value = NONE::value;
149     };
150
151     template <>
152     struct RouteParameters<mpl::nil,mpl::nil,mpl::nil,0u>
153     {
154         typedef mpl::nil Stream;
155         typedef mpl::nil Area;
156         typedef NilLevel Level;
157     };
158
159     template <class T, class A2, class A1>
160     struct RouteParameters<T,A2,A1,1u>
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::Stream, mpl::nil>::value ));
165         typedef T Stream;
166     };
167
168     template <class T, class A2, class A1>
169     struct RouteParameters<T,A2,A1,2u>
170         : public RouteParameters<A2,A1,mpl::nil>
171     {
172         typedef RouteParameters<A2,A1,mpl::nil> base;
173         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
174         typedef T Area;
175     };
176
177     template <class T, class A2, class A1>
178     struct RouteParameters<T,A2,A1,3u>
179         : public RouteParameters<A2,A1,mpl::nil>
180     {
181         typedef RouteParameters<A2,A1,mpl::nil> base;
182         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Area, mpl::nil>::value ));
183         typedef typename T::SENFLogArea Area;
184     };
185
186     template <class T, class A2, class A1>
187     struct RouteParameters<T,A2,A1,4u>
188         : public RouteParameters<A2,A1,mpl::nil>
189     {
190         typedef RouteParameters<A2,A1,mpl::nil> base;
191         BOOST_STATIC_ASSERT(( boost::is_same<typename base::Level, NilLevel>::value ));
192         typedef T Level;
193     };
194
195     template <class T, class RV>
196     struct InstanceP
197     {
198         static RV * value() { return & T::instance(); }
199     };
200
201     template <class RV>
202     struct InstanceP<mpl::nil, RV>
203     {
204         static RV * value() { return 0; }
205     };
206
207 #endif
208
209 }}}
210
211 //-/////////////////////////////////////////////////////////////////////////////////////////////////
212 #endif
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
218 // comment-column: 40
219 // c-file-style: "senf"
220 // indent-tabs-mode: nil
221 // ispell-local-dictionary: "american"
222 // compile-command: "scons -u test"
223 // End: