Skip to content

Commit e471274

Browse files
author
lexeyo
committed
fix logging: avoid termination on LoggerHelper::Impl instantiation error
Avoids termination on a LoggerHelper::Impl instantiation error. Implements fallback to a null logger in this case. commit_hash:569ef03531986963210e4d7a21ccb5332cc62c64
1 parent 8f63cce commit e471274

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

universal/src/logging/log_helper.cpp

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include <cstdio>
44
#include <memory>
5-
#include <typeinfo>
5+
#include <ostream>
66

77
#include <fmt/compile.h>
8+
#include <boost/config.hpp>
89
#include <boost/container/small_vector.hpp>
910
#include <boost/exception/diagnostic_information.hpp>
1011

@@ -109,13 +110,15 @@ constexpr bool NeedsQuoteEscaping(char c) { return c == '\"' || c == '\\'; }
109110

110111
} // namespace
111112

112-
LogHelper::LogHelper(
113-
LoggerRef logger,
114-
Level level,
115-
LogClass log_class,
116-
const utils::impl::SourceLocation& location
117-
) noexcept
118-
: pimpl_(ThreadLocalMemPool<Impl>::Pop(logger, level, log_class, location)) {
113+
LogHelper::LogHelper(LoggerRef logger, Level level, LogClass log_class, const utils::impl::SourceLocation& location)
114+
noexcept {
115+
try {
116+
pimpl_ = ThreadLocalMemPool<Impl>::Pop(logger, level, log_class, location);
117+
} catch (...) {
118+
InternalLoggingError("Failed to create an implementation instance. Logger is non-functional");
119+
return;
120+
}
121+
119122
try {
120123
logger.PrependCommonTags(GetTagWriter());
121124
} catch (...) {
@@ -132,15 +135,24 @@ LogHelper::LogHelper(
132135
: LogHelper(logger ? *logger : logging::GetNullLogger(), level, log_class, location) {}
133136

134137
LogHelper::~LogHelper() {
138+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
139+
return;
140+
}
135141
DoLog();
136142
ThreadLocalMemPool<Impl>::Push(std::move(pimpl_));
137143
}
138144

139145
constexpr size_t kSizeLimit = 10000;
140146

141-
bool LogHelper::IsLimitReached() const noexcept { return pimpl_->GetTextSize() >= kSizeLimit; }
147+
bool LogHelper::IsLimitReached() const noexcept {
148+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
149+
return true;
150+
}
151+
return pimpl_->GetTextSize() >= kSizeLimit;
152+
}
142153

143154
void LogHelper::DoLog() noexcept {
155+
UASSERT(pimpl_ != nullptr);
144156
try {
145157
pimpl_->Finish();
146158
} catch (...) {
@@ -158,7 +170,9 @@ void LogHelper::InternalLoggingError(std::string_view message) noexcept {
158170
// ignore
159171
exc_info = "unknown"; // fits into SSO
160172
}
161-
pimpl_->MarkAsBroken();
173+
if (BOOST_LIKELY(pimpl_ != nullptr)) {
174+
pimpl_->MarkAsBroken();
175+
}
162176
UASSERT_MSG(false, fmt::format("{}: {}", message, exc_info));
163177
}
164178

@@ -268,6 +282,9 @@ LogHelper& LogHelper::operator<<(LogExtra&& extra) noexcept {
268282
}
269283

270284
LogHelper& LogHelper::PutTag(std::string_view key, const LogExtra::Value& value) noexcept {
285+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
286+
return *this;
287+
}
271288
try {
272289
pimpl_->AddTag(key, value);
273290
} catch (...) {
@@ -277,6 +294,9 @@ LogHelper& LogHelper::PutTag(std::string_view key, const LogExtra::Value& value)
277294
}
278295

279296
LogHelper& LogHelper::PutSwTag(std::string_view key, std::string_view value) noexcept {
297+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
298+
return *this;
299+
}
280300
try {
281301
pimpl_->AddTag(key, value);
282302
} catch (...) {
@@ -286,25 +306,46 @@ LogHelper& LogHelper::PutSwTag(std::string_view key, std::string_view value) noe
286306
}
287307

288308
void LogHelper::PutFloatingPoint(float value) {
309+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
310+
return;
311+
}
289312
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
290313
}
291314
void LogHelper::PutFloatingPoint(double value) {
315+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
316+
return;
317+
}
292318
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
293319
}
294320
void LogHelper::PutFloatingPoint(long double value) {
321+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
322+
return;
323+
}
295324
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
296325
}
297326
void LogHelper::PutUnsigned(unsigned long long value) {
327+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
328+
return;
329+
}
298330
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
299331
}
300332
void LogHelper::PutSigned(long long value) {
333+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
334+
return;
335+
}
301336
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
302337
}
303338
void LogHelper::PutBoolean(bool value) {
339+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
340+
return;
341+
}
304342
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{}"), value);
305343
}
306344

307345
LogHelper& LogHelper::operator<<(Hex hex) noexcept {
346+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
347+
return *this;
348+
}
308349
try {
309350
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("0x{:016X}"), hex.value);
310351
} catch (...) {
@@ -314,6 +355,9 @@ LogHelper& LogHelper::operator<<(Hex hex) noexcept {
314355
}
315356

316357
LogHelper& LogHelper::operator<<(HexShort hex) noexcept {
358+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
359+
return *this;
360+
}
317361
try {
318362
fmt::format_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), FMT_COMPILE("{:X}"), hex.value);
319363
} catch (...) {
@@ -331,11 +375,24 @@ LogHelper& LogHelper::operator<<(Quoted value) noexcept {
331375
return *this;
332376
}
333377

334-
void LogHelper::Put(std::string_view value) { pimpl_->AddText(value); }
378+
void LogHelper::Put(std::string_view value) {
379+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
380+
return;
381+
}
382+
pimpl_->AddText(value);
383+
}
335384

336-
void LogHelper::Put(char value) { pimpl_->AddText(std::string_view(&value, 1)); }
385+
void LogHelper::Put(char value) {
386+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
387+
return;
388+
}
389+
pimpl_->AddText(std::string_view(&value, 1));
390+
}
337391

338392
void LogHelper::PutRaw(std::string_view value_needs_no_escaping) {
393+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
394+
return;
395+
}
339396
pimpl_->GetBufferForRawValuePart().append(value_needs_no_escaping);
340397
}
341398

@@ -364,6 +421,10 @@ void LogHelper::PutException(const std::exception& ex) {
364421
}
365422

366423
void LogHelper::PutQuoted(std::string_view value) {
424+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
425+
return;
426+
}
427+
367428
constexpr size_t kQuotesSize = 2;
368429

369430
const auto old_message_size = pimpl_->GetTextSize();
@@ -395,16 +456,30 @@ void LogHelper::PutQuoted(std::string_view value) {
395456
}
396457

397458
void LogHelper::VFormat(fmt::string_view fmt, fmt::format_args args) noexcept {
459+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
460+
return;
461+
}
398462
try {
399463
fmt::vformat_to(fmt::appender(pimpl_->GetBufferForRawValuePart()), fmt, args);
400464
} catch (...) {
401465
InternalLoggingError("Failed to extend log with fmt::format_args");
402466
}
403467
}
404468

405-
std::ostream& LogHelper::Stream() { return pimpl_->Stream(); }
469+
std::ostream& LogHelper::Stream() {
470+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
471+
static std::ostream null_stream(nullptr);
472+
return null_stream;
473+
}
474+
return pimpl_->Stream();
475+
}
406476

407-
void LogHelper::FlushStream() { pimpl_->Stream().flush(); }
477+
void LogHelper::FlushStream() {
478+
if (BOOST_UNLIKELY(pimpl_ == nullptr)) {
479+
return;
480+
}
481+
pimpl_->Stream().flush();
482+
}
408483

409484
LogHelper& operator<<(LogHelper& lh, std::chrono::system_clock::time_point tp) {
410485
lh << utils::datetime::UtcTimestring(tp);

0 commit comments

Comments
 (0)