mirror of https://github.com/gabime/spdlog.git
Error handler
parent
9df63cee2a
commit
3153f22b8d
@ -0,0 +1,19 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "spdlog/common.h"
|
||||
|
||||
// by default, prints the error to stderr, thread safe
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
class default_err_handler {
|
||||
mutable std::mutex mutex_;
|
||||
public:
|
||||
void handle(const std::string& origin, const source_loc& loc, const std::string &err_msg) const;
|
||||
};
|
||||
|
||||
|
||||
}} // namespace spdlog::details
|
@ -1,33 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include "spdlog/common.h"
|
||||
|
||||
// by default, prints the error to stderr. thread safe.
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
class error_handler {
|
||||
public:
|
||||
explicit error_handler(std::string name);
|
||||
error_handler(const error_handler &);
|
||||
error_handler(error_handler &&) noexcept;
|
||||
// for simplicity allow only construction of this class.
|
||||
// otherwise we need to deal with mutexes and potential deadlocks.
|
||||
error_handler &operator=(const error_handler &) = delete;
|
||||
error_handler &operator=(error_handler &&) = delete;
|
||||
void handle(const source_loc& loc, const std::string &err_msg) const;
|
||||
void set_name(const std::string& name);
|
||||
void set_custom_handler(err_handler handler);
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
std::string name_;
|
||||
err_handler custom_handler_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
}} // namespace spdlog::details
|
@ -0,0 +1,27 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "iostream"
|
||||
#include "spdlog/details/default_err_handler.h"
|
||||
#include "spdlog/details/os.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
void default_err_handler::handle(const std::string &origin, const source_loc &loc, const std::string &err_msg) const {
|
||||
std::lock_guard lk{mutex_};
|
||||
const auto tm_time = os::localtime();
|
||||
char date_buf[128];
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
std::string msg;
|
||||
if (loc.empty()) {
|
||||
msg = fmt_lib::format("[*** LOGGING ERROR ***] [{}] [{}] {}\n", date_buf, origin, err_msg);
|
||||
}
|
||||
else {
|
||||
msg = fmt_lib::format("[*** LOGGING ERROR ***] [{}({})] [{}] [{}] {}\n", loc.filename, loc.line, date_buf, origin, err_msg);
|
||||
}
|
||||
std::fputs(msg.c_str(), stderr);
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
@ -1,55 +0,0 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#include "spdlog/details/error_handler.h"
|
||||
#include "spdlog/details/os.h"
|
||||
#include "iostream"
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
|
||||
error_handler::error_handler(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
error_handler::error_handler(const error_handler &other) {
|
||||
std::lock_guard lk{other.mutex_};
|
||||
name_ = other.name_;
|
||||
custom_handler_ = other.custom_handler_;
|
||||
}
|
||||
|
||||
error_handler::error_handler(error_handler &&other) noexcept {
|
||||
std::lock_guard lk{other.mutex_};
|
||||
name_ = std::move(other.name_);
|
||||
custom_handler_ = std::move(other.custom_handler_);
|
||||
}
|
||||
|
||||
void error_handler::handle(const source_loc &loc, const std::string &err_msg) const {
|
||||
std::lock_guard lk{mutex_};
|
||||
if (custom_handler_) {
|
||||
custom_handler_(err_msg);
|
||||
return;
|
||||
}
|
||||
const auto tm_time = os::localtime();
|
||||
char date_buf[128];
|
||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||
std::string msg;
|
||||
if (loc.empty()) {
|
||||
msg = fmt_lib::format("[*** LOGGING ERROR ***] [{}] [{}] {}\n", date_buf, name_, err_msg);
|
||||
}
|
||||
else {
|
||||
msg = fmt_lib::format("[*** LOGGING ERROR ***] [{}({})] [{}] [{}] {}\n", loc.filename, loc.line, date_buf, name_, err_msg);
|
||||
}
|
||||
std::fputs(msg.c_str(), stderr);
|
||||
}
|
||||
void error_handler::set_name(const std::string &name) {
|
||||
std::lock_guard lk{mutex_};
|
||||
name_ = name;
|
||||
}
|
||||
|
||||
void error_handler::set_custom_handler(err_handler handler) {
|
||||
std::lock_guard lk{mutex_};
|
||||
custom_handler_ = std::move(handler);
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
} // namespace spdlog
|
Loading…
Reference in New Issue