rosconsole_glog.cpp
Go to the documentation of this file.
1 #include "ros/console.h"
2 
3 #include <glog/logging.h>
4 
5 namespace ros
6 {
7 namespace console
8 {
9 namespace impl
10 {
11 
12 std::vector<std::pair<std::string, levels::Level> > rosconsole_glog_log_levels;
14 
15 void initialize()
16 {
17  google::InitGoogleLogging("rosconsole");
18 }
19 
20 std::string getName(void* handle);
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  index++;
79  }
80  // add unknown names on demand with default level
81  rosconsole_glog_log_levels.push_back(std::pair<std::string, levels::Level>(name, ::ros::console::levels::Info));
82  return (void*)(rosconsole_glog_log_levels.size() - 1);
83 }
84 
85 std::string getName(void* handle)
86 {
87  size_t index = (size_t)handle;
88  if(index < rosconsole_glog_log_levels.size())
89  {
90  return rosconsole_glog_log_levels[index].first;
91  }
92  return "";
93 }
94 
96 {
97  rosconsole_glog_appender = appender;
98 }
99 
100 void shutdown()
101 {}
102 
103 bool get_loggers(std::map<std::string, levels::Level>& loggers)
104 {
105  for(std::vector<std::pair<std::string, levels::Level> >::const_iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
106  {
107  loggers[it->first] = it->second;
108  }
109  return true;
110 }
111 
112 bool set_logger_level(const std::string& name, levels::Level level)
113 {
114  for(std::vector<std::pair<std::string, levels::Level> >::iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
115  {
116  if(name == it->first)
117  {
118  it->second = level;
119  return true;
120  }
121  }
122  return false;
123 }
124 
125 } // namespace impl
126 } // namespace console
127 } // namespace ros
void print(void *handle,::ros::console::Level level, const char *str, const char *file, const char *function, int line)
std::string getName(void *handle)
void register_appender(LogAppender *appender)
bool get_loggers(std::map< std::string, levels::Level > &loggers)
bool set_logger_level(const std::string &name, levels::Level level)
bool isEnabledFor(void *handle,::ros::console::Level level)
std::vector< std::pair< std::string, levels::Level > > rosconsole_glog_log_levels
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 Sun Feb 3 2019 03:29:44