error: ‘logCallback’ was not declared in this scope
时间: 2023-07-09 09:40:58 浏览: 107
抱歉,我的代码中缺少了logCallback函数的声明,导致编译时出现了该错误。请将以下代码添加到main函数前面,即可解决该问题:
```c++
void logCallback(const rosgraph_msgs::Log::ConstPtr& log);
```
完整的C++代码示例如下:
```c++
#include <ros/ros.h>
#include <ros/console.h>
#include <rosgraph_msgs/Log.h>
void logCallback(const rosgraph_msgs::Log::ConstPtr& log);
int main(int argc, char **argv)
{
ros::init(argc, argv, "log_listener");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe("/rosout", 1000, logCallback);
ros::spin();
return 0;
}
void logCallback(const rosgraph_msgs::Log::ConstPtr& log)
{
// 解析日志信息
std::string log_msg = log->msg;
std::string log_level = log->level;
std::string log_file = log->file;
std::string log_line = std::to_string(log->line);
std::string log_time = std::to_string(log->header.stamp.toSec());
// 根据日志等级设置终端颜色
if (log_level == "INFO") {
ROS_INFO_STREAM("\033[32m" << "[" << log_level << "]" << "[" << log_file << "-" << log_line << "]" << "[" << log_time << "]" << ": " << log_msg << "\033[0m");
} else if (log_level == "WARN") {
ROS_WARN_STREAM("\033[33m" << "[" << log_level << "]" << "[" << log_file << "-" << log_line << "]" << "[" << log_time << "]" << ": " << log_msg << "\033[0m");
} else if (log_level == "ERROR") {
ROS_ERROR_STREAM("\033[31m" << "[" << log_level << "]" << "[" << log_file << "-" << log_line << "]" << "[" << log_time << "]" << ": " << log_msg << "\033[0m");
} else if (log_level == "FATAL") {
ROS_FATAL_STREAM("\033[35m" << "[" << log_level << "]" << "[" << log_file << "-" << log_line << "]" << "[" << log_time << "]" << ": " << log_msg << "\033[0m");
}
}
```
请注意,我还将log->line和log->header.stamp.toSec()转换为了字符串类型,以便与其他字符串拼接。
阅读全文