From 568c3c697bcc2e5099f54ade68a4fe151ec1268f Mon Sep 17 00:00:00 2001 From: Joao Paulo Magalhaes Date: Fri, 3 May 2024 00:01:22 +0200 Subject: [PATCH] improve error logging functions --- src/c4/yml/common.hpp | 64 +++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/c4/yml/common.hpp b/src/c4/yml/common.hpp index fb81aba03..592d21a79 100644 --- a/src/c4/yml/common.hpp +++ b/src/c4/yml/common.hpp @@ -6,12 +6,27 @@ #include #include #include -#include +#ifndef C4_MSVC +#include +#else +#include +#endif + + +#ifndef RYML_LOGBUF_SIZE +/// size for the log buffer. individual logs may be larger than this value. +#define RYML_LOGBUF_SIZE (256) +#endif + +#ifndef RYML_LOGBUF_SIZE_MAX +/// size for the fallback larger log buffer. +#define RYML_LOGBUF_SIZE_MAX (1024) +#endif #ifndef RYML_ERRMSG_SIZE /// size for the error message buffer - #define RYML_ERRMSG_SIZE 1024 +#define RYML_ERRMSG_SIZE 1024 #endif @@ -448,7 +463,8 @@ typedef enum { } BlockChomp_e; -/** Abstracts the fact that a filter result may not fit in the intended memory. */ +/** Abstracts the fact that a scalar filter result may not fit in the + * intended memory. */ struct FilterResult { C4_ALWAYS_INLINE bool valid() const noexcept { return str.str != nullptr; } @@ -456,7 +472,8 @@ struct FilterResult C4_ALWAYS_INLINE csubstr get() { RYML_ASSERT(valid()); return str; } csubstr str; }; -/** Abstracts the fact that a filter result may not fit in the intended memory. */ +/** Abstracts the fact that a scalar filter result may not fit in the + * intended memory. */ struct FilterResultExtending { C4_ALWAYS_INLINE bool valid() const noexcept { return str.str != nullptr; } @@ -471,7 +488,9 @@ struct FilterResultExtending //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- + namespace detail { +// is there a better way to do this? template struct _charconstant_t : public std::conditional::value, @@ -529,45 +548,32 @@ namespace detail { template C4_NO_INLINE void _parse_dump(DumpFn dumpfn, csubstr fmt, Args&& ...args) { - char writebuf[256]; + char writebuf[RYML_LOGBUF_SIZE]; auto results = format_dump_resume(dumpfn, writebuf, fmt, std::forward(args)...); // resume writing if the results failed to fit the buffer - if(C4_UNLIKELY(results.bufsize > sizeof(writebuf))) // bufsize will be that of the largest element serialized. Eg int(1), will require 1 byte. + if(C4_UNLIKELY(results.bufsize > RYML_LOGBUF_SIZE)) // bufsize will be that of the largest element serialized. Eg int(1) will require 1 byte. { - results = format_dump_resume(dumpfn, results, writebuf, fmt, std::forward(args)...); - if(C4_UNLIKELY(results.bufsize > sizeof(writebuf))) - { - results = format_dump_resume(dumpfn, results, writebuf, fmt, std::forward(args)...); - C4_CHECK(results.bufsize <= sizeof(writebuf)); - } + const size_t bufsize = results.bufsize <= RYML_LOGBUF_SIZE_MAX ? results.bufsize : RYML_LOGBUF_SIZE_MAX; + #ifdef C4_MSVC + substr largerbuf = {static_cast(_alloca(bufsize)), results.bufsize}; + #else + substr largerbuf = {static_cast(alloca(bufsize)), results.bufsize}; + #endif + results = format_dump_resume(dumpfn, results, largerbuf, fmt, std::forward(args)...); } } template -C4_NO_INLINE void _report_err(Callbacks const& C4_RESTRICT callbacks, Location const& C4_RESTRICT loc, csubstr fmt, Args const& C4_RESTRICT ...args) +C4_NORETURN C4_NO_INLINE void _report_err(Callbacks const& C4_RESTRICT callbacks, csubstr fmt, Args const& C4_RESTRICT ...args) { char errmsg[RYML_ERRMSG_SIZE] = {0}; detail::_SubstrWriter writer(errmsg); auto dumpfn = [&writer](csubstr s){ writer.append(s); }; _parse_dump(dumpfn, fmt, args...); writer.append('\n'); - if(loc.name.len) - _parse_dump(dumpfn, "{}:", loc.name); - _parse_dump(dumpfn, "{}:{}: ", loc.line, loc.col); - size_t len = writer.pos < RYML_ERRMSG_SIZE ? writer.pos : RYML_ERRMSG_SIZE; - callbacks.m_error(errmsg, len, loc, callbacks.m_user_data); -} -template -C4_NO_INLINE void _report_err(Callbacks const& C4_RESTRICT callbacks, csubstr fmt, Args const& C4_RESTRICT ...args) -{ - char errmsg[RYML_ERRMSG_SIZE] = {0}; - detail::_SubstrWriter writer(errmsg); - auto dumpfn = [&writer](csubstr s){ writer.append(s); }; - _parse_dump(dumpfn, fmt, args...); - writer.append('\n'); - size_t len = writer.pos < RYML_ERRMSG_SIZE ? writer.pos : RYML_ERRMSG_SIZE; + const size_t len = writer.pos < RYML_ERRMSG_SIZE ? writer.pos : RYML_ERRMSG_SIZE; callbacks.m_error(errmsg, len, {}, callbacks.m_user_data); + C4_UNREACHABLE_AFTER_ERR(); } - } // namespace detail