自制简易C++日志库——优雅地调试VINS-mono

    天下之患,莫大于不知其然而然。——(宋)苏轼《策略》

    最近一直在研究VINS-mono,由于它是基于ROS(Robot Operating System)开发的,运行的时候需要启动rviz和播放包文件,所以调试起来很不方便。有句话说得好——会的事情认真做,不会的事情问着做。通过询问各路大神,得到了多种方法,具体如下:

  1. printf大法或者cout大法。亲测无效,在终端启动的话,让他打印到哪里呢?
  2. ROS_DEBUG,就像程序当中已经有的那样,照葫芦画瓢。理想很丰满,但是实际操作起来并不简单,而且由于对ROS不熟悉,稍有不慎,整个系统都崩掉了。
  3. 开源日志库,如log4cpp、log4cplus。看了一下,功能过于强大,不够简易,反而会拖慢编译时间。
  4. gtest。我太菜了,百度了一下,一头雾水,不知道是什么东西。
  5. Qt Creator。理论上应该是可行的,有大神自称就是用Qt调试ROS程序的,但是我下载下来鼓捣了一天,没弄成。

    但是,正所谓富人有富人的活法,穷人有穷人的活法;聪明人有聪明人的办法,笨人自然也有笨人的方法。既然printf和cout不行,那干脆建一个txt文档,写到文档里好了。话不多说,上代码!

    首先是创建路径文件夹,这里根据当前的年月日日期来命名。

bool createFolder(string &folderPath) {
    time_t timer;
    time(&timer);
    tm *t_tm = localtime(&timer);
    folderPath += to_string(t_tm->tm_year + 1900) + "_" + to_string(t_tm->tm_mon + 1) + "_" + to_string(t_tm->tm_mday);
    if (access(folderPath.c_str(), 0) == -1) {    //如果文件夹不存在需要创建
        mkdir(folderPath.c_str(), S_IRWXU);
        return true;
    } else {    //如果文件夹存在直接退出
        return true;
    }
}

    对txt文件追加内容。

bool writeFile(string filename, string tag, string str) {
    ofstream ofileAgain;
    ofileAgain.open(filename, ios::app);
    ofileAgain << tag + "\t\t" + str << endl;
    ofileAgain.close();
    return 0;
}

    供调用的函数。

void MLGJing(string name, string tag, string str) {
    string folderPath = "/home/dong/LELB/";
    createFolder(folderPath);
    string filename = folderPath + "/" + name + ".txt";
    writeFile(filename, tag, str);
}

    cpp文件所有内容如下。

#include 
#include 
#include 

using namespace std;

bool createFolder(string &folderPath) {
    time_t timer;
    time(&timer);
    tm *t_tm = localtime(&timer);
    folderPath += to_string(t_tm->tm_year + 1900) + "_" + to_string(t_tm->tm_mon + 1) + "_" + to_string(t_tm->tm_mday);
    if (access(folderPath.c_str(), 0) == -1) {
        mkdir(folderPath.c_str(), S_IRWXU);
        return true;
    } else {
        return true;
    }
}

bool writeFile(string filename, string tag, string str) {
    ofstream ofileAgain;
    ofileAgain.open(filename, ios::app);
    ofileAgain << tag + "\t\t" + str << endl;
    ofileAgain.close();
    return 0;
}

void MLGJing(string name, string tag, string str) {
    string folderPath = "/home/dong/LELB/";
    createFolder(folderPath);
    string filename = folderPath + "/" + name + ".txt";
    writeFile(filename, tag, str);
}

    编写.h头文件。

#ifndef LOGLIB_LIBRARY_H
#define LOGLIB_LIBRARY_H

#include 

using namespace std;
void MLGJing(string name, string tag, string str);

#endif //LOGLIB_LIBRARY_H

    CMakeLists.txt文件。

cmake_minimum_required(VERSION 3.15)
project(LogLib)

set(CMAKE_CXX_STANDARD 14)

add_library(LogLib SHARED library.cpp library.h)

    完成代码编写之后,需要对工程进行编译,生成共享库。

    之后对于需要使用日志库进行调试的工程,需要在CMakeLists.txt文件中,添加头文件路径以及链接库。

include_directories(/home/dong/Code/LogLib)
target_link_libraries(demo1 /home/dong/Code/LogLib/cmake-build-debug/libLogLib.so)

    在代码cpp文件中,添加头文件。

#include 

    之后,就可以调用void MLGJing(string name, string tag, string str)函数将相关信息写入到txt文档中了。实现了对VINS-mono的调试。

    如果大家有更好的方法或者会文章开头提到的一些方法,欢迎大家评论或者私信我。

你可能感兴趣的:(VINS)