spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Can't seem to get pointer from spdlog::get

Open zjkhahah opened this issue 1 year ago • 7 comments

#include "spdlog/spdlog.h" #include "spdlog/sinks/daily_file_sink.h" #include <spdlog/sinks/stdout_color_sinks.h> #include int main(int, char* []) {

      auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("../logfile", 23, 59);
    // create synchronous  loggers
    auto nt_logger = std::make_shared<spdlog::logger>("net", daily_sink);
    auto hw_logger  = std::make_shared<spdlog::logger>("hw",  daily_sink);
    auto db_logger  = std::make_shared<spdlog::logger>("db",  daily_sink);      
   // nt_logger->set_pattern("[source %s] [function %!] [line %#] %v");
    //nt_logger->set_level(spdlog::level::debug); // independent levels
    hw_logger->set_level(spdlog::level::debug);
    hw_logger->debug("This is a warning message111");
    spdlog::register_logger(hw_logger);
   spdlog::register_logger(db_logger);
   spdlog::register_logger(nt_logger);
   auto logger=spdlog::get("hw_logger");
   std::cout << logger<< "\n"<< nt_logger << std::endl;

}

output: 0 0x1ed12b0

zjkhahah avatar Jan 19 '24 01:01 zjkhahah

Looks like 0x1ed12b0 is printed on it?

tt4g avatar Jan 19 '24 01:01 tt4g

Looks like 0x1ed12b0 is printed on it?

But the logger output is 0
0x1ed12b0 output auto nt_logger = std::make_sharedspdlog::logger("net", daily_sink);

zjkhahah avatar Jan 19 '24 01:01 zjkhahah

There is an error when compiling the official library but libspdlog.a can be generated normally.

error: specialization of ‘template<class T, class Char, class Enable> struct fmt::v9::formatter’ in different namespace [-fpermissive] struct fmt::formatter<my_type> : fmt::formatterstd::string { ^ In file included from /home/zjk/APP/LinuxLCD/module/spdlog/include/spdlog/fmt/fmt.h:28:0, from /home/zjk/APP/LinuxLCD/module/spdlog/include/spdlog/common.h:50, from /home/zjk/APP/LinuxLCD/module/spdlog/include/spdlog/spdlog.h:12, from /home/zjk/APP/LinuxLCD/module/spdlog/example/example.cpp:30: /home/zjk/APP/LinuxLCD/module/spdlog/include/spdlog/fmt/bundled/core.h:791:8: error: from definition of ‘template<class T, class Char, class Enable> struct fmt::v9::formatter’ [-fpermissive] struct formatter { ^ example/CMakeFiles/example.dir/build.make:62: recipe for target 'example/CMakeFiles/example.dir/example.cpp.o' failed make[2]: *** [example/CMakeFiles/example.dir/example.cpp.o] Error 1 CMakeFiles/Makefile2:122: recipe for target 'example/CMakeFiles/example.dir/all' failed make[1]: *** [example/CMakeFiles/example.dir/all] Error 2 Makefile:151: recipe for target 'all' failed make: *** [all] Error 2

zjkhahah avatar Jan 19 '24 02:01 zjkhahah

The subject , source code, and output do not match so I don't know what you are doing. Only spdlog::get() calls spdlog::get("hw_logger"), and this output does not seem to be output: 0.

Please provide minimal source code that can reproduce the problem and accurate STDOUT.

tt4g avatar Jan 19 '24 03:01 tt4g

Sorry, I may not have explained clearly what I meant. The wiki explains that the logger was not found and the return value is null. I have already registered it before. Why is the return value still null?

zjkhahah avatar Jan 19 '24 05:01 zjkhahah

      auto daily_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("../logfile", 23, 59);
    // create synchronous  loggers
    auto nt_logger = std::make_shared<spdlog::logger>("net", daily_sink);
    auto hw_logger  = std::make_shared<spdlog::logger>("hw",  daily_sink);
    auto db_logger  = std::make_shared<spdlog::logger>("db",  daily_sink);      
   // nt_logger->set_pattern("[source %s] [function %!] [line %#] %v");
    //nt_logger->set_level(spdlog::level::debug); // independent levels
    hw_logger->set_level(spdlog::level::debug);
    hw_logger->debug("This is a warning message111");
    spdlog::register_logger(hw_logger);
   spdlog::register_logger(db_logger);
   spdlog::register_logger(nt_logger);
   auto logger=spdlog::get("hw_logger");
    logger->debug("test");

output Segmentation fault

zjkhahah avatar Jan 19 '24 05:01 zjkhahah

I noticed I missed [A](std::make_shared<spdlog::logger>("hw", daily_sink);).

The argument to spdlog::get() is the logger name, not the variable name of the logger. In other words, you must pass "hw", not "hw_logger".

-   auto logger=spdlog::get("hw_logger");
+   auto logger=spdlog::get("hw");
    logger->debug("test");

tt4g avatar Jan 19 '24 05:01 tt4g