sick_scan_logging.h
Go to the documentation of this file.
1 /*
2  * macro definitions for logging to support both ros and non-ros.
3  *
4  * Copyright (C) 2022, Ing.-Buero Dr. Michael Lehning, Hildesheim
5  * Copyright (C) 2022, SICK AG, Waldkirch
6  * All rights reserved.
7  *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 *
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are met:
25 *
26 * * Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * * Neither the name of Osnabrueck University nor the names of its
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission.
34 * * Neither the name of SICK AG nor the names of its
35 * contributors may be used to endorse or promote products derived from
36 * this software without specific prior written permission
37 * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
38 * contributors may be used to endorse or promote products derived from
39 * this software without specific prior written permission
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE.
52  *
53  * Authors:
54  * Michael Lehning <michael.lehning@lehning.de>
55  *
56  */
57 
58 #ifndef SICK_LOGGING_H_INCLUDED
59 #define SICK_LOGGING_H_INCLUDED
60 #include <string>
61 #include <sstream>
62 
63 // fprintf-like conversion of va_args to string, thanks to https://codereview.stackexchange.com/questions/115760/use-va-list-to-format-a-string
64 #ifdef WIN32
65 std::string vargs_to_string(const char* const format, ...);
66 #else
67 std::string vargs_to_string(const char *const format, ...) __attribute__ ((format (printf, 1, 2)));
68 #endif
69 
70 // Common diagnostic status codes for API and ROS
72 {
73  OK = 0, // Normal operation mode: lidar is initialized, sick_scan_xd receives scan data, poinclouds are published (equivalent to ros: diagnostic_msgs::DiagnosticStatus::OK)
74  WARN = 1, // non-fatal warning, operation continues (equivalent to ros: diagnostic_msgs::DiagnosticStatus::WARN)
75  SICK_DIAG_ERROR = 2, // general error, should not occure (equivalent to ros: diagnostic_msgs::DiagnosticStatus::ERROR)
76  INIT = 3, // Initialization (startup) phase: establishing communication or initializing lidar after start or reconnect
77  EXIT = 4 // sick_scan_xd exits
78 };
79 
80 #define SICK_DIAGNOSTIC_STATUS_WARN (SICK_DIAGNOSTIC_STATUS::WARN)
81 #define SICK_DIAGNOSTIC_STATUS_ERROR (SICK_DIAGNOSTIC_STATUS::SICK_DIAG_ERROR)
82 
83 // Set the global diagnostic status and message (OK, WARN, ERROR, INIT or EXIT)
84 void setDiagnosticStatus(SICK_DIAGNOSTIC_STATUS status_code, const std::string& status_message);
85 
86 // Returns the global diagnostic status and message (OK, WARN, ERROR, INIT or EXIT)
87 void getDiagnosticStatus(SICK_DIAGNOSTIC_STATUS& status_code, std::string& status_message);
88 
89 // Notifies all registered log message listener, i.e. all registered listener callbacks are called for all messages of type INFO, WARN, ERROR or FATAL
90 void notifyLogMessageListener(int msg_level, const std::string& message);
91 
92 // Notifies all registered listener about a new diagnostic status
93 void notifyDiagnosticListener(SICK_DIAGNOSTIC_STATUS status_code, const std::string& status_message);
94 
95 // Set verbose level 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR, 4=FATAL or 5=QUIET (equivalent to ros::console::levels),
96 // i.e. print messages on console above the given verbose level.
97 // Default verbose level is 1 (INFO), i.e. print informational, warnings and error messages.
98 void setVerboseLevel(int32_t verbose_level);
99 
100 // Returns the current verbose level 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR, 4=FATAL or 5=QUIET. Default verbose level is 1 (INFO)
101 int32_t getVerboseLevel();
102 
103 #if __ROS_VERSION <= 1 // i.e. native Linux or Windows or ROS-1
104 
105 #define SICK_INFO_LOG(ros_level,...) do{ std::string _msg=vargs_to_string(__VA_ARGS__); if(ros_level>=getVerboseLevel()){ROS_LOG(ros_level,ROSCONSOLE_DEFAULT_NAME,__VA_ARGS__);} notifyLogMessageListener(ros_level,_msg); }while(0)
106 #define SICK_INFO_LOG_STREAM(ros_level,args) do{ std::stringstream _msg; _msg<<args; if(ros_level>=getVerboseLevel()){ROS_LOG_STREAM(ros_level,ROSCONSOLE_DEFAULT_NAME,args);} notifyLogMessageListener(ros_level,_msg.str()); }while(0)
107 #define SICK_ERROR_LOG(ros_level,diag_status,...) do{ std::string _msg=vargs_to_string(__VA_ARGS__); setDiagnosticStatus(diag_status,_msg); if(ros_level>=getVerboseLevel()){ROS_LOG(ros_level,ROSCONSOLE_DEFAULT_NAME,__VA_ARGS__);} notifyLogMessageListener(ros_level,_msg); }while(0)
108 #define SICK_ERROR_LOG_STREAM(ros_level,diag_status,args) do{ std::stringstream _msg; _msg<<args; setDiagnosticStatus(diag_status,_msg.str()); if(ros_level>=getVerboseLevel()){ROS_LOG_STREAM(ros_level,ROSCONSOLE_DEFAULT_NAME,args);} notifyLogMessageListener(ros_level,_msg.str()); }while(0)
109 
110 #undef ROS_DEBUG
111 #undef ROS_DEBUG_STREAM
112 #define ROS_DEBUG(...) SICK_INFO_LOG(::ros::console::levels::Debug,__VA_ARGS__)
113 #define ROS_DEBUG_STREAM(args) SICK_INFO_LOG_STREAM(::ros::console::levels::Debug,args)
114 
115 #undef ROS_INFO
116 #undef ROS_INFO_STREAM
117 #define ROS_INFO(...) SICK_INFO_LOG(::ros::console::levels::Info,__VA_ARGS__)
118 #define ROS_INFO_STREAM(args) SICK_INFO_LOG_STREAM(::ros::console::levels::Info,args)
119 
120 #undef ROS_WARN
121 #undef ROS_WARN_STREAM
122 #define ROS_WARN(...) SICK_ERROR_LOG(::ros::console::levels::Warn,SICK_DIAGNOSTIC_STATUS_WARN,__VA_ARGS__)
123 #define ROS_WARN_STREAM(args) SICK_ERROR_LOG_STREAM(::ros::console::levels::Warn,SICK_DIAGNOSTIC_STATUS_WARN,args)
124 
125 #undef ROS_ERROR
126 #undef ROS_ERROR_STREAM
127 #define ROS_ERROR(...) SICK_ERROR_LOG(::ros::console::levels::Error,SICK_DIAGNOSTIC_STATUS_ERROR,__VA_ARGS__)
128 #define ROS_ERROR_STREAM(args) SICK_ERROR_LOG_STREAM(::ros::console::levels::Error,SICK_DIAGNOSTIC_STATUS_ERROR,args)
129 
130 #undef ROS_FATAL
131 #undef ROS_FATAL_STREAM
132 #define ROS_FATAL(...) SICK_ERROR_LOG(::ros::console::levels::Fatal,SICK_DIAGNOSTIC_STATUS_ERROR,__VA_ARGS__)
133 #define ROS_FATAL_STREAM(args) SICK_ERROR_LOG_STREAM(::ros::console::levels::Fatal,SICK_DIAGNOSTIC_STATUS_ERROR,args)
134 
135 #elif __ROS_VERSION == 2 // i.e. ROS-2
136 
137 #define SICK_INFO_LOG(ros_level,...) do{ std::string _msg=vargs_to_string(__VA_ARGS__); notifyLogMessageListener(ros_level,_msg); }while(0)
138 #define SICK_INFO_LOG_STREAM(ros_level,args) do{ std::stringstream _msg; _msg<<args; notifyLogMessageListener(ros_level,_msg.str()); }while(0)
139 #define SICK_ERROR_LOG(ros_level,diag_status,...) do{ std::string _msg=vargs_to_string(__VA_ARGS__); setDiagnosticStatus(diag_status,_msg); notifyLogMessageListener(ros_level,_msg); }while(0)
140 #define SICK_ERROR_LOG_STREAM(ros_level,diag_status,args) do{ std::stringstream _msg; _msg<<args; setDiagnosticStatus(diag_status,_msg.str()); notifyLogMessageListener(ros_level,_msg.str()); }while(0)
141 
142 #define RCLCPP_LOGGER rclcpp::get_logger("sick_scan_xd")
143 #define ROS_FATAL(...) do{ SICK_ERROR_LOG(4,SICK_DIAGNOSTIC_STATUS_ERROR,__VA_ARGS__); RCLCPP_FATAL(RCLCPP_LOGGER,__VA_ARGS__); }while(0)
144 #define ROS_ERROR(...) do{ SICK_ERROR_LOG(3,SICK_DIAGNOSTIC_STATUS_ERROR,__VA_ARGS__); RCLCPP_ERROR(RCLCPP_LOGGER,__VA_ARGS__); }while(0)
145 #define ROS_WARN(...) do{ SICK_ERROR_LOG(2,SICK_DIAGNOSTIC_STATUS_WARN,__VA_ARGS__); RCLCPP_WARN(RCLCPP_LOGGER,__VA_ARGS__); }while(0)
146 #define ROS_INFO(...) do{ SICK_INFO_LOG(1,__VA_ARGS__); RCLCPP_INFO(RCLCPP_LOGGER,__VA_ARGS__); }while(0)
147 #define ROS_DEBUG(...) do{ RCLCPP_DEBUG(RCLCPP_LOGGER,__VA_ARGS__); }while(0)
148 #define ROS_FATAL_STREAM(args) do{ SICK_ERROR_LOG_STREAM(4,SICK_DIAGNOSTIC_STATUS_ERROR,args); RCLCPP_FATAL_STREAM(RCLCPP_LOGGER,args); }while(0)
149 #define ROS_ERROR_STREAM(args) do{ SICK_ERROR_LOG_STREAM(3,SICK_DIAGNOSTIC_STATUS_ERROR,args); RCLCPP_ERROR_STREAM(RCLCPP_LOGGER,args); }while(0)
150 #define ROS_WARN_STREAM(args) do{ SICK_ERROR_LOG_STREAM(2,SICK_DIAGNOSTIC_STATUS_WARN,args); RCLCPP_WARN_STREAM(RCLCPP_LOGGER,args); }while(0)
151 #define ROS_INFO_STREAM(args) do{ SICK_INFO_LOG_STREAM(1,args); RCLCPP_INFO_STREAM(RCLCPP_LOGGER,args); }while(0)
152 #define ROS_DEBUG_STREAM(args) do{ RCLCPP_DEBUG_STREAM(RCLCPP_LOGGER,args); }while(0)
153 
154 #endif // __ROS_VERSION
155 #endif // SICK_LOGGING_H_INCLUDED
notifyDiagnosticListener
void notifyDiagnosticListener(SICK_DIAGNOSTIC_STATUS status_code, const std::string &status_message)
Definition: api_impl.cpp:1492
vargs_to_string
std::string vargs_to_string(const char *const format,...) __attribute__((format(printf
getVerboseLevel
int32_t getVerboseLevel()
Definition: sick_generic_laser.cpp:329
INIT
@ INIT
Definition: sick_scan_logging.h:76
notifyLogMessageListener
void notifyLogMessageListener(int msg_level, const std::string &message)
Definition: api_impl.cpp:1480
WARN
@ WARN
Definition: sick_scan_logging.h:74
setVerboseLevel
void setVerboseLevel(int32_t verbose_level)
Definition: sick_generic_laser.cpp:323
OK
@ OK
Definition: sick_scan_logging.h:73
setDiagnosticStatus
void setDiagnosticStatus(SICK_DIAGNOSTIC_STATUS status_code, const std::string &status_message)
Definition: sick_generic_laser.cpp:302
sick_scan_xd_api_test.verbose_level
verbose_level
Definition: sick_scan_xd_api_test.py:346
getDiagnosticStatus
void getDiagnosticStatus(SICK_DIAGNOSTIC_STATUS &status_code, std::string &status_message)
Definition: sick_generic_laser.cpp:314
SICK_DIAG_ERROR
@ SICK_DIAG_ERROR
Definition: sick_scan_logging.h:75
SICK_DIAGNOSTIC_STATUS
SICK_DIAGNOSTIC_STATUS
Definition: sick_scan_logging.h:71
EXIT
@ EXIT
Definition: sick_scan_logging.h:77


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:11