mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
Make assert write to both log file and cerr (#1009)
This commit is contained in:
@@ -20,13 +20,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "util/SourceLocation.h"
|
||||
#include "util/log/Logger.h"
|
||||
|
||||
#include <boost/stacktrace.hpp>
|
||||
#include <boost/stacktrace/stacktrace.hpp>
|
||||
#include <fmt/core.h>
|
||||
#include <fmt/format.h>
|
||||
#include <fmt/ostream.h>
|
||||
|
||||
template <>
|
||||
struct fmt::formatter<boost::stacktrace::stacktrace> : ostream_formatter {};
|
||||
#include <cstdlib>
|
||||
|
||||
namespace util {
|
||||
|
||||
@@ -41,9 +42,11 @@ assertImpl(
|
||||
)
|
||||
{
|
||||
if (!condition) {
|
||||
fmt::println(stderr, "Assertion '{}' failed at {}:{}:", expression, location.file_name(), location.line());
|
||||
fmt::println(stderr, format, std::forward<Args>(args)...);
|
||||
fmt::println(stderr, "Stacktrace:\n{}\n", boost::stacktrace::stacktrace());
|
||||
LOG(LogService::fatal()) << "Assertion '" << expression << "' failed at " << location.file_name() << ":"
|
||||
<< location.line() << ":\n"
|
||||
<< fmt::format(format, std::forward<Args>(args)...) << "\n"
|
||||
<< "Stacktrace:\n"
|
||||
<< boost::stacktrace::stacktrace() << "\n";
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <boost/log/expressions/predicates/channel_severity_filter.hpp>
|
||||
#include <boost/log/keywords/auto_flush.hpp>
|
||||
#include <boost/log/keywords/file_name.hpp>
|
||||
#include <boost/log/keywords/filter.hpp>
|
||||
#include <boost/log/keywords/format.hpp>
|
||||
#include <boost/log/keywords/max_size.hpp>
|
||||
#include <boost/log/keywords/open_mode.hpp>
|
||||
@@ -112,9 +113,14 @@ LogService::init(util::Config const& config)
|
||||
std::string format = config.valueOr<std::string>("log_format", defaultFormat);
|
||||
|
||||
if (config.valueOr("log_to_console", false)) {
|
||||
boost::log::add_console_log(std::cout, keywords::format = format);
|
||||
boost::log::add_console_log(
|
||||
std::cout, keywords::format = format, keywords::filter = log_severity < Severity::FTL
|
||||
);
|
||||
}
|
||||
|
||||
// Always print fatal logs to cerr
|
||||
boost::log::add_console_log(std::cerr, keywords::format = format, keywords::filter = log_severity >= Severity::FTL);
|
||||
|
||||
if (auto logDir = config.maybeValue<std::string>("log_directory"); logDir) {
|
||||
boost::filesystem::path dirPath{logDir.value()};
|
||||
if (!boost::filesystem::exists(dirPath))
|
||||
|
||||
@@ -28,5 +28,5 @@ TEST(AssertTests, assertTrue)
|
||||
|
||||
TEST(AssertTests, assertFalse)
|
||||
{
|
||||
EXPECT_DEATH({ ASSERT(false, "failure"); }, "failure");
|
||||
EXPECT_DEATH({ ASSERT(false, "failure"); }, ".*");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user