console.cpp
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 #include "console_bridge/console.h"
38 
39 #include <cstdio>
40 #include <cstdarg>
41 
42 #include <iostream>
43 #include <mutex>
44 
46 
47 struct DefaultOutputHandler
48 {
49  DefaultOutputHandler(void)
50  {
51  output_handler_ = static_cast<console_bridge::OutputHandler*>(&std_output_handler_);
52  previous_output_handler_ = output_handler_;
54  }
55 
56  console_bridge::OutputHandlerSTD std_output_handler_;
57  console_bridge::OutputHandler *output_handler_;
58  console_bridge::OutputHandler *previous_output_handler_;
59  console_bridge::LogLevel logLevel_;
60  std::mutex lock_; // it is likely the outputhandler does some I/O, so we serialize it
61 };
62 
63 // we use this function because we want to handle static initialization correctly
64 // however, the first run of this function is not thread safe, due to the use of a static
65 // variable inside the function. For this reason, we ensure the first call happens during
66 // static initialization using a proxy class
67 static DefaultOutputHandler* getDOH(void)
68 {
69  static DefaultOutputHandler DOH;
70  return &DOH;
71 }
72 
73 #define USE_DOH \
74  DefaultOutputHandler *doh = getDOH(); \
75  std::lock_guard<std::mutex> lock_guard(doh->lock_)
76 
77 #define MAX_BUFFER_SIZE 1024
78 
80 
82 {
83  USE_DOH;
84  doh->previous_output_handler_ = doh->output_handler_;
85  doh->output_handler_ = NULL;
86 }
87 
89 {
90  USE_DOH;
91  std::swap(doh->previous_output_handler_, doh->output_handler_);
92 }
93 
95 {
96  USE_DOH;
97  doh->previous_output_handler_ = doh->output_handler_;
98  doh->output_handler_ = oh;
99 }
100 
102 {
103  return getDOH()->output_handler_;
104 }
105 
106 void console_bridge::log_deprecated(const char *file, int line,
107  LogLevel level, const char* m, ...)
108 {
109  /*
110  * Workaround: see bug ros/console 30
111  * Exact copy of console_bridge::log, please remove it once the
112  * deprecation time expires.
113  */
114  USE_DOH;
115  if (doh->output_handler_ && level >= doh->logLevel_)
116  {
117  va_list __ap;
118  va_start(__ap, m);
119  char buf[MAX_BUFFER_SIZE];
120 #ifdef _MSC_VER
121  vsnprintf_s(buf, sizeof(buf), _TRUNCATE, m, __ap);
122 #else
123  vsnprintf(buf, sizeof(buf), m, __ap);
124 #endif
125  va_end(__ap);
126  buf[MAX_BUFFER_SIZE - 1] = '\0';
127 
128  doh->output_handler_->log(buf, level, file, line);
129  }
130 }
131 
132 void console_bridge::log(const char *file, int line, LogLevel level, const char* m, ...)
133 {
134  USE_DOH;
135  if (doh->output_handler_ && level >= doh->logLevel_)
136  {
137  va_list __ap;
138  va_start(__ap, m);
139  char buf[MAX_BUFFER_SIZE];
140 #ifdef _MSC_VER
141  vsnprintf_s(buf, sizeof(buf), _TRUNCATE, m, __ap);
142 #else
143  vsnprintf(buf, sizeof(buf), m, __ap);
144 #endif
145  va_end(__ap);
146  buf[MAX_BUFFER_SIZE - 1] = '\0';
147 
148  doh->output_handler_->log(buf, level, file, line);
149  }
150 }
151 
153 {
154  USE_DOH;
155  doh->logLevel_ = level;
156 }
157 
159 {
160  USE_DOH;
161  return doh->logLevel_;
162 }
163 
164 static const char* LogLevelString[4] = {"Debug: ", "Info: ", "Warning: ", "Error: "};
165 
167 {
169  {
170  std::cerr << LogLevelString[level] << text << std::endl;
171  std::cerr << " at line " << line << " in " << filename << std::endl;
172  std::cerr.flush();
173  }
174  else
175  {
176  std::cout << LogLevelString[level] << text << std::endl;
177  std::cout.flush();
178  }
179 }
180 
182 {
183 #ifdef _MSC_VER
184  errno_t err = fopen_s(&file_, filename, "a");
185  if (err != 0 || !file_)
186 #else
187  file_ = fopen(filename, "a");
188  if (!file_)
189 #endif
190  std::cerr << "Unable to open log file: '" << filename << "'" << std::endl;
191 }
192 
194 {
195  if (file_)
196  if (fclose(file_) != 0)
197  std::cerr << "Error closing logfile" << std::endl;
198 }
199 
201 {
202  if (file_)
203  {
204  fprintf(file_, "%s%s\n", LogLevelString[level], text.c_str());
206  fprintf(file_, " at line %d in %s\n", line, filename);
207  fflush(file_);
208  }
209 }
console_bridge::LogLevel
LogLevel
The set of priorities for message logging.
Definition: console.h:109
console_bridge::log_deprecated
CONSOLE_BRIDGE_DEPRECATED void log_deprecated(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...
Definition: console.cpp:106
console_bridge::CONSOLE_BRIDGE_LOG_WARN
@ CONSOLE_BRIDGE_LOG_WARN
Definition: console.h:113
console_bridge::log
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...
Definition: console.cpp:132
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
console.h
console_bridge::OutputHandlerFile::file_
FILE * file_
The file to save to.
Definition: console.h:171
console_bridge::OutputHandlerSTD
Default implementation of OutputHandler. This sends the information to the console.
Definition: console.h:144
console_bridge::OutputHandlerSTD::log
virtual void log(const std::string &text, LogLevel level, const char *filename, int line)
log a message to the output handler with the given text and logging level from a specific file and li...
Definition: console.cpp:166
m
std::mutex m
Definition: test-waiting-on.cpp:126
console_bridge::OutputHandlerFile::~OutputHandlerFile
virtual ~OutputHandlerFile(void)
Definition: console.cpp:193
LogLevelString
static const char * LogLevelString[4]
Definition: console.cpp:164
console_bridge::restorePreviousOutputHandler
void restorePreviousOutputHandler(void)
Restore the output handler that was previously in use (if any)
Definition: console.cpp:88
console_bridge::getOutputHandler
OutputHandler * getOutputHandler(void)
Get the instance of the OutputHandler currently used. This is NULL in case there is no output handler...
Definition: console.cpp:101
NULL
#define NULL
Definition: tinycthread.c:47
level
GLint level
Definition: glad/glad/glad.h:1412
console_bridge::useOutputHandler
void useOutputHandler(OutputHandler *oh)
Specify the instance of the OutputHandler to use. By default, this is OutputHandlerSTD.
Definition: console.cpp:94
console_bridge::setLogLevel
void setLogLevel(LogLevel level)
Set the minimum level of logging data to output. Messages with lower logging levels will not be recor...
Definition: console.cpp:152
python-tutorial-1-depth.line
string line
Definition: python-tutorial-1-depth.py:38
test-projection-from-recording.filename
filename
Definition: test-projection-from-recording.py:15
console_bridge::OutputHandler
Generic class to handle output from a piece of code.
Definition: console.h:125
console_bridge::OutputHandlerFile::OutputHandlerFile
OutputHandlerFile(const char *filename)
The name of the file in which to save the message data.
Definition: console.cpp:181
console_bridge::getLogLevel
LogLevel getLogLevel(void)
Retrieve the current level of logging data. Messages with lower logging levels will not be recorded.
Definition: console.cpp:158
console_bridge::noOutputHandler
void noOutputHandler(void)
This function instructs ompl that no messages should be outputted. Equivalent to useOutputHandler(NUL...
Definition: console.cpp:81
console_bridge::OutputHandlerFile::log
virtual void log(const std::string &text, LogLevel level, const char *filename, int line)
log a message to the output handler with the given text and logging level from a specific file and li...
Definition: console.cpp:200
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glad/glad/glad.h:3610


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01