c++ - 使用spdlog(C++)进行记录,记录器未将日志写入文件

标签 c++ googletest spdlog

我正在尝试使用google test执行测试时在文本文件中记录基本信息。 我的最终目标是记录异常的跟踪。

在项目中,我使用的是C++ 11,它是通过CMake和spdlog仅作为 header 添加到项目中的最新Google测试版本(已添加到libs中的项目中)。

由于某种原因,即使试图强制刷新,记录器也不会写入文件。我尝试了互联网上的其他“快速入门”,但没有任何帮助。我不认为这可能是一个问题,但是一个假设是,您不能在测试的上下文中写入文件。该项目的结构如下:

.
|
├── build
├── cmake
├── libs
|   ├── ...
|   └── spdlog (*)
├── src
|   ├── ...
|   ├── main.cpp
|   └── CMakeLists.txt
├── test
|   ├── ...
|   ├── CMakeLists.txt
|   └── core
|       └── logging
|           ├── log
|           |   └── logger.txt
|           └── logging_test.cpp
├── ...
└── CMakeLists.txt 

目录中的(*)文件是spdlog / include / spdlog https://github.com/gabime/spdlog/tree/v1.x/include/spdlog中的文件

这是测试类 logging_test.cpp中的代码。运行测试check_exception_thrown_equal后,尚未将任何内容写入logger.txt。可能是什么问题?
#include <exception>
#include "gtest/gtest.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>


class LoggingTest: public testing::Test {

 protected:
  std::shared_ptr<spdlog::logger> logger;

  struct ExceptionTemplate : std::exception {
    const char* what() { return "ExceptionTemplate triggered!"; }
  };

 // create logger 
 void create_logger() {
   // Create basic file logger (not rotated)
   logger = spdlog::basic_logger_mt("logger", "log/logger.txt");
 }

  // setup logger configurations 
  void set_up() {
    logger->set_level(spdlog::level::info);
    logger->flush_on(spdlog::level::info);
    logger->info("Debug logger setup done. \n");
  }

  // critical method that generates and returns my exception of type ExceptionTemplate
  ExceptionTemplate exception_generator() {
    try {

      ///////////////////////////////
      // call to critical method here
      ///////////////////////////////

      throw ExceptionTemplate();
    }
    catch (ExceptionTemplate &e) {
      log_exception(e);
      return e;
    }
  }

  // write to logger 
  void log_exception(ExceptionTemplate e) {
    try {

      LoggingTest::create_logger();
      LoggingTest::set_up();

      logger->info("Exception raised! {}", e.what());
    }
    catch (const spdlog::spdlog_ex &ex) {
      std::cout << "Log initialization failed: " << ex.what() << std::endl;
    }
  }
};


TEST_F(LoggingTest, check_exception_thrown_equal) {
  ASSERT_STREQ(LoggingTest::exception_generator().what(), "ExceptionTemplate triggered!");
}

最佳答案

尝试使用一个更简单的设置,您不需要所有的“helper”功能等等,也不需要一开始就与异常相关的东西。只需在调用类的构造函数时记录一条消息即可。

class LoggingTest {
  LoggingTest() {
    auto logger = spdlog::basic_logger_mt("test_logger", "logs/test.txt");
    spdlog::set_default_logger(logger);
    spdlog::flush_on(spdlog::level::info);

    spdlog::get("test_logger")->info("LoggingTest::ctor");
  }
}

然后只需在main(或其他任何地方)中创建该类的实例。确保目录存在并且可写(尽管此使用情况会导致错误,而不是无提示的失败)。

关于c++ - 使用spdlog(C++)进行记录,记录器未将日志写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59017143/

相关文章:

c++ - 如何从另一个 View 设置过滤器?

c++ - 声明所有虚拟方法以使用 gtest 进行模拟?

googletest - 谷歌测试 : assert on equality to one of two values

c++ - 如何写一个不同大小的6维数组?

c++ - 返回指向静态对象的指针

c++ - 如何在 gmock 中测试失败

c++ - 在自定义反向 vector 迭代器中看不到第一个元素

c++ - 如何在 C++ 中的嵌套词法作用域可访问的作用域中声明静态信息?

c++ - spdlog 意外包含引用点

c++ - 如何在C++中检查用户输入