console_bridge_dev  master
It is a ROS-independent, pure CMake (i.e. non-catkin and non-rosbuild package) that provides logging calls that mirror those found in rosconsole, but for applications that are not necessarily using ROS.
console.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ryan Luna, Ioan Sucan */
36 
37 #ifndef INCLUDE_CONSOLE_BRIDGE_CONSOLE_H_
38 #define INCLUDE_CONSOLE_BRIDGE_CONSOLE_H_
39 
40 #include <string>
41 
42 #include "./console_bridge_export.h"
43 
66 #define CONSOLE_BRIDGE_logError(...) \
67  console_bridge::log(__FILE__, __LINE__, console_bridge::CONSOLE_BRIDGE_LOG_ERROR, __VA_ARGS__)
68 
69 #define CONSOLE_BRIDGE_logWarn(...) \
70  console_bridge::log(__FILE__, __LINE__, console_bridge::CONSOLE_BRIDGE_LOG_WARN, __VA_ARGS__)
71 
72 #define CONSOLE_BRIDGE_logInform(...) \
73  console_bridge::log(__FILE__, __LINE__, console_bridge::CONSOLE_BRIDGE_LOG_INFO, __VA_ARGS__)
74 
75 #define CONSOLE_BRIDGE_logDebug(...) \
76  console_bridge::log(__FILE__, __LINE__, console_bridge::CONSOLE_BRIDGE_LOG_DEBUG, __VA_ARGS__)
77 
82 namespace console_bridge
83 {
85 enum CONSOLE_BRIDGE_DLLAPI LogLevel
86  {
91  CONSOLE_BRIDGE_LOG_NONE
92  };
93 
100 class CONSOLE_BRIDGE_DLLAPI OutputHandler
101 {
102 public:
104 
105  virtual ~OutputHandler(void){}
106 
115  virtual void log(const std::string &text, LogLevel level, const char *filename, int line) = 0;
116 };
117 
121 class CONSOLE_BRIDGE_DLLAPI OutputHandlerSTD : public OutputHandler
122 {
123 public:
125 
126  virtual void log(const std::string &text, LogLevel level, const char *filename, int line);
127 };
128 
130 class CONSOLE_BRIDGE_DLLAPI OutputHandlerFile : public OutputHandler
131 {
132 public:
134  explicit OutputHandlerFile(const char *filename);
135 
136  virtual ~OutputHandlerFile(void);
137 
138  virtual void log(const std::string &text, LogLevel level, const char *filename, int line);
139 
140 private:
142  FILE *file_;
143 };
144 
149 CONSOLE_BRIDGE_DLLAPI void noOutputHandler(void);
150 
154 CONSOLE_BRIDGE_DLLAPI void restorePreviousOutputHandler(void);
155 
160 CONSOLE_BRIDGE_DLLAPI void useOutputHandler(OutputHandler *oh);
161 
166 CONSOLE_BRIDGE_DLLAPI OutputHandler* getOutputHandler(void);
167 
172 CONSOLE_BRIDGE_DLLAPI void setLogLevel(LogLevel level);
173 
178 CONSOLE_BRIDGE_DLLAPI LogLevel getLogLevel(void);
179 
185 CONSOLE_BRIDGE_DLLAPI void log(const char *file,
186  int line,
187  LogLevel level,
188  const char* m,
189  ...);
190 } // namespace console_bridge
191 
192 #endif // INCLUDE_CONSOLE_BRIDGE_CONSOLE_H_
console_bridge::getLogLevel
CONSOLE_BRIDGE_DLLAPI LogLevel getLogLevel(void)
Retrieve the current level of logging data. Messages with lower logging levels will not be recorded.
console_bridge::restorePreviousOutputHandler
CONSOLE_BRIDGE_DLLAPI void restorePreviousOutputHandler(void)
Restore the output handler that was previously in use (if any)
console_bridge::noOutputHandler
CONSOLE_BRIDGE_DLLAPI void noOutputHandler(void)
This function instructs console bridge that no messages should be outputted. Equivalent to useOutputH...
console_bridge
Message namespace. This contains classes needed to output error messages (or logging) from within the...
Definition: console.h:82
CONSOLE_BRIDGE_LOG_ERROR
CONSOLE_BRIDGE_LOG_ERROR
Definition: console.h:90
console_bridge::OutputHandlerSTD::OutputHandlerSTD
OutputHandlerSTD(void)
Definition: console.h:124
console_bridge::OutputHandler::OutputHandler
OutputHandler(void)
Definition: console.h:103
CONSOLE_BRIDGE_LOG_DEBUG
CONSOLE_BRIDGE_LOG_DEBUG
Definition: console.h:87
console_bridge::OutputHandlerSTD
Default implementation of OutputHandler. This sends the information to the console.
Definition: console.h:121
console_bridge::useOutputHandler
CONSOLE_BRIDGE_DLLAPI void useOutputHandler(OutputHandler *oh)
Specify the instance of the OutputHandler to use. By default, this is OutputHandlerSTD.
console_bridge::setLogLevel
CONSOLE_BRIDGE_DLLAPI void setLogLevel(LogLevel level)
Set the minimum level of logging data to output. Messages with lower logging levels will not be recor...
console_bridge::OutputHandlerFile
Implementation of OutputHandler that saves messages in a file.
Definition: console.h:130
console_bridge::OutputHandler::~OutputHandler
virtual ~OutputHandler(void)
Definition: console.h:105
console_bridge::OutputHandler
Generic class to handle output from a piece of code.
Definition: console.h:100
CONSOLE_BRIDGE_LOG_INFO
CONSOLE_BRIDGE_LOG_INFO
Definition: console.h:88
CONSOLE_BRIDGE_LOG_WARN
CONSOLE_BRIDGE_LOG_WARN
Definition: console.h:89
console_bridge::getOutputHandler
CONSOLE_BRIDGE_DLLAPI OutputHandler * getOutputHandler(void)
Get the instance of the OutputHandler currently used. This is NULL in case there is no output handler...
console_bridge::log
CONSOLE_BRIDGE_DLLAPI void log(const char *file, int line, LogLevel level, const char *m,...)
Root level logging function. This should not be invoked directly, but rather used via a logging macro...