From eebe143ea5ed89166423ed9a1c589b0bb082fead Mon Sep 17 00:00:00 2001 From: Sergey Podobry Date: Sat, 29 Jun 2024 00:05:56 +0300 Subject: [PATCH] Add ability to use ascdump/hexdump with printf (#290) --- include/plog/Helpers/AscDump.h | 5 +++++ include/plog/Helpers/HexDump.h | 5 +++++ samples/AscDump/Main.cpp | 23 +++++++++++++++++++++++ samples/HexDump/Main.cpp | 23 +++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/include/plog/Helpers/AscDump.h b/include/plog/Helpers/AscDump.h index 18b9a6f..c61d267 100644 --- a/include/plog/Helpers/AscDump.h +++ b/include/plog/Helpers/AscDump.h @@ -13,6 +13,11 @@ namespace plog { } + util::nstring str() const + { + return (util::nostringstream() << *this).str(); + } + friend util::nostringstream& operator<<(util::nostringstream& stream, const AscDump& ascDump); private: diff --git a/include/plog/Helpers/HexDump.h b/include/plog/Helpers/HexDump.h index b0493d7..371f232 100644 --- a/include/plog/Helpers/HexDump.h +++ b/include/plog/Helpers/HexDump.h @@ -35,6 +35,11 @@ namespace plog return *this; } + util::nstring str() const + { + return (util::nostringstream() << *this).str(); + } + friend util::nostringstream& operator<<(util::nostringstream& stream, const HexDump&); private: diff --git a/samples/AscDump/Main.cpp b/samples/AscDump/Main.cpp index 9707830..512aeff 100644 --- a/samples/AscDump/Main.cpp +++ b/samples/AscDump/Main.cpp @@ -28,5 +28,28 @@ int main() void* p = malloc(100); PLOGI << "p: " << plog::ascdump(p, 100); + // `plog::ascdump` can be used with `printf`. Note that `.str()` returns `nstring`, + // so it can be narrow or wide and thus requires a proper format specifier. + // + // If `nstring` is wide: + // - use wide format string and %s (preferable) + // - use narrow format string and %S + // If `nstring` is narrow: + // - use narrow format string and %s (preferable) + // + // Wrap format string with `PLOG_NSTR` to automatically support wide and narrow strings with %s. + // + // `nstring` is narrow if char encoding is UTF-8 otherwise it's wide. This allows to + // support all symbols from different languages. + + PLOGI.printf(PLOG_NSTR("printf: %s"), plog::ascdump(p, 100).str().c_str()); + +#if PLOG_CHAR_IS_UTF8 + PLOGI.printf("printf: %s", plog::ascdump(p, 100).str().c_str()); +#else + PLOGI.printf(L"printf: %s", plog::ascdump(p, 100).str().c_str()); + PLOGI.printf("printf: %S", plog::ascdump(p, 100).str().c_str()); +#endif + return 0; } diff --git a/samples/HexDump/Main.cpp b/samples/HexDump/Main.cpp index 5f2868b..79c2415 100644 --- a/samples/HexDump/Main.cpp +++ b/samples/HexDump/Main.cpp @@ -40,5 +40,28 @@ int main() void* p = malloc(100); PLOGI << "p: " << plog::hexdump(p, 100); + // `plog::hexdump` can be used with `printf`. Note that `.str()` returns `nstring`, + // so it can be narrow or wide and thus requires a proper format specifier. + // + // If `nstring` is wide: + // - use wide format string and %s (preferable) + // - use narrow format string and %S + // If `nstring` is narrow: + // - use narrow format string and %s (preferable) + // + // Wrap format string with `PLOG_NSTR` to automatically support wide and narrow strings with %s. + // + // `nstring` is narrow if char encoding is UTF-8 otherwise it's wide. This allows to + // support all symbols from different languages. + + PLOGI.printf(PLOG_NSTR("printf: %s"), plog::hexdump(p, 100).str().c_str()); + +#if PLOG_CHAR_IS_UTF8 + PLOGI.printf("printf: %s", plog::hexdump(p, 100).str().c_str()); +#else + PLOGI.printf(L"printf: %s", plog::hexdump(p, 100).str().c_str()); + PLOGI.printf("printf: %S", plog::hexdump(p, 100).str().c_str()); +#endif + return 0; }