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

Problem adding file sync after spdlog has been setup... #3313

Open
htartisan opened this issue Jan 7, 2025 · 7 comments
Open

Problem adding file sync after spdlog has been setup... #3313

htartisan opened this issue Jan 7, 2025 · 7 comments

Comments

@htartisan
Copy link

I am working on a new project where I am trying to use speedlog. I have used it in the past and it seemed to work fine.

In the new project, I am creating an initial logging configuration where I create a "main" logger,, and I add a console log sink (see below).

 auto m_mainLogger =
        std::make_shared<spdlog::logger>(m_sLogName.c_str());

 auto m_consoleSink =
            std::make_shared<spdlog::sinks::stdout_color_sink_mt>();

 m_mainLogger->sinks().push_back(m_consoleSink);

That seems to work fine, and I can see (colored) log output to the console.

However, after the application configuration is loaded, I then try to add a file sink to the "main" logger (see below).

auto m_fileSink =
                std::make_shared<spdlog::sinks::rotating_file_sink_mt>
                (
                    sLogFilePath,
                    MAX_LOG_FILE_SIZE,
                    MAX_LOG_FILES,
                    ROTATE_LOGS_ON_STARTUP
                );

m_mainLogger->sinks().push_back(m_fileSink);

I can then see that the log file is opened, but I never see any log text in the log file.

Is there an issue adding additional log sinks once the initial logging configuration has been setup?

@htartisan
Copy link
Author

Also NOTE that... I tried adding the following statement to the log configuration, after the log file sink has been added to the "main" logger...

/spdlog::set_default_logger(m_mainLogger);

When I do that, NO log text ends up going to either the console or the log file.

@tt4g
Copy link
Contributor

tt4g commented Jan 8, 2025

Please submit code showing how m_mainLogger is actually used.
Without knowing that, we can't guess what is going on.

And, from auto m_mainLogger, it looks like a local variable, but why call spdlog::set_default_logger(m_mainLogger);?

@htartisan
Copy link
Author

The shared pointers (m_mainLogger, m_consoleSink, m_fileSink) are all defined in a global class, that exists for the life of the program.

Logging statements are issued as follows:
SPDLOG_ERROR("this is an example error message");

As mentioned, this works fine at logging to the console (m_mainLogger / m_consoleSink), but the error statements don't end up in the log file.

@htartisan
Copy link
Author

NOTE: This issue has been happening on a spdlog build in Windows x64.

I have not yet tried it in my Linux x64 build.

@tt4g
Copy link
Contributor

tt4g commented Jan 8, 2025

SPDLOG_ERROR always uses the default logger.
It will not work unless set to the default logger by spdlog::set_default_logger(m_mainLogger).

See also: https://github.com/gabime/spdlog/wiki/0.-FAQ#how-to-remove-all-debug-statements-at-compile-time-

@htartisan
Copy link
Author

htartisan commented Jan 8, 2025 via email

@tt4g
Copy link
Contributor

tt4g commented Jan 8, 2025

If you are running an application on a Windows machine, the OS will discard to buffered data (not written to a file) if the process exits without flushing the log.
Are you calling spdlog::shutdown() or logger->flush() before the application exits?

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