Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spdlog and nlohmann/json are incompatible ? #2764

Closed
mvaccaro-dev opened this issue Jun 16, 2023 · 8 comments
Closed

spdlog and nlohmann/json are incompatible ? #2764

mvaccaro-dev opened this issue Jun 16, 2023 · 8 comments

Comments

@mvaccaro-dev
Copy link

I'm going crazy with this...

main.cpp

#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE

#include <stdio.h>
#include <iostream>
#include <string>
#include <chrono>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/daily_file_sink.h>

#include "config/test.h"

int main(int argc, char * argv[])
{
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(spdlog::level::trace);
    console_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%s:%#] [%^%l%$] %v");
	
    auto file_sink = std::make_shared<spdlog::sinks::daily_file_sink_mt>("daily.log", 0, 1);
    file_sink->set_level(spdlog::level::info);
    file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%s:%#] [%^%l%$] %v");

    std::vector<spdlog::sink_ptr> sinks {console_sink, file_sink};
    auto logger = std::make_shared<spdlog::logger>(LOGGER_NAME, sinks.begin(), sinks.end());
    logger->set_level(spdlog::level::trace);	
    logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%s:%#] [%^%l%$] %v");

    spdlog::register_logger(logger);	

    Test* test = new Test();

    return 0;
}

config/test.h

#ifndef _TEST_H_
#define _TEST_H_

#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <spdlog/spdlog.h>
#include <chrono>
#include <ctime>

#include "json.hpp"

#define LOGGER_NAME                     "logger"

class Test
{
    public:
        Test();

    private:
        std::shared_ptr<spdlog::logger> m_logger;        
};

#endif

config/test.cpp

#include "test.h"

Test::Test()
{
    this->m_logger = spdlog::get(LOGGER_NAME);
    
    std::cout << "TRACE: " << this->m_logger->should_log(spdlog::level::trace) << std::endl;
    std::cout << "DEBUG: " << this->m_logger->should_log(spdlog::level::debug) << std::endl;
    std::cout << "INFO: " << this->m_logger->should_log(spdlog::level::info) << std::endl;
    std::cout << "WARN: " << this->m_logger->should_log(spdlog::level::warn) << std::endl;
    std::cout << "ERROR: " << this->m_logger->should_log(spdlog::level::err) << std::endl;
    std::cout << "CRITICAL: " << this->m_logger->should_log(spdlog::level::critical) << std::endl;

    SPDLOG_LOGGER_TRACE(this->m_logger, "Test");
    SPDLOG_LOGGER_DEBUG(this->m_logger, "Test");
    SPDLOG_LOGGER_INFO(this->m_logger, "Test");
    SPDLOG_LOGGER_WARN(this->m_logger, "Test");
    SPDLOG_LOGGER_ERROR(this->m_logger, "Test");
    SPDLOG_LOGGER_CRITICAL(this->m_logger, "Test");
}

Console output

TRACE: 1
DEBUG: 1
INFO: 1
WARN: 1
ERROR: 1
CRITICAL: 1
[2023-06-16 11:08:37.282] [test.cpp:23] [info] Test
[2023-06-16 11:08:37.283] [test.cpp:24] [warning] Test
[2023-06-16 11:08:37.283] [test.cpp:25] [error] Test
[2023-06-16 11:08:37.283] [test.cpp:26] [critical] Test

Expected console output

TRACE: 1
DEBUG: 1
INFO: 1
WARN: 1
ERROR: 1
CRITICAL: 1
==> [2023-06-16 11:08:37.282] [test.cpp:21] [trace] Test <==
==> [2023-06-16 11:08:37.282] [test.cpp:22] [debug] Test <==
[2023-06-16 11:08:37.282] [test.cpp:23] [info] Test
[2023-06-16 11:08:37.283] [test.cpp:24] [warning] Test
[2023-06-16 11:08:37.283] [test.cpp:25] [error] Test
[2023-06-16 11:08:37.283] [test.cpp:26] [critical] Test

This is a bug or I'm doing something wrong ?

@gabime
Copy link
Owner

gabime commented Jun 16, 2023

How is it related with the with nlohmanjson?

@mvaccaro-dev
Copy link
Author

I had initially noticed that by not including the nlohmann library, the messages were printing correctly. However, now, doing new tests, it would appear that there is no connection with that library. At the moment I don't understand...

@mvaccaro-dev
Copy link
Author

I'm sorry, I made a mistake. The topic title is wrong because there is no link to the nlohmann/json library. In fact, by not including the library, the bug still occurs.

@gabime
Copy link
Owner

gabime commented Jun 16, 2023

Perhaps the test.cpp get compiled before main.cpp and before the SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE is defined.

@mvaccaro-dev
Copy link
Author

mvaccaro-dev commented Jun 16, 2023

Seems that order is correct

pi@raspberrypi:~/test_logger $ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/test_logger
pi@raspberrypi:~/test_logger $ make
[ 33%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/app.dir/test.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
pi@raspberrypi:~/test_logger $ ./app
TRACE: 1
DEBUG: 1
INFO: 1
WARN: 1
ERROR: 1
CRITICAL: 1
[2023-06-16 15:47:33.249] [test.cpp:16] [info] Test
[2023-06-16 15:47:33.249] [test.cpp:17] [warning] Test
[2023-06-16 15:47:33.249] [test.cpp:18] [error] Test
[2023-06-16 15:47:33.249] [test.cpp:19] [critical] Test
pi@raspberrypi:~/test_logger $

Wait..If I move #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE from main.cpp to test.h, the output is correct! Seems that the #define needs to be overrided

pi@raspberrypi:~/test_logger $ make
Scanning dependencies of target app
[ 33%] Building CXX object CMakeFiles/app.dir/main.cpp.o
In file included from /home/pi/test_logger/main.cpp:9:
/home/pi/test_logger/test.h:4: warning: "SPDLOG_ACTIVE_LEVEL" redefined
 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE

In file included from /usr/local/include/spdlog/spdlog.h:12,
                 from /home/pi/test_logger/main.cpp:5:
/usr/local/include/spdlog/common.h:228: note: this is the location of the previous definition
 #    define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO

[ 66%] Building CXX object CMakeFiles/app.dir/test.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
pi@raspberrypi:~/test_logger $ ./app
TRACE: 1
DEBUG: 1
INFO: 1
WARN: 1
ERROR: 1
CRITICAL: 1
[2023-06-16 15:53:26.156] [test.cpp:14] [trace] Test
[2023-06-16 15:53:26.156] [test.cpp:15] [debug] Test
[2023-06-16 15:53:26.156] [test.cpp:16] [info] Test
[2023-06-16 15:53:26.156] [test.cpp:17] [warning] Test
[2023-06-16 15:53:26.156] [test.cpp:18] [error] Test
[2023-06-16 15:53:26.156] [test.cpp:19] [critical] Test

@gabime
Copy link
Owner

gabime commented Jun 16, 2023

Try defining if as a compiler flag, or in each compilation unit

@mvaccaro-dev
Copy link
Author

Thanks for your hint. I've removed #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE from every file and compiled with this minimal cmake

cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD 14)
set(PROJECT_NAME app)
project(${PROJECT_NAME})
set(PROJECT_SOURCE_DIR .)
file(GLOB SOURCE_FILES
    ${PROJECT_SOURCE_DIR}/*.cpp
    ${PROJECT_SOURCE_DIR}/*.h
)
find_package(spdlog REQUIRED)
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} spdlog::spdlog)

Now the output is correct. I think that should be mentioned in the wiki

@gabime
Copy link
Owner

gabime commented Jun 16, 2023

Great. The wiki is open for all to edit. Feel free to add it there.

@gabime gabime closed this as completed Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants