3c56a5f62d6a06e0c950e67b3142a9e28b7f6cb5
[senf.git] / senf / Utils / StatisticsTargets.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 StatisticsTargets non-inline non-template implementation */
25
26 #include "StatisticsTargets.hh"
27 #include "StatisticsTargets.ih"
28
29 // Custom includes
30 #include <boost/bind.hpp>
31 #include <senf/Utils/Console/ParsedCommand.hh>
32 #include "Statistics.hh"
33
34 //#include "StatisticsTargets.mpp"
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::detail::StatisticsLoggerRegistry
40
41 prefix_ void
42 senf::detail::StatisticsLoggerRegistry::apply(StatisticsBase & stats,
43                                               unsigned rank, console::DirectoryNode & dir)
44 {
45     Adders::const_iterator i (adders_.begin());
46     Adders::const_iterator const i_end (adders_.end());
47     for (; i != i_end; ++i)
48         (*i)(stats, rank, dir);
49 }
50
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52
53 namespace {
54
55     struct RegisterStatisticsLogger
56     {
57         RegisterStatisticsLogger();
58
59         static void adder(senf::StatisticsBase & stats,
60                           unsigned rank, senf::console::DirectoryNode & dir);
61
62         static void consoleCreate(senf::StatisticsBase & stats,
63                                   unsigned rank, std::string const & prefix);
64     };
65
66     RegisterStatisticsLogger registerStatisticsLogger;
67 }
68
69 prefix_ RegisterStatisticsLogger::RegisterStatisticsLogger()
70 {
71     senf::detail::StatisticsLoggerRegistry::instance().add(&adder);
72 }
73
74 prefix_ void RegisterStatisticsLogger::adder(senf::StatisticsBase & stats,
75                                              unsigned rank, senf::console::DirectoryNode & dir)
76 {
77     namespace kw = senf::console::kw;
78     namespace fty = senf::console::factory;
79     dir.add("logger", fty::Command<void (std::string const &)>(
80                 boost::bind(&consoleCreate, boost::ref(stats), rank, _1))
81             .arg("prefix","Optional prefix string to add to each log message",
82                  kw::default_value = "")
83             .doc("Send log messages to statistics log stream") );
84 }
85
86 prefix_ void RegisterStatisticsLogger::consoleCreate(senf::StatisticsBase & stats,
87                                                      unsigned rank, std::string const & prefix)
88 {
89     stats.output(rank).connect(senf::StatisticsLogger(prefix),
90                                "senf::StatisticsLogger(\"" + prefix + "\")");
91 }
92
93 //-/////////////////////////////////////////////////////////////////////////////////////////////////
94 #undef prefix_
95 //#include "StatisticsTargets.mpp"
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // End: