rosconsole_glog.cpp
Go to the documentation of this file.
1 #include "ros/console.h"
2 #define ROSCONSOLE_CONSOLE_IMPL_EXPORTS
3 #include "ros/console_impl.h"
4 
5 #include <glog/logging.h>
6 
7 namespace ros
8 {
9 namespace console
10 {
11 namespace impl
12 {
13 
14 std::vector<std::pair<std::string, levels::Level> > rosconsole_glog_log_levels;
16 
17 void initialize()
18 {
19  google::InitGoogleLogging("rosconsole");
20 }
21 
22 void print(void* handle, ::ros::console::Level level, const char* str, const char* file, const char* function, int line)
23 {
24  // still printing to console
25  ::ros::console::backend::print(0, level, str, file, function, line);
26 
27  // pass log message to appender
29  {
30  rosconsole_glog_appender->log(level, str, file, function, line);
31  }
32 
33  google::LogSeverity glog_level;
34  if(level == ::ros::console::levels::Info)
35  {
36  glog_level = google::GLOG_INFO;
37  }
38  else if(level == ::ros::console::levels::Warn)
39  {
40  glog_level = google::GLOG_WARNING;
41  }
42  else if(level == ::ros::console::levels::Error)
43  {
44  glog_level = google::GLOG_ERROR;
45  }
46  else if(level == ::ros::console::levels::Fatal)
47  {
48  glog_level = google::GLOG_FATAL;
49  }
50  else
51  {
52  // ignore debug
53  return;
54  }
55  std::string name = getName(handle);
56  google::LogMessage(file, line, glog_level).stream() << name << ": " << str;
57 }
58 
59 bool isEnabledFor(void* handle, ::ros::console::Level level)
60 {
61  size_t index = (size_t)handle;
62  if(index < rosconsole_glog_log_levels.size())
63  {
64  return level >= rosconsole_glog_log_levels[index].second;
65  }
66  return false;
67 }
68 
69 void* getHandle(const std::string& name)
70 {
71  size_t count = rosconsole_glog_log_levels.size();
72  for(size_t index = 0; index < count; index++)
73  {
74  if(name == rosconsole_glog_log_levels[index].first)
75  {
76  return (void*)index;
77  }
78  }
79  // add unknown names on demand with default level
80  rosconsole_glog_log_levels.push_back(std::pair<std::string, levels::Level>(name, ::ros::console::levels::Info));
81  return (void*)(rosconsole_glog_log_levels.size() - 1);
82 }
83 
84 std::string getName(void* handle)
85 {
86  size_t index = (size_t)handle;
87  if(index < rosconsole_glog_log_levels.size())
88  {
89  return rosconsole_glog_log_levels[index].first;
90  }
91  return "";
92 }
93 
95 {
96  rosconsole_glog_appender = appender;
97 }
98 
100 {
101  if(rosconsole_glog_appender == appender)
102  {
104  }
105 }
106 
107 void shutdown()
108 {}
109 
110 bool get_loggers(std::map<std::string, levels::Level>& loggers)
111 {
112  for(std::vector<std::pair<std::string, levels::Level> >::const_iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
113  {
114  loggers[it->first] = it->second;
115  }
116  return true;
117 }
118 
119 bool set_logger_level(const std::string& name, levels::Level level)
120 {
121  for(std::vector<std::pair<std::string, levels::Level> >::iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
122  {
123  if(name == it->first)
124  {
125  it->second = level;
126  return true;
127  }
128  }
129  return false;
130 }
131 
132 } // namespace impl
133 } // namespace console
134 } // namespace ros
ros::console::impl::get_loggers
ROSCONSOLE_CONSOLE_IMPL_DECL bool get_loggers(std::map< std::string, levels::Level > &loggers)
Definition: rosconsole_glog.cpp:110
print
void print(ros::console::Level level, const std::string &s)
Definition: example.cpp:38
ros::console::levels::Error
@ Error
Definition: console_backend.h:58
ros::console::impl::rosconsole_glog_log_levels
std::vector< std::pair< std::string, levels::Level > > rosconsole_glog_log_levels
Definition: rosconsole_glog.cpp:14
ros
ros::console::impl::getName
ROSCONSOLE_CONSOLE_IMPL_DECL std::string getName(void *handle)
Definition: rosconsole_glog.cpp:84
ros::console::LogAppender
Definition: console.h:100
console.h
ros::console::impl::getHandle
ROSCONSOLE_CONSOLE_IMPL_DECL void * getHandle(const std::string &name)
Definition: rosconsole_glog.cpp:69
ros::console::impl::print
ROSCONSOLE_CONSOLE_IMPL_DECL void print(void *handle, ::ros::console::Level level, const char *str, const char *file, const char *function, int line)
Definition: rosconsole_glog.cpp:22
ros::console::levels::Level
Level
Definition: console_backend.h:53
ros::console::levels::Info
@ Info
Definition: console_backend.h:56
console_impl.h
ros::console::impl::shutdown
ROSCONSOLE_CONSOLE_IMPL_DECL void shutdown()
Definition: rosconsole_glog.cpp:107
ros::console::levels::Fatal
@ Fatal
Definition: console_backend.h:59
ros::console::impl::isEnabledFor
ROSCONSOLE_CONSOLE_IMPL_DECL bool isEnabledFor(void *handle, ::ros::console::Level level)
Definition: rosconsole_glog.cpp:59
ros::console::impl::deregister_appender
ROSCONSOLE_CONSOLE_IMPL_DECL void deregister_appender(LogAppender *appender)
Definition: rosconsole_glog.cpp:99
ros::console::impl::rosconsole_glog_appender
LogAppender * rosconsole_glog_appender
Definition: rosconsole_glog.cpp:15
ros::console::levels::Warn
@ Warn
Definition: console_backend.h:57
ros::console::impl::initialize
ROSCONSOLE_CONSOLE_IMPL_DECL void initialize()
Definition: rosconsole_glog.cpp:17
ros::console::LogAppender::log
virtual void log(::ros::console::Level level, const char *str, const char *file, const char *function, int line)=0
ros::console::impl::set_logger_level
ROSCONSOLE_CONSOLE_IMPL_DECL bool set_logger_level(const std::string &name, levels::Level level)
Definition: rosconsole_glog.cpp:119
ros::console::impl::register_appender
ROSCONSOLE_CONSOLE_IMPL_DECL void register_appender(LogAppender *appender)
Definition: rosconsole_glog.cpp:94


rosconsole
Author(s): Josh Faust
autogenerated on Wed Mar 2 2022 00:53:52