115 #include <string_view>
116 #include <type_traits>
176 #ifdef VLINK_LOG_LEVEL
177 static constexpr uint8_t kMinimumLevel = VLINK_LOG_LEVEL;
179 static constexpr uint8_t kMinimumLevel = kTrace;
189 #ifdef VLINK_LOG_DETAIL_LEVEL
190 static constexpr uint8_t kDetailLevel = VLINK_LOG_DETAIL_LEVEL;
192 static constexpr uint8_t kDetailLevel = kWarn;
201 static constexpr
int kLocalBufferSize = 4096;
249 static void init(
const std::string& app_name =
"",
const std::string& log_path =
"") noexcept;
267 static
void flush() noexcept;
274 static
void register_console_handler(
Callback&& callback) noexcept;
281 static
void register_file_handler(
Callback&& callback) noexcept;
288 static
void set_console_level(
Level level) noexcept;
295 static
void set_file_level(
Level level) noexcept;
306 static
void set_console_fmt_enable(
bool enable) noexcept;
313 [[nodiscard]] static
Level get_console_level() noexcept;
320 [[nodiscard]] static
Level get_file_level() noexcept;
327 [[nodiscard]] static
bool get_console_fmt_enable() noexcept;
334 static
void set_stream_flag(std::ios_base::fmtflags flags) noexcept;
341 static
void set_stream_precision(
int precision) noexcept;
348 static
void set_stream_width(
int width) noexcept;
355 [[nodiscard]] static std::ios_base::fmtflags get_stream_flag() noexcept;
362 [[nodiscard]] static
int get_stream_precision() noexcept;
369 [[nodiscard]] static
int get_stream_width() noexcept;
383 static
void enable_backtrace(
size_t size) noexcept;
390 static
void disable_backtrace() noexcept;
401 static
void dump_backtrace() noexcept;
408 [[nodiscard]] static
bool is_busy() noexcept;
421 [[nodiscard]] static
bool is_writable(
Level level) noexcept;
431 [[nodiscard]] static
bool try_acquire_periodic_log(
Level level, int64_t interval_ms,
432 std::atomic<uint64_t>& last_log_time_ns) noexcept;
440 [[nodiscard]] static constexpr std::string_view extract_filename(std::string_view path) noexcept;
455 template <
Level LevelT, typename DetailT, typename... ArgsT>
456 static
void print_stream_style(DetailT&& detail, ArgsT&&... args);
468 template <
Level LevelT, typename DetailT, typename... ArgsT>
482 template <
Logger::
Level LevelT, typename DetailT, typename FormatT, typename... ArgsT>
483 static
void print_c_style(DetailT&& detail, FormatT&&
format, ArgsT&&... args);
492 template <
Level LevelT, typename... ArgsT>
493 static
void print(ArgsT&&... args);
496 struct StreamGuard final {
497 StreamGuard() noexcept =
default;
499 StreamGuard(StreamGuard&& other) noexcept : ptr(std::exchange(other.ptr,
nullptr)) {}
501 ~StreamGuard() noexcept {
503 release_local_stream(ptr);
507 void acquire() noexcept { ptr = &get_local_stream(); }
525 template <Logger::Level LevelT>
527 static constexpr
bool kIsEnabled = (LevelT >= kMinimumLevel && LevelT <
Logger::kOff);
530 if constexpr (kIsEnabled) {
531 if VLIKELY (should_log<LevelT>()) {
538 if constexpr (kIsEnabled) {
539 if VLIKELY (should_log<LevelT>()) {
542 push_detail_to_stream(detail, *stream_.ptr);
552 if constexpr (kIsEnabled) {
554 finalize_log<LevelT>(stream_.ptr->take_view());
559 template <
typename T>
561 if constexpr (kIsEnabled) {
563 stream_.ptr->push(std::forward<T>(t));
575 explicit operator bool() const noexcept {
return stream_.ptr !=
nullptr; }
588 template <Level LevelT>
589 static
bool should_log() noexcept;
591 static
bool can_log(Level level) noexcept;
593 static
void write(Level level, std::string_view log) noexcept;
595 template <Level LevelT>
596 static
void finalize_log(std::string_view log_view);
598 template <typename DetailT>
599 static
void push_detail_to_stream(DetailT&& detail,
FastStream& stream) noexcept;
601 template <typename DetailT>
602 static std::string_view format_with_detail(DetailT&& detail, const
char* msg,
int len, StreamGuard& guard) noexcept;
604 static
FastStream& get_local_stream() noexcept;
606 static
void release_local_stream(
FastStream* stream) noexcept;
608 void initialize_file_channel() noexcept;
610 void initialize_plugin() noexcept;
612 static
void write_to_console(Level level, std::string_view log,
bool formatted = false) noexcept;
614 static
void write_to_console_line(Level level, std::string_view log) noexcept;
616 void write_to_file(Level level, std::string_view log) noexcept;
619 std::unique_ptr<Impl> impl_;
621 template <
Logger::Level LevelT>
633 inline constexpr std::string_view
Logger::extract_filename(std::string_view path) noexcept {
634 auto pos = path.find_last_of(
"/\\");
635 return (pos == std::string_view::npos) ? path : path.substr(pos + 1);
638 template <
Logger::Level LevelT,
typename DetailT,
typename... ArgsT>
646 auto& stream = *guard.ptr;
648 if constexpr (std::is_same_v<std::decay_t<DetailT>,
DetailInfo>) {
649 push_detail_to_stream(detail, stream);
652 (stream.push(args), ...);
654 finalize_log<LevelT>(stream.take_view());
657 template <
Logger::Level LevelT,
typename DetailT,
typename... ArgsT>
660 [[maybe_unused]] ArgsT&&... args) {
665 std::string_view log_view;
668 char local_buffer[kLocalBufferSize];
670 auto written =
static_cast<int>(result.out - local_buffer);
672 local_buffer[written] =
'\0';
674 log_view = format_with_detail(detail, local_buffer, written, guard);
676 finalize_log<LevelT>(log_view);
679 template <
Logger::Level LevelT,
typename DetailT,
typename FormatT,
typename... ArgsT>
681 [[maybe_unused]] ArgsT&&... args) {
688 if constexpr (
sizeof...(ArgsT) == 0) {
690 auto& stream = *guard.ptr;
692 if constexpr (std::is_same_v<std::decay_t<DetailT>,
DetailInfo>) {
693 push_detail_to_stream(detail, stream);
697 finalize_log<LevelT>(stream.take_view());
699 char local_buffer[kLocalBufferSize];
700 auto written = std::snprintf(local_buffer, kLocalBufferSize,
format, args...);
704 }
else if VUNLIKELY (written >= kLocalBufferSize) {
705 written = kLocalBufferSize - 1;
708 finalize_log<LevelT>(format_with_detail(detail, local_buffer, written, guard));
714 print_stream_style<LevelT>(
NoDetail{}, args...);
717 template <Logger::Level LevelT>
718 inline bool Logger::should_log() noexcept {
719 if constexpr (LevelT < Logger::kMinimumLevel || LevelT >=
Logger::kOff) {
724 return Logger::can_log(LevelT);
728 template <Logger::Level LevelT>
729 inline void Logger::finalize_log(std::string_view log_view) {
730 Logger::write(LevelT, log_view);
734 throw Exception::RuntimeError(std::string(log_view));
738 template <
typename DetailT>
739 inline void Logger::push_detail_to_stream(DetailT&& detail, FastStream& stream) noexcept {
740 auto& [file, line] = detail;
748 template <
typename DetailT>
749 inline std::string_view Logger::format_with_detail(DetailT&& detail,
const char* msg,
int len,
750 StreamGuard& guard) noexcept {
753 auto& stream = *guard.ptr;
755 push_detail_to_stream(detail, stream);
758 stream.write_raw(msg,
static_cast<size_t>(len));
761 return stream.take_view();
764 return std::string_view(msg,
static_cast<size_t>(len));
779 #define VLINK_LOG_GET_DETAIL(level) \
781 if constexpr ((level) >= VLinkLogger::kDetailLevel) { \
782 return VLinkLogger::DetailInfo{VLinkLogger::extract_filename(__FILE__), __LINE__}; \
784 return VLinkLogger::NoDetail{}; \
788 #define VLINK_LOG_HEX(offset) std::hex, std::uppercase, std::setw(offset), std::setfill('0')
790 #define VLINK_LOG_HEXSS(offset) std::hex << std::uppercase << std::setw(offset) << std::setfill('0')
792 #define VLINK_LOG_IF_T VLinkLogger::is_writable(VLinkLogger::kTrace)
794 #define VLINK_LOG_IF_D VLinkLogger::is_writable(VLinkLogger::kDebug)
796 #define VLINK_LOG_IF_I VLinkLogger::is_writable(VLinkLogger::kInfo)
798 #define VLINK_LOG_IF_W VLinkLogger::is_writable(VLinkLogger::kWarn)
800 #define VLINK_LOG_IF_E VLinkLogger::is_writable(VLinkLogger::kError)
802 #define VLINK_LOG_IF_F VLinkLogger::is_writable(VLinkLogger::kFatal)
804 #define VLINK_LOG_IS_WRITABLE(level) \
805 ((level) >= VLinkLogger::kMinimumLevel && (level) < VLinkLogger::kOff && VLinkLogger::is_writable(level))
807 #define VLINK_LOG_CALL(level, function, ...) \
808 (VLINK_LOG_IS_WRITABLE(level) ? VLinkLogger::function<level>(VLINK_LOG_GET_DETAIL(level), __VA_ARGS__) \
809 : static_cast<void>(0))
811 #define VLINK_LOG_T(...) VLINK_LOG_CALL(VLinkLogger::kTrace, print_stream_style, __VA_ARGS__)
813 #define VLINK_LOG_D(...) VLINK_LOG_CALL(VLinkLogger::kDebug, print_stream_style, __VA_ARGS__)
815 #define VLINK_LOG_I(...) VLINK_LOG_CALL(VLinkLogger::kInfo, print_stream_style, __VA_ARGS__)
817 #define VLINK_LOG_W(...) VLINK_LOG_CALL(VLinkLogger::kWarn, print_stream_style, __VA_ARGS__)
819 #define VLINK_LOG_E(...) VLINK_LOG_CALL(VLinkLogger::kError, print_stream_style, __VA_ARGS__)
821 #define VLINK_LOG_F(...) VLINK_LOG_CALL(VLinkLogger::kFatal, print_stream_style, __VA_ARGS__)
823 #define VLINK_MLOG_T(...) VLINK_LOG_CALL(VLinkLogger::kTrace, print_format_style, __VA_ARGS__)
825 #define VLINK_MLOG_D(...) VLINK_LOG_CALL(VLinkLogger::kDebug, print_format_style, __VA_ARGS__)
827 #define VLINK_MLOG_I(...) VLINK_LOG_CALL(VLinkLogger::kInfo, print_format_style, __VA_ARGS__)
829 #define VLINK_MLOG_W(...) VLINK_LOG_CALL(VLinkLogger::kWarn, print_format_style, __VA_ARGS__)
831 #define VLINK_MLOG_E(...) VLINK_LOG_CALL(VLinkLogger::kError, print_format_style, __VA_ARGS__)
833 #define VLINK_MLOG_F(...) VLINK_LOG_CALL(VLinkLogger::kFatal, print_format_style, __VA_ARGS__)
835 #define VLINK_CLOG_T(...) VLINK_LOG_CALL(VLinkLogger::kTrace, print_c_style, __VA_ARGS__)
837 #define VLINK_CLOG_D(...) VLINK_LOG_CALL(VLinkLogger::kDebug, print_c_style, __VA_ARGS__)
839 #define VLINK_CLOG_I(...) VLINK_LOG_CALL(VLinkLogger::kInfo, print_c_style, __VA_ARGS__)
841 #define VLINK_CLOG_W(...) VLINK_LOG_CALL(VLinkLogger::kWarn, print_c_style, __VA_ARGS__)
843 #define VLINK_CLOG_E(...) VLINK_LOG_CALL(VLinkLogger::kError, print_c_style, __VA_ARGS__)
845 #define VLINK_CLOG_F(...) VLINK_LOG_CALL(VLinkLogger::kFatal, print_c_style, __VA_ARGS__)
847 #define VLINK_SLOG_IMPL(level) \
848 VLINK_LOG_IS_WRITABLE(level) && VLinkLogger::WrapperStream<level>(VLINK_LOG_GET_DETAIL(level))
850 #define VLINK_SLOG_T VLINK_SLOG_IMPL(VLinkLogger::kTrace)
852 #define VLINK_SLOG_D VLINK_SLOG_IMPL(VLinkLogger::kDebug)
854 #define VLINK_SLOG_I VLINK_SLOG_IMPL(VLinkLogger::kInfo)
856 #define VLINK_SLOG_W VLINK_SLOG_IMPL(VLinkLogger::kWarn)
858 #define VLINK_SLOG_E VLINK_SLOG_IMPL(VLinkLogger::kError)
860 #define VLINK_SLOG_F VLINK_SLOG_IMPL(VLinkLogger::kFatal)
862 #define VLINK_LOG_EVERY_MS_IMPL(level, interval_ms, ...) \
864 if constexpr ((level) >= VLinkLogger::kMinimumLevel && (level) < VLinkLogger::kFatal) { \
865 if VUNLIKELY ([](int64_t vlink_interval_ms) noexcept { \
866 static std::atomic<uint64_t> vlink_last_log_time_ns{0}; \
867 return VLinkLogger::try_acquire_periodic_log(level, vlink_interval_ms, vlink_last_log_time_ns); \
868 }(static_cast<int64_t>(interval_ms))) { \
869 VLinkLogger::print_stream_style<level>(VLINK_LOG_GET_DETAIL(level), __VA_ARGS__); \
874 #define VLINK_LOG_T_EVERY_MS(interval_ms, ...) VLINK_LOG_EVERY_MS_IMPL(VLinkLogger::kTrace, interval_ms, __VA_ARGS__)
876 #define VLINK_LOG_D_EVERY_MS(interval_ms, ...) VLINK_LOG_EVERY_MS_IMPL(VLinkLogger::kDebug, interval_ms, __VA_ARGS__)
878 #define VLINK_LOG_I_EVERY_MS(interval_ms, ...) VLINK_LOG_EVERY_MS_IMPL(VLinkLogger::kInfo, interval_ms, __VA_ARGS__)
880 #define VLINK_LOG_W_EVERY_MS(interval_ms, ...) VLINK_LOG_EVERY_MS_IMPL(VLinkLogger::kWarn, interval_ms, __VA_ARGS__)
882 #define VLINK_LOG_E_EVERY_MS(interval_ms, ...) VLINK_LOG_EVERY_MS_IMPL(VLinkLogger::kError, interval_ms, __VA_ARGS__)
884 #ifndef VLINK_LOG_DISABLE_SHORT
886 #define VLOG_T_EVERY_MS(interval_ms, ...) VLINK_LOG_T_EVERY_MS(interval_ms, __VA_ARGS__)
888 #define VLOG_D_EVERY_MS(interval_ms, ...) VLINK_LOG_D_EVERY_MS(interval_ms, __VA_ARGS__)
890 #define VLOG_I_EVERY_MS(interval_ms, ...) VLINK_LOG_I_EVERY_MS(interval_ms, __VA_ARGS__)
892 #define VLOG_W_EVERY_MS(interval_ms, ...) VLINK_LOG_W_EVERY_MS(interval_ms, __VA_ARGS__)
894 #define VLOG_E_EVERY_MS(interval_ms, ...) VLINK_LOG_E_EVERY_MS(interval_ms, __VA_ARGS__)
896 #define VLOG_T(...) VLINK_LOG_T(__VA_ARGS__)
898 #define VLOG_D(...) VLINK_LOG_D(__VA_ARGS__)
900 #define VLOG_I(...) VLINK_LOG_I(__VA_ARGS__)
902 #define VLOG_W(...) VLINK_LOG_W(__VA_ARGS__)
904 #define VLOG_E(...) VLINK_LOG_E(__VA_ARGS__)
906 #define VLOG_F(...) VLINK_LOG_F(__VA_ARGS__)
908 #define CLOG_T(...) VLINK_CLOG_T(__VA_ARGS__)
910 #define CLOG_D(...) VLINK_CLOG_D(__VA_ARGS__)
912 #define CLOG_I(...) VLINK_CLOG_I(__VA_ARGS__)
914 #define CLOG_W(...) VLINK_CLOG_W(__VA_ARGS__)
916 #define CLOG_E(...) VLINK_CLOG_E(__VA_ARGS__)
918 #define CLOG_F(...) VLINK_CLOG_F(__VA_ARGS__)
920 #define MLOG_T(...) VLINK_MLOG_T(__VA_ARGS__)
922 #define MLOG_D(...) VLINK_MLOG_D(__VA_ARGS__)
924 #define MLOG_I(...) VLINK_MLOG_I(__VA_ARGS__)
926 #define MLOG_W(...) VLINK_MLOG_W(__VA_ARGS__)
928 #define MLOG_E(...) VLINK_MLOG_E(__VA_ARGS__)
930 #define MLOG_F(...) VLINK_MLOG_F(__VA_ARGS__)
932 #define SLOG_T VLINK_SLOG_T
934 #define SLOG_D VLINK_SLOG_D
936 #define SLOG_I VLINK_SLOG_I
938 #define SLOG_W VLINK_SLOG_W
940 #define SLOG_E VLINK_SLOG_E
942 #define SLOG_F VLINK_SLOG_F
Logger-friendly std::ostream with a custom growable string buffer.
Definition: fast_stream.h:91
Global singleton logger with four formatting styles and independently configurable sinks.
Definition: logger.h:139
static void print_stream_style(DetailT &&detail, ArgsT &&... args)
Stream-style entry point (used by VLOG_* / print).
static void print_format_style(DetailT &&detail, format::format_string< ArgsT... > format, ArgsT &&... args)
Placeholder-style entry point (used by MLOG_*).
Level
Message severity level.
Definition: logger.h:159
@ kOff
Disable sink.
Definition: logger.h:166
@ kFatal
Unrecoverable; throws Exception::RuntimeError.
Definition: logger.h:165
static void print(ArgsT &&... args)
Convenience stream-style entry point without source location.
Style
Internal output style tag used by the print_* family.
Definition: logger.h:147
static void print_c_style(DetailT &&detail, FormatT &&format, ArgsT &&... args)
printf-style entry point (used by CLOG_*).
static void flush() noexcept
Flushes stdout, stderr and the active built-in file backend or plugin.
std::pair< std::string_view, int > DetailInfo
Carries the source file name and line number for the detail prefix.
Definition: logger.h:224
static void init(const std::string &app_name="", const std::string &log_path="") noexcept
Initialises the logger singleton.
Move-only type-erased callable analogue of std::move_only_function with pool spill.
Definition: functional.h:135
Thin final wrappers around the standard exception hierarchy used by VLink.
Allocation-light std::ostream backed by a growable string buffer.
Pool-backed type-erased callables: copyable vlink::Function and move-only vlink::MoveFunction.
Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLINK_SINGLETON_CHECK(classname)
Embeds a static check that enforces one-instance-per-process for classname.
Definition: macros.h:192
#define VLINK_EXPORT
Definition: macros.h:81
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
constexpr std::string_view get() noexcept
Returns the trimmed compile-time identifier for TypeT.
Definition: name_detector.h:603
Sentinel type indicating no detail prefix is attached.
Definition: logger.h:232
RAII helper backing SLOG_*; collects tokens and flushes on destruction.
Definition: logger.h:526
WrapperStream(WrapperStream &&other) noexcept=default
WrapperStream & operator<<(T &&t) noexcept
Definition: logger.h:560
~WrapperStream() noexcept(LevelT !=Level::kFatal)
Definition: logger.h:551
WrapperStream(Logger::NoDetail) noexcept
Definition: logger.h:529
WrapperStream(DetailInfo &&detail) noexcept
Definition: logger.h:537
WrapperStream & operator=(WrapperStream &&)=delete