Skip to content

Commit

Permalink
Add ability to use ascdump/hexdump with printf (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergiusTheBest committed Jun 29, 2024
1 parent 5e86847 commit 55aa95f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/plog/Helpers/AscDump.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ namespace plog
{
}

util::nstring str() const
{
util::nostringstream ss;
ss << *this;
return ss.str();
}

friend util::nostringstream& operator<<(util::nostringstream& stream, const AscDump& ascDump);

private:
Expand Down
7 changes: 7 additions & 0 deletions include/plog/Helpers/HexDump.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ namespace plog
return *this;
}

util::nstring str() const
{
util::nostringstream ss;
ss << *this;
return ss.str();
}

friend util::nostringstream& operator<<(util::nostringstream& stream, const HexDump&);

private:
Expand Down
23 changes: 23 additions & 0 deletions samples/AscDump/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 23 additions & 0 deletions samples/HexDump/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 55aa95f

Please sign in to comment.