RSC  0.19.0
ConsoleLogger.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  * Copyright (C) 2018 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
7  *
8  * This file may be licensed under the terms of the
9  * GNU Lesser General Public License Version 3 (the ``LGPL''),
10  * or (at your option) any later version.
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the LGPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the LGPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The development of this software was supported by:
23  * CoR-Lab, Research Institute for Cognition and Robotics
24  * Bielefeld University
25  *
26  * ============================================================ */
27 
28 #include "ConsoleLogger.h"
29 
30 #include "../misc/langutils.h"
31 
32 using namespace std;
33 
34 namespace rsc {
35 namespace logging {
36 
37 ConsoleLogger::ConsoleLogger(const string& name, const Level& level) :
38  name(name), level(level) {
39 }
40 
42 }
43 
45  boost::recursive_mutex::scoped_lock lock(mutex);
46  return level;
47 }
48 
50  boost::recursive_mutex::scoped_lock lock(mutex);
51  this->level = level;
52 }
53 
54 string ConsoleLogger::getName() const {
55  boost::recursive_mutex::scoped_lock lock(mutex);
56  return name;
57 }
58 
59 void ConsoleLogger::setName(const string& name) {
60  boost::recursive_mutex::scoped_lock lock(mutex);
61  this->name = name;
62 }
63 
64 ostream& ConsoleLogger::printHeader(ostream& stream, const Level& level) {
65  return stream << rsc::misc::currentTimeMillis() << " " << this->name
66  << " [" << level << "]: ";
67 }
68 
69 std::ostream& ConsoleLogger::printBody(std::ostream& stream,
70  const Level& /*level*/,
71  const std::string& msg) {
72  return stream << msg << endl;
73 }
74 
75 void ConsoleLogger::log(const Level& level, const string& msg) {
76  boost::recursive_mutex::scoped_lock lock(mutex);
77  if (isEnabledFor(level)) {
78  std::ostream& stream = printHeader(cerr, level);
79  printBody(stream, level, msg);
80  }
81 }
82 
83 }
84 }
boost::recursive_mutex mutex
Definition: ConsoleLogger.h:92
void setLevel(const Level &level)
virtual bool isEnabledFor(const Level &level) const
Definition: Logger.cpp:91
STL namespace.
boost::uint64_t currentTimeMillis()
Returns the current system time as milliseconds.
Definition: langutils.cpp:64
Level
Possible logging levels.
Definition: Logger.h:63
virtual std::ostream & printBody(std::ostream &stream, const Level &level, const std::string &msg)
Print the msg as the body of the log message.
Level getLevel() const
Returns the currently defined level of the logger.
std::string getName() const
Returns the hierarchical name of the logger.
virtual std::ostream & printHeader(std::ostream &stream, const Level &level)
Prints a generic header for this logger to the stream.
void log(const Level &level, const std::string &msg)
void setName(const std::string &name)
Sets the name of the logger.