Added experimental C++ feature define `_TRACE_USE_FMT`

Fortune for tracemac's current commit: Future blessing − 末吉
master
Avril 3 years ago
parent 5e7df7a9ef
commit d681471216
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -158,6 +158,6 @@ uninstall:
cd $(INCLUDE) && find . -type f | xargs -I {} rm "$(DESTDIR)$(PREFIX)/include/{}"
-rmdir $(DESTDIR)$(PREFIX)/include/$(PROJECT)
$(PROJECT)-test: lib$(PROJECT).so
$(CC) $(CFLAGS) src/test/*.c -o $@ -l:$< -Wl,-flto -Wl,-O3 $(LDFLAGS)
$(PROJECT)-test: lib$(PROJECT).a
$(CXX) $(CXXFLAGS) src/test/*.cpp -o $@ -l:$< -lfmt -Wl,-flto -Wl,-O3 $(LDFLAGS)
-valgrind ./$@

@ -2,6 +2,14 @@
#ifndef _TRACE_H
#define _TRACE_H
#ifdef __cplusplus
#ifdef _TRACE_USE_FMT
#include <utility>
#include <fmt/format.h>
#endif
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
@ -33,5 +41,35 @@ const char* trace_name_of(enum trace_level lv);
extern const char* const _TRACE_CONTROL_ENV_NAME;
extern const char* const _trace_level_descriptions[_TRACE_LEVEL_NUM];
#ifdef __cplusplus
}
#ifdef _TRACE_USE_FMT
namespace Trace {
//TODO: How to get caller info?
template<trace_level Level, size_t N, typename... Args>
inline constexpr void printf(const char (&msg)[N], Args&&... pack) {
if (Level >= trace_max_level()) { fmt::vprint(stderr, msg, fmt::make_format_args(std::forward<Args>(pack)...)); std::putc('\n', stderr); };
}
#define _TRACE_L(level, name) template<size_t N, typename... Args> \
inline constexpr void name(const char (&msg)[N], Args&&... pack) \
{ ::Trace::printf<TRACE_LEVEL_ ## level >(msg, std::forward<Args>(pack)...); }
_TRACE_L(DEBUG, Debug);
_TRACE_L(DEBUG, Trace);
_TRACE_L(INFO, Info);
_TRACE_L(WARN, Warn);
_TRACE_L(ERROR, Error);
template<size_t N, typename... Args>
[[noreturn]]
inline constexpr void Fatal(const char (&msg)[N], Args&&... pack)
{ ::Trace::printf<TRACE_LEVEL_FATAL >(msg, std::forward<Args>(pack)...);
::std::terminate();
}
#undef _TRACE_L
}
#endif
#endif
#endif /* _TRACE_H */

@ -0,0 +1,12 @@
#include <trace.h>
int main()
{
Trace::printf<TRACE_LEVEL_DEBUG>("hello world");
Trace::Info("Info");
Trace::Warn("Warning: {}", 123);
Trace::Error("Error!: {}", "Error");
Trace::Fatal("Fatal error: {}", "terminating");
return 0;
}
Loading…
Cancel
Save