log.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2021 Universal Robots A/S
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 // All source code contained in and/or linked to in this message (the “Source Code”) is subject to the copyright of
17 // Universal Robots A/S and/or its licensors. THE SOURCE CODE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 // OR IMPLIED, INCLUDING – BUT NOT LIMITED TO – WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
19 // NONINFRINGEMENT. USE OF THE SOURCE CODE IS AT YOUR OWN RISK AND UNIVERSAL ROBOTS A/S AND ITS LICENSORS SHALL, TO THE
20 // MAXIMUM EXTENT PERMITTED BY LAW, NOT BE LIABLE FOR ANY ERRORS OR MALICIOUS CODE IN THE SOURCE CODE, ANY THIRD-PARTY
21 // CLAIMS, OR ANY OTHER CLAIMS AND DAMAGES, INCLUDING INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES,
22 // OR ANY LOSS OF PROFITS, EXPECTED SAVINGS, OR REVENUES, WHETHER INCURRED DIRECTLY OR INDIRECTLY, OR ANY LOSS OF DATA,
23 // USE, GOODWILL, OR OTHER INTANGIBLE LOSSES, RESULTING FROM YOUR USE OF THE SOURCE CODE. You may make copies of the
24 // Source Code for use in connection with a Universal Robots or UR+ product, provided that you include (i) an
25 // appropriate copyright notice (“© [the year in which you received the Source Code or the Source Code was first
26 // published, e.g. “2021”] Universal Robots A/S and/or its licensors”) along with the capitalized section of this notice
27 // in all copies of the Source Code. By using the Source Code, you agree to the above terms. For more information,
28 // please contact legal@universal-robots.com.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include "ur_client_library/log.h"
33 #include <cstdarg>
34 
35 namespace urcl
36 {
37 class Logger
38 {
39 public:
41  {
43  log_handler_.reset(new DefaultLogHandler());
44  }
45 
47  {
48  if (log_handler_)
49  {
50  log_handler_.reset();
51  }
52  }
53 
54  void registerLogHandler(std::unique_ptr<LogHandler> loghandler)
55  {
56  log_handler_ = std::move(loghandler);
57  }
58 
60  {
61  log_handler_.reset(new DefaultLogHandler());
62  }
63 
64  void log(const char* file, int line, LogLevel level, const char* txt)
65  {
66  if (!log_handler_)
67  {
68  log_handler_.reset(new DefaultLogHandler());
69  }
70 
71  log_handler_->log(file, line, level, txt);
72  }
73 
74  void setLogLevel(LogLevel level)
75  {
76  log_level_ = level;
77  }
78 
80  {
81  return log_level_;
82  }
83 
84 private:
85  std::unique_ptr<LogHandler> log_handler_;
87 };
89 
90 void registerLogHandler(std::unique_ptr<LogHandler> loghandler)
91 {
92  g_logger.registerLogHandler(std::move(loghandler));
93 }
94 
96 {
97  g_logger.unregisterLogHandler();
98 }
99 
101 {
102  g_logger.setLogLevel(level);
103 }
104 
105 void log(const char* file, int line, LogLevel level, const char* fmt, ...)
106 {
107  if (level >= g_logger.getLogLevel())
108  {
109  size_t buffer_size = 1024;
110  std::unique_ptr<char> buffer;
111  buffer.reset(new char[buffer_size]);
112 
113  va_list args;
114  va_start(args, fmt);
115  va_list args_copy;
116  va_copy(args_copy, args);
117 
118  size_t characters = 1 + std::vsnprintf(buffer.get(), buffer_size, fmt, args);
119 
120  if (characters >= buffer_size)
121  {
122  buffer_size = characters + 1;
123  buffer.reset(new char[buffer_size]);
124  std::vsnprintf(buffer.get(), buffer_size, fmt, args_copy);
125  }
126 
127  va_end(args);
128  va_end(args_copy);
129 
130  g_logger.log(file, line, level, buffer.get());
131  }
132 }
133 
134 } // namespace urcl
Logger g_logger
Definition: log.cpp:88
LogLevel getLogLevel()
Definition: log.cpp:79
void log(const char *file, int line, LogLevel level, const char *txt)
Definition: log.cpp:64
LogHandler object for default handling of logging messages. This class is used when no other LogHandl...
LogLevel log_level_
Definition: log.cpp:86
void unregisterLogHandler()
Definition: log.cpp:59
void setLogLevel(LogLevel level)
Definition: log.cpp:74
~Logger()
Definition: log.cpp:46
std::unique_ptr< LogHandler > log_handler_
Definition: log.cpp:85
Logger()
Definition: log.cpp:40
LogLevel
Different log levels.
Definition: log.h:47
void registerLogHandler(std::unique_ptr< LogHandler > loghandler)
Definition: log.cpp:54


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26