RSC  0.16.0
LoggerTreeNode.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSC project.
4  *
5  * Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 #include <boost/enable_shared_from_this.hpp>
34 #include <boost/function.hpp>
35 #include <boost/shared_ptr.hpp>
36 
37 #include "LoggerProxy.h"
38 
39 namespace rsc {
40 namespace logging {
41 
43 typedef boost::shared_ptr<LoggerTreeNode> LoggerTreeNodePtr;
44 typedef boost::weak_ptr<LoggerTreeNode> LoggerTreeNodeWeakPtr;
45 
55 class LoggerTreeNode: public boost::enable_shared_from_this<LoggerTreeNode> {
56 public:
57 
62  typedef std::vector<std::string> NamePath;
63 
71  LoggerTreeNode(const std::string& name, LoggerTreeNodeWeakPtr parent);
72 
80  LoggerTreeNode(const std::string& name, LoggerProxyPtr loggerProxy,
81  LoggerTreeNodeWeakPtr parent);
82 
83  LoggerTreeNodePtr getParent() const;
84 
85  std::string getName() const;
86 
88 
90 
97  bool addChild(LoggerTreeNodePtr child);
98 
99  typedef boost::function<
100  LoggerProxyPtr(const NamePath& name, LoggerTreeNodePtr node)> CreateFunction;
101 
111  LoggerTreeNodePtr addChildren(const NamePath& path,
112  CreateFunction createFn, const NamePath &processedPath = NamePath());
113 
119  class Visitor {
120  public:
121  virtual ~Visitor();
122 
131  virtual bool visit(const NamePath& path, LoggerTreeNodePtr node, const Logger::Level& parentLevel) = 0;
132 
133  };
134  typedef boost::shared_ptr<Visitor> VisitorPtr;
135 
141  void visit(VisitorPtr visitor, const NamePath& thisPath = NamePath());
142 
143  bool hasChild(const std::string& name) const;
144 
145  bool hasChild(const NamePath& path) const;
146 
147  LoggerTreeNodePtr getChild(const std::string& name) const;
148 
149  LoggerTreeNodePtr getChild(const NamePath& path) const;
150 
151  void clearChildren();
152 
161  static NamePath nameToPath(const std::string& name);
162  static std::string pathToName(const NamePath& path);
163 
164  boost::shared_ptr<Logger::Level> getAssignedLevel() const;
165  void setAssignedLevel(boost::shared_ptr<Logger::Level> level);
166  void setAssignedLevel(const Logger::Level& level);
167  bool hasAssignedLevel() const;
168 
169 private:
170 
175  std::string name;
177 
178  LoggerTreeNodeWeakPtr parent;
179  std::map<std::string, LoggerTreeNodePtr> children;
180 
181  boost::shared_ptr<Logger::Level> assignedLevel;
182 
183 };
184 
185 }
186 }
187 
boost::shared_ptr< LoggerProxy > LoggerProxyPtr
Definition: LoggerProxy.h:112
LoggerTreeNodePtr getParent() const
Visitor interface to operate on the tree.
A simple tree representation for loggers.
void setAssignedLevel(boost::shared_ptr< Logger::Level > level)
LoggerTreeNodePtr getChild(const std::string &name) const
LoggerTreeNodeWeakPtr parent
void setLoggerProxy(LoggerProxyPtr loggerProxy)
std::vector< std::string > NamePath
A unique representation of a name.
Level
Possible logging levels.
Definition: Logger.h:63
LoggerProxyPtr getLoggerProxy() const
std::string name
New name part of this node.
LoggerTreeNode(const std::string &name, LoggerTreeNodeWeakPtr parent)
Creates a new node without an assigned LoggerProxy.
bool hasChild(const std::string &name) const
boost::shared_ptr< Logger::Level > getAssignedLevel() const
boost::shared_ptr< LoggerTreeNode > LoggerTreeNodePtr
boost::shared_ptr< Visitor > VisitorPtr
virtual bool visit(const NamePath &path, LoggerTreeNodePtr node, const Logger::Level &parentLevel)=0
Called for each node in the tree.
boost::shared_ptr< Logger::Level > assignedLevel
std::string getName() const
std::map< std::string, LoggerTreeNodePtr > children
boost::function< LoggerProxyPtr(const NamePath &name, LoggerTreeNodePtr node)> CreateFunction
boost::weak_ptr< LoggerTreeNode > LoggerTreeNodeWeakPtr
static std::string pathToName(const NamePath &path)
static NamePath nameToPath(const std::string &name)
Converts a string name of the form a.test.string to a hierarchical logger path representation.
LoggerTreeNodePtr addChildren(const NamePath &path, CreateFunction createFn, const NamePath &processedPath=NamePath())
Retrieves an (indirect) child and creates required ancestors of this child using a custom callback me...
bool addChild(LoggerTreeNodePtr child)
Adds a child if it does not exist so far.