Skip to content

Commit

Permalink
improve error logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed May 2, 2024
1 parent 216b24a commit 568c3c6
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/c4/yml/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@
#include <cstddef>
#include <c4/substr.hpp>
#include <c4/yml/export.hpp>
#include <cstdio>

#ifndef C4_MSVC
#include <alloca.h>
#else
#include <malloc.h>
#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


Expand Down Expand Up @@ -448,15 +463,17 @@ 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; }
C4_ALWAYS_INLINE size_t required_len() const noexcept { return str.len; }
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; }
Expand All @@ -471,7 +488,9 @@ struct FilterResultExtending
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


namespace detail {
// is there a better way to do this?
template<int8_t signedval, uint8_t unsignedval>
struct _charconstant_t
: public std::conditional<std::is_signed<char>::value,
Expand Down Expand Up @@ -529,45 +548,32 @@ namespace detail {
template<class DumpFn, class ...Args>
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>(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>(args)...);
if(C4_UNLIKELY(results.bufsize > sizeof(writebuf)))
{
results = format_dump_resume(dumpfn, results, writebuf, fmt, std::forward<Args>(args)...);
C4_CHECK(results.bufsize <= sizeof(writebuf));
}
const size_t bufsize = results.bufsize <= RYML_LOGBUF_SIZE_MAX ? results.bufsize : RYML_LOGBUF_SIZE_MAX;

Check warning on line 556 in src/c4/yml/common.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/common.hpp#L556

Added line #L556 was not covered by tests
#ifdef C4_MSVC
substr largerbuf = {static_cast<char*>(_alloca(bufsize)), results.bufsize};
#else
substr largerbuf = {static_cast<char*>(alloca(bufsize)), results.bufsize};

Check warning on line 560 in src/c4/yml/common.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/common.hpp#L560

Added line #L560 was not covered by tests
#endif
results = format_dump_resume(dumpfn, results, largerbuf, fmt, std::forward<Args>(args)...);

Check warning on line 562 in src/c4/yml/common.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/common.hpp#L562

Added line #L562 was not covered by tests
}
}
template<class ...Args>
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<class ...Args>
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();

Check warning on line 575 in src/c4/yml/common.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/common.hpp#L575

Added line #L575 was not covered by tests
}

} // namespace detail


Expand Down

0 comments on commit 568c3c6

Please sign in to comment.