file sink seems to fail when being called from another module

Viewed 24

Using spdlog-1.10.0 (using meson wrap file)

I have a logger.cpp module:

#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <iostream>
#include <memory>
#include <vector>

void create_logger() {
  std::string logger_name = "logger";
  std::vector<spdlog::sink_ptr> sinks;

  auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
  stdout_sink->set_level(spdlog::level::warn);
  sinks.push_back(stdout_sink);

  auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(".log");
  file_sink->set_level(spdlog::level::level_enum::trace);
  sinks.push_back(file_sink);

  auto logger = std::make_shared<spdlog::logger>(logger_name, begin(sinks), end(sinks));
  if (!logger) {
    std::cerr << "failed to create logger" << std::endl;
  }

  logger->set_level(spdlog::level::level_enum::trace);
  spdlog::register_logger(logger);
}

std::shared_ptr<spdlog::logger> get_logger(){
  auto logger = spdlog::get("logger");
  if(!logger){
      std::cerr << "can't get logger" << std::endl;
  }
  return logger;
}


int main(){
  create_logger();
  get_logger()->debug("Dloading config");
  get_logger()->error("Eloading config");
  get_logger()->info("Iloading config");
  get_logger()->trace("Tloading config");
  get_logger()->warn("Wloading config");
}

When compiling and running this module, I get:

[2022-07-07 14:16:05.738] [logger] [error] Eloading config                                                                                                                                                                                    
[2022-07-07 14:16:05.738] [logger] [warning] Wloading config                                                                                                                                                                                  
$ cat .log                                                                                                                                                                                                                                      
[2022-07-07 14:16:05.738] [logger] [debug] Dloading config                                                                                                                                                                                    
[2022-07-07 14:16:05.738] [logger] [error] Eloading config                                                                                                                                                                                    
[2022-07-07 14:16:05.738] [logger] [info] Iloading config                                                                                                                                                                                     
[2022-07-07 14:16:05.738] [logger] [trace] Tloading config                                                                                                                                                                                    
[2022-07-07 14:16:05.738] [logger] [warning] Wloading config  

However, when running it from another module (using get_logger()->info / trace) I just get the console sink, and nothing is being logged to the log file...

1 Answers

it was the flushing...

Adding logger->flush_on(spdlog::level::level_enum::trace); fixed it, but I don't get why it behaves differently when running from one module vs. running from 2 modules, so I'm still waiting for a better answer...

Related