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