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
28  if(rosconsole_glog_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  {
103  rosconsole_glog_appender = 0;
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
ROSCONSOLE_CONSOLE_IMPL_DECL void print(void *handle, ::ros::console::Level level, const char *str, const char *file, const char *function, int line)
ROSCONSOLE_CONSOLE_IMPL_DECL void shutdown()
ROSCONSOLE_CONSOLE_IMPL_DECL std::string getName(void *handle)
ROSCONSOLE_CONSOLE_IMPL_DECL void register_appender(LogAppender *appender)
ROSCONSOLE_CONSOLE_IMPL_DECL void initialize()
ROSCONSOLE_CONSOLE_IMPL_DECL bool get_loggers(std::map< std::string, levels::Level > &loggers)
ROSCONSOLE_CONSOLE_IMPL_DECL bool set_logger_level(const std::string &name, levels::Level level)
ROSCONSOLE_CONSOLE_IMPL_DECL bool isEnabledFor(void *handle, ::ros::console::Level level)
ROSCONSOLE_CONSOLE_IMPL_DECL void deregister_appender(LogAppender *appender)
std::vector< std::pair< std::string, levels::Level > > rosconsole_glog_log_levels
ROSCONSOLE_CONSOLE_IMPL_DECL void * getHandle(const std::string &name)
void print(ros::console::Level level, const std::string &s)
Definition: example.cpp:38
LogAppender * rosconsole_glog_appender
virtual void log(::ros::console::Level level, const char *str, const char *file, const char *function, int line)=0


rosconsole
Author(s): Josh Faust
autogenerated on Mon Feb 28 2022 23:30:41