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

New user: forcing programme exit when spdlog::error is called #1363

Closed
a-jp opened this issue Dec 18, 2019 · 4 comments
Closed

New user: forcing programme exit when spdlog::error is called #1363

a-jp opened this issue Dec 18, 2019 · 4 comments

Comments

@a-jp
Copy link

a-jp commented Dec 18, 2019

Hi,
I'm a new user. I would like to ensure I can trigger a programme stop when/if I send anything to the error stream as this is the way I would like my programme to function. I have quite a simple set up:

    spdlog::set_level(spdlog::level::debug); // Set global log level to debug

    spdlog::set_error_handler([](const std::string &msg) {
        throw std::runtime_error(msg);
    });

   spdlog::error("Something failed");

However, the programme continues to run after this statement. Can anyone help me get this working?
Thanks,
Andy

@gabime
Copy link
Owner

gabime commented Dec 18, 2019

set_error_ handler is not for this purpose. it is called when there is a problem in the logging itself(e.g. fail write to log file).

Please see #1328 on how to achieve this.

@a-jp
Copy link
Author

a-jp commented Dec 18, 2019

Thanks, is there any chance you could provide a small example including how to add a sink? I'm quite new to the library and not really sure how to do that or when is the correct point to inject the new sink? I have looked at the wiki and the sinks folder. Thanks again.

@tt4g
Copy link
Contributor

tt4g commented Dec 18, 2019

@a-jp For your example (NOTICE It has not been confirmed that it works):

#include "spdlog/base_sink.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/details/null_mutex.h"

#include <memory>
#include <mutex>
#include <string>

template<typename Mutex>
class error_proxy_sink : public spdlog::sinks::base_sink<Mutex>
{
    using BaseSink = spdlog::sinks::base_sink<Mutex>;

    std::shared_ptr<spdlog::sinks::sink> sink_;

public:

    explicit error_proxy_sink(
        std::shared_ptr<spdlog::sinks::sink> sink)
        : sink_(sink)
    {

    }

    error_proxy_sink(const error_proxy_sink&) = delete;
    error_proxy_sink& operator=(const error_proxy_sink&) = delete;

protected:
    void sink_it_(const spdlog::details::log_msg &msg) override
    {
        if (spdlog::level::err == msg.level)
        {
             // Call your error callback.
        }

        if (sink_->should_log(msg.level))
        {
            sink_->log(msg);
        }
    }

    void flush_() override
    {
        sink_->flush();
    }

    void set_pattern_(const std::string &pattern) override
    {
        set_formatter_(spdlog::details::make_unique<spdlog::pattern_formatter>(pattern));
    }

    void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
    {
        BaseSink::formatter_ = std::move(sink_formatter);

        sink_->set_formatter(BaseSink::formatter_->clone());
    }
};

using error_proxy_sink_mt = error_proxy_sink<std::mutex>;
using error_proxy_sink_st = error_proxy_sink<spdlog::details::null_mutex>;

Usage:

#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_sinks.h"

#include <memory>

int main(int, char*)
{
    auto console_sink = std::make_shared<stdout_sink_mt>();
    auto do_on_error_sink = std::make_shared<error_proxy_sink_mt>(console_sink);

    auto logger = std::make_shared<spdlog::logger>("console_with_do_on_error", do_on_error_sink);
    spdlog::register_logger(logger);

    logger->info("foo");
    logger->error("bar");
    logger->error("baz");

    spdlog::shutdown();

    return 0;
}

See also:

@a-jp
Copy link
Author

a-jp commented Dec 18, 2019

thank you

@gabime gabime closed this as completed Dec 18, 2019
bachittle pushed a commit to bachittle/spdlog that referenced this issue Dec 22, 2022
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

3 participants