C++项目日志管理(所有日志输入到一个文件中)----------Glog和 Log4cplus

- 安装

glog:
sudo apt-get install libgoogle-glog-dev
log4cplus:
sudo apt-get install liblog4cplus-dev
  • cmakelist
方式一:
find_package(glog REQUIRED)
# set(GLOG_INCLUDE_DIRS "/usr/include/glog")
# set(GLOG_LIBRARIES "/usr/lib/x86_64-linux-gnu/libglog.a")

方式二:
# find_package(log4cplus REQUIRED)
set(LOG4CPLUS_INCLUDE_DIRS "/usr/include/log4cplus")
set(LOG4CPLUS_LIBRARIES "/usr/lib/x86_64-linux-gnu/liblog4cplus.a")

include_directories(
 include
 ${catkin_INCLUDE_DIRS}
 ${GLOG_INCLUDE_DIRS}
 ${LOG4CPLUS_INCLUDE_DIRS}
)

target_link_libraries(pure_pursuit
   	${catkin_LIBRARIES} 
   	${LOG4CPLUS_LIBRARIES} 
   	glog::glog
   	 )

- 头文件

glog:

#include <glog/logging.h>
#include <glog/raw_logging.h>
#include <glog/log_severity.h>
#include <glog/stl_logging.h>
#include <glog/vlog_is_on.h>
		log4cplus:
#include <log4cplus/log4judpappender.h>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/fileappender.h>
#include <iomanip>

- 源文件

glog:
在main.cpp文件中添加如下:

int main(int argc, char **argv)
{
      google::InitGoogleLogging(argv[0]);

      FLAGS_logtostderr = false;
      FLAGS_logbufsecs = 0; // 日志实时输出

      google::SetLogDestination(google::GLOG_INFO, "/home/luo/works/ws/test_ws/src/autoware_robot/log/autoware/pure_log_info_"); // 设置 google::INFO 级别的日志存储路径和文件名前缀
      LOG(INFO) << "HELLO " << "ok!";

      // FLAGS_log_dir = "works/detect_aruco_ws/src/visual_2.0_recharging/log/";//设置日志文件输出目录
      FLAGS_minloglevel = google::INFO;
      FLAGS_logtostderr = false;  //是否打印到控制台
      FLAGS_alsologtostderr = true;  //打印到日志同时是否打印到控制台

      ros::init(argc, argv, "pure_pursuit");
      waypoint_follower::PurePursuitNode ppn;
      ppn.run();


      google::ShutdownGoogleLogging();
      return 0;
}

在ClassA.cpp文件中添加如下:

#include <glog/logging.h>
LOG(INFO) << "------computeLookaheadDistance() = " << computeLookaheadDistance() << std::endl;

至此Glog会将main.cpp,ClassA.cpp中的所有log信息打印在指定的文件中,
如果想将ClassB.cpp,ClassD.cpp,ClassV.cpp中的log信息也打印在指定文件中,同理操作即可。

log4cplus:

在main.cpp中添加如下:


#include <ros/ros.h>
#include <iostream>
#include "op_behavior_selector_core.h"

#include <log4cplus/log4judpappender.h>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>

// #include "/home/luo/works/ws/test_ws/src/autoware_robot/autoware/common/log_manager/logger.h"

using namespace std;

log4cplus::Logger local_mylogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("MainApp")); 
      
int main(int argc, char **argv)
{

      log4cplus::initialize();
      // 日志配置文件log4cplus.config
      log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(
            "/works/ws/test_ws/src/autoware_robot/log/autoware/local_log4cplus.config"));  

      // MainApp对应配置文件log4cplus.config中的logger
      // static log4cplus::Logger local_mylogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("MainApp")); 

	ros::init(argc, argv, "op_behavior_selector");
	BehaviorGeneratorNS::BehaviorGen beh_gen;
	beh_gen.MainLoop();

      log4cplus::Logger::shutdown();

	return 0;
}

local_log4cplus.config 如下:

#MainApp
log4cplus.logger.MainApp=INFO,MyFileAppender

log4cplus.rootLogger = TRACE,SA
log4cplus.logger.logConsole = TRACE,LC
 
#MyFileAppender
log4cplus.appender.MyFileAppender=log4cplus::RollingFileAppender
log4cplus.appender.MyFileAppender.File/works/ws/test_ws/src/autoware_robot/log/autoware/local_app.log
log4cplus.appender.MyFileAppender.MaxFileSize=10MB
log4cplus.appender.MyFileAppender.MaxBackupIndex=5
log4cplus.appender.MyFileAppender.layout=log4cplus::PatternLayout
log4cplus.appender.MyFileAppender.layout.ConversionPattern=[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l] %m%n

[%-5p][%D{%Y/%m/%d %H:%M:%S:%q}][%-l] %m%n 输出信息格式(时间,年月,哪个cpp多少行等待),自己定义

其他ClassA.hpp文件中添加:

#include <log4cplus/log4judpappender.h>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/fileappender.h>
#include <iomanip>


namespace PlannerHNS
{
class DecisionMaker
{
public:
      log4cplus::Logger decisionmaker_mylogger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("MainApp")); 
}

在ClassA.cpp添加:

           LOG4CPLUS_INFO_FMT(decisionmaker_mylogger, LOG4CPLUS_TEXT("m_CarInfo.wheel_base = %lf\n"), m_CarInfo.wheel_base);
至此,main.cpp,ClassA.cpp中log信息都将答应到指定文件中,
如果还有其他ClassB.cpp,ClassC.cpp同理添加。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值