ros_logger.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 The urg_stamped Authors
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 
17 #include <iostream>
18 #include <sstream>
19 #include <string>
20 
21 #include <ros/ros.h>
22 
23 #include <scip2/logger.h>
24 
25 namespace
26 {
27 class ROSOutStreamBuffer : public std::stringbuf
28 {
29 public:
30  enum Type
31  {
32  LOG_DEBUG,
33  LOG_INFO,
34  LOG_WARN,
35  LOG_ERROR,
36  LOG_FATAL,
37  };
38  Type type_;
39 
40  explicit ROSOutStreamBuffer(Type type)
41  : type_(type)
42  {
43  }
44  virtual int sync()
45  {
46  std::string log = str();
47  if (!log.empty() && log.back() == '\n')
48  log.pop_back();
49 
50  switch (type_)
51  {
52  case LOG_DEBUG:
53  ROS_DEBUG("%s", log.c_str());
54  break;
55  case LOG_INFO:
56  ROS_INFO("%s", log.c_str());
57  break;
58  case LOG_WARN:
59  ROS_WARN("%s", log.c_str());
60  break;
61  case LOG_ERROR:
62  ROS_ERROR("%s", log.c_str());
63  break;
64  case LOG_FATAL:
65  ROS_FATAL("%s", log.c_str());
66  break;
67  }
68  str("");
69  return 0;
70  }
71 };
72 
73 ROSOutStreamBuffer debug_buf(ROSOutStreamBuffer::LOG_DEBUG);
74 ROSOutStreamBuffer info_buf(ROSOutStreamBuffer::LOG_INFO);
75 ROSOutStreamBuffer warn_buf(ROSOutStreamBuffer::LOG_WARN);
76 ROSOutStreamBuffer error_buf(ROSOutStreamBuffer::LOG_ERROR);
77 ROSOutStreamBuffer fatal_buf(ROSOutStreamBuffer::LOG_FATAL);
78 std::ostream debug_logger(&debug_buf);
79 std::ostream info_logger(&info_buf);
80 std::ostream warn_logger(&warn_buf);
81 std::ostream error_logger(&error_buf);
82 std::ostream fatal_logger(&fatal_buf);
83 } // namespace
84 
85 namespace urg_stamped
86 {
88 {
89  scip2::logger::setDebugLogger(&debug_logger);
90  scip2::logger::setInfoLogger(&info_logger);
91  scip2::logger::setWarnLogger(&warn_logger);
92  scip2::logger::setErrorLogger(&error_logger);
93  scip2::logger::setFatalLogger(&fatal_logger);
94 }
95 } // namespace urg_stamped
void setErrorLogger(std::ostream *l)
Definition: logger.cpp:84
void setInfoLogger(std::ostream *l)
Definition: logger.cpp:76
#define ROS_FATAL(...)
#define ROS_WARN(...)
void setDebugLogger(std::ostream *l)
Definition: logger.cpp:72
#define ROS_INFO(...)
void setWarnLogger(std::ostream *l)
Definition: logger.cpp:80
void setFatalLogger(std::ostream *l)
Definition: logger.cpp:88
void setROSLogger()
Definition: ros_logger.cpp:87
#define ROS_ERROR(...)
std::string type_
Definition: logger.cpp:34
#define ROS_DEBUG(...)


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05