-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Comments
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. |
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. |
@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: |
thank you |
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:
However, the programme continues to run after this statement. Can anyone help me get this working?
Thanks,
Andy
The text was updated successfully, but these errors were encountered: