rosconsole_glog.cpp
Go to the documentation of this file.
00001 #include "ros/console.h"
00002 
00003 #include <glog/logging.h>
00004 
00005 namespace ros
00006 {
00007 namespace console
00008 {
00009 namespace impl
00010 {
00011 
00012 std::vector<std::pair<std::string, levels::Level> > rosconsole_glog_log_levels;
00013 LogAppender* rosconsole_glog_appender = 0;
00014 
00015 void initialize()
00016 {
00017   google::InitGoogleLogging("rosconsole");
00018 }
00019 
00020 std::string getName(void* handle);
00021 
00022 void print(void* handle, ::ros::console::Level level, const char* str, const char* file, const char* function, int line)
00023 {
00024   // still printing to console
00025   ::ros::console::backend::print(0, level, str, file, function, line);
00026 
00027   // pass log message to appender
00028   if(rosconsole_glog_appender)
00029   {
00030     rosconsole_glog_appender->log(level, str, file, function, line);
00031   }
00032 
00033   google::LogSeverity glog_level;
00034   if(level == ::ros::console::levels::Info)
00035   {
00036     glog_level = google::GLOG_INFO;
00037   }
00038   else if(level == ::ros::console::levels::Warn)
00039   {
00040     glog_level = google::GLOG_WARNING;
00041   }
00042   else if(level == ::ros::console::levels::Error)
00043   {
00044     glog_level = google::GLOG_ERROR;
00045   }
00046   else if(level == ::ros::console::levels::Fatal)
00047   {
00048     glog_level = google::GLOG_FATAL;
00049   }
00050   else
00051   {
00052     // ignore debug
00053     return;
00054   }
00055   std::string name = getName(handle);
00056   google::LogMessage(file, line, glog_level).stream() << name << ": " << str;
00057 }
00058 
00059 bool isEnabledFor(void* handle, ::ros::console::Level level)
00060 {
00061   size_t index = (size_t)handle;
00062   if(index < rosconsole_glog_log_levels.size())
00063   {
00064     return level >= rosconsole_glog_log_levels[index].second;
00065   }
00066   return false;
00067 }
00068 
00069 void* getHandle(const std::string& name)
00070 {
00071   size_t count = rosconsole_glog_log_levels.size();
00072   for(size_t index = 0; index < count; index++)
00073   {
00074     if(name == rosconsole_glog_log_levels[index].first)
00075     {
00076       return (void*)index;
00077     }
00078     index++;
00079   }
00080   // add unknown names on demand with default level
00081   rosconsole_glog_log_levels.push_back(std::pair<std::string, levels::Level>(name, ::ros::console::levels::Info));
00082   return (void*)(rosconsole_glog_log_levels.size() - 1);
00083 }
00084 
00085 std::string getName(void* handle)
00086 {
00087   size_t index = (size_t)handle;
00088   if(index < rosconsole_glog_log_levels.size())
00089   {
00090     return rosconsole_glog_log_levels[index].first;
00091   }
00092   return "";
00093 }
00094 
00095 void register_appender(LogAppender* appender)
00096 {
00097   rosconsole_glog_appender = appender;
00098 }
00099 
00100 void shutdown()
00101 {}
00102 
00103 bool get_loggers(std::map<std::string, levels::Level>& loggers)
00104 {
00105   for(std::vector<std::pair<std::string, levels::Level> >::const_iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
00106   {
00107     loggers[it->first] = it->second;
00108   }
00109   return true;
00110 }
00111 
00112 bool set_logger_level(const std::string& name, levels::Level level)
00113 {
00114   for(std::vector<std::pair<std::string, levels::Level> >::iterator it = rosconsole_glog_log_levels.begin(); it != rosconsole_glog_log_levels.end(); it++)
00115   {
00116     if(name == it->first)
00117     {
00118       it->second = level;
00119       return true;
00120     }
00121   }
00122   return false;
00123 }
00124 
00125 } // namespace impl
00126 } // namespace console
00127 } // namespace ros


rosconsole
Author(s): Josh Faust
autogenerated on Fri Aug 28 2015 12:33:01