ROS1中g3log的安装&使用
基于ros1-melodic版本进行封装使用
g3log库安装
git clone https:
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:
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 {
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_Helper(bool debug, std::string program);
~G3log_Helper();
private:
bool debug_;
std::unique_ptr<g3::LogWorker> worker;
};
g3log_helper.cpp
#include "g3log_helper.h"
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();
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;
std::future<void> received =
logHandle->call(&LogRotate::setMaxLogSize, k10MBInBytes);
g3::initializeLogging(worker.get());
}
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";
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";
LOGF_IF(INFO, (less < more), "if %d<%d then this text will be logged", less, more);
CHECK(less != more);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)