log.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2021 Universal Robots A/S
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the {copyright_holder} nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include "ur_client_library/log.h"
33 #include <cstdarg>
34 #include <cstdio>
35 
36 namespace urcl
37 {
38 class Logger
39 {
40 public:
42  {
44  log_handler_.reset(new DefaultLogHandler());
45  }
46 
48  {
49  if (log_handler_)
50  {
51  log_handler_.reset();
52  }
53  }
54 
55  void registerLogHandler(std::unique_ptr<LogHandler> loghandler)
56  {
57  log_handler_ = std::move(loghandler);
58  }
59 
61  {
62  log_handler_.reset(new DefaultLogHandler());
63  }
64 
65  void log(const char* file, int line, LogLevel level, const char* txt)
66  {
67  if (!log_handler_)
68  {
69  log_handler_.reset(new DefaultLogHandler());
70  }
71 
72  log_handler_->log(file, line, level, txt);
73  }
74 
75  void setLogLevel(LogLevel level)
76  {
77  log_level_ = level;
78  }
79 
81  {
82  return log_level_;
83  }
84 
85 private:
86  std::unique_ptr<LogHandler> log_handler_;
88 };
90 
91 void registerLogHandler(std::unique_ptr<LogHandler> loghandler)
92 {
93  g_logger.registerLogHandler(std::move(loghandler));
94 }
95 
97 {
99 }
100 
102 {
103  g_logger.setLogLevel(level);
104 }
105 
106 void log(const char* file, int line, LogLevel level, const char* fmt, ...)
107 {
108  if (level >= g_logger.getLogLevel())
109  {
110  size_t buffer_size = 1024;
111  std::unique_ptr<char[]> buffer;
112  buffer.reset(new char[buffer_size]);
113 
114  va_list args;
115  va_start(args, fmt);
116  va_list args_copy;
117  va_copy(args_copy, args);
118 
119  size_t characters = 1 + std::vsnprintf(buffer.get(), buffer_size, fmt, args);
120 
121  if (characters >= buffer_size)
122  {
123  buffer_size = characters + 1;
124  buffer.reset(new char[buffer_size]);
125  std::vsnprintf(buffer.get(), buffer_size, fmt, args_copy);
126  }
127 
128  va_end(args);
129  va_end(args_copy);
130 
131  g_logger.log(file, line, level, buffer.get());
132  }
133 }
134 
135 } // namespace urcl
urcl::Logger::log
void log(const char *file, int line, LogLevel level, const char *txt)
Definition: log.cpp:65
urcl::Logger::unregisterLogHandler
void unregisterLogHandler()
Definition: log.cpp:60
urcl::setLogLevel
void setLogLevel(LogLevel level)
Set log level this will disable messages with lower log level.
Definition: log.cpp:101
urcl::g_logger
Logger g_logger
Definition: log.cpp:89
urcl::DefaultLogHandler
LogHandler object for default handling of logging messages. This class is used when no other LogHandl...
Definition: default_log_handler.h:41
urcl::Logger
Definition: log.cpp:38
urcl::Logger::~Logger
~Logger()
Definition: log.cpp:47
urcl
Definition: bin_parser.h:36
urcl::registerLogHandler
void registerLogHandler(std::unique_ptr< LogHandler > loghandler)
Register a new LogHandler object, for handling log messages.
Definition: log.cpp:91
urcl::Logger::registerLogHandler
void registerLogHandler(std::unique_ptr< LogHandler > loghandler)
Definition: log.cpp:55
urcl::unregisterLogHandler
void unregisterLogHandler()
Unregister current log handler, this will enable default log handler.
Definition: log.cpp:96
urcl::Logger::Logger
Logger()
Definition: log.cpp:41
log.h
urcl::Logger::setLogLevel
void setLogLevel(LogLevel level)
Definition: log.cpp:75
urcl::Logger::getLogLevel
LogLevel getLogLevel()
Definition: log.cpp:80
urcl::Logger::log_level_
LogLevel log_level_
Definition: log.cpp:87
urcl::LogLevel::INFO
@ INFO
urcl::log
void log(const char *file, int line, LogLevel level, const char *fmt,...)
Log a message, this is used internally by the macros to unpack the log message. Use the macros instea...
Definition: log.cpp:106
urcl::LogLevel
LogLevel
Different log levels.
Definition: log.h:34
urcl::Logger::log_handler_
std::unique_ptr< LogHandler > log_handler_
Definition: log.cpp:86
default_log_handler.h


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58