ROS1中g3log的安装&使用
基于ros1-melodic版本进行封装使用
g3log库安装
git clone https://github.com/KjellKod/g3log.git
cd g3log
git checkout c++11-master
mkdir build
cd build
cmake ..
make
sudo make install
g3sinks库安装
ros环境下的boost库安装目录与g3sinks要求的不一致,所以先修改下 ./g3sinks/sink_lograte/CMakeLists.txt
,target_link_libraries
修改为如下
target_link_libraries(
g3logrotate
PUBLIC ${G3LOG_LIBRARY}
/usr/lib/x86_64-linux-gnu/libboost_filesystem.so
/usr/lib/x86_64-linux-gnu/libboost_system.so
# ${Boost_LIBRARIES}
#PRIVATE Boost::filesystem
#PRIVATE Boost::system
PRIVATE ${ZLIB_LIBRARY}
)
git clone https://github.com/KjellKod/g3sinks.git
cd g3sinks
git checkout c++11-master
mkdir build
cd build
cmake ..
make
sudo make install
g3log封装
g3log_helper.h
#pragma once
#include <iostream>
#include <stdlib.h>
#include <memory>
#include <g3sinks/LogRotate.h>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
struct CustomSink {
// Linux xterm color
// http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
enum FG_Color { YELLOW = 33, RED = 31, GREEN = 32, WHITE = 97 };
FG_Color GetColor(const LEVELS level) const {
if (level.value == WARNING.value) {
return YELLOW;
}
if (level.value == DEBUG.value) {
return GREEN;
}
if (g3::internal::wasFatal(level)) {
return RED;
}
return WHITE;
}
void ReceiveLogMessage(g3::LogMessageMover logEntry) {
auto level = logEntry.get()._level;
auto color = GetColor(level);
std::cout << "\033[" << color << "m" << logEntry.get().toString()
<< "\033[m" << std::endl;
}
};
class G3log_Helper {
public:
// G3Log配置:
G3log_Helper(bool debug, std::string program);
// G3Log内存清理:
~G3log_Helper();
private:
bool debug_;
std::unique_ptr<g3::LogWorker> worker;
};
g3log_helper.cpp
#include "g3log_helper.h"
// G3Log配置:
G3log_Helper::G3log_Helper(bool debug, std::string program) : debug_(debug) {
// 配置日志目录
std::string home = getenv("HOME");
std::string dir_log = home + "/g3log";
std::string mkdir_log = "mkdir -p " + dir_log;
system(mkdir_log.c_str());
worker = g3::LogWorker::createLogWorker();
// 取消默认日志目录配置
// auto handle = worker->addDefaultLogger(program, LOGDIR);
// 日志是否输出到终端
if (debug_) {
auto sinkHandle = worker->addSink(std2::make_unique<CustomSink>(),
&CustomSink::ReceiveLogMessage);
}
// 自定义日志目录
auto logHandle = worker->addSink(
std::make_unique<LogRotate>(program, dir_log), &LogRotate::save);
// 自定义日志大小
const int k10MBInBytes = 10 * 1024 * 1024; // 10M
std::future<void> received =
logHandle->call(&LogRotate::setMaxLogSize, k10MBInBytes);
g3::initializeLogging(worker.get());
}
// G3Log内存清理:
G3log_Helper::~G3log_Helper() {
g3::internal::shutDownLogging();
}
g3log语法
// 类初始化
G3log_Helper gh(true, "test_log");
// 正常流式输出
LOG(DEBUG) << "Hi " << "DEBUG";
LOG(INFO) << "Hi " << "INFO";
LOG(WARNING) << "Hi " << "WARNING";
// printf式输出
LOGF(INFO, "Hi log %d", 123);
LOGF(WARNING, "Printf-style syntax is also %s", "available");
// 条件输出
int less = 1;
int more = 2;
LOG_IF(INFO, (less < more)) << "If [true], then this text will be logged";
// or with printf-like syntax
LOGF_IF(INFO, (less < more), "if %d<%d then this text will be logged", less, more);
//*CHECK(false)*会触发一个“fatal”的信息
CHECK(less != more); // not FATAL
// CHECK(less > more) << "CHECK(false) triggers a FATAL message";