Skip to content

Commit 225c563

Browse files
Add ASCII dumper (#213)
1 parent 3fb1671 commit 225c563

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ There are a number of samples that demonstrate various aspects of using plog. Th
10021002
|------|-----------|
10031003
|[Android](samples/Android)|Shows how to use [AndroidAppender](#androidappender).|
10041004
|[Arduino](samples/Arduino)|Arduino sample - not finished yet!|
1005+
|[AscDump](samples/AscDump)|Shows how to use `plog::ascdump` to dump binary buffers into ASCII.|
10051006
|[Chained](samples/Chained)|Shows how to chain a logger in a shared library with the main logger (route messages).|
10061007
|[ColorConsole](samples/ColorConsole)|Shows how to use [ColorConsoleAppender](#colorconsoleappender).|
10071008
|[CustomAppender](samples/CustomAppender)|Shows how to implement a custom appender that stores log messages in memory.|

include/plog/Helpers/AscDump.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
#include <plog/Util.h>
3+
#include <cctype>
4+
5+
namespace plog
6+
{
7+
class AscDump
8+
{
9+
public:
10+
AscDump(const void* ptr, size_t size)
11+
: m_ptr(static_cast<const char*>(ptr))
12+
, m_size(size)
13+
{
14+
}
15+
16+
friend util::nostringstream& operator<<(util::nostringstream& stream, const AscDump& ascDump);
17+
18+
private:
19+
const char* m_ptr;
20+
size_t m_size;
21+
};
22+
23+
inline util::nostringstream& operator<<(util::nostringstream& stream, const AscDump& ascDump)
24+
{
25+
for (size_t i = 0; i < ascDump.m_size; ++i)
26+
{
27+
stream << (std::isprint(ascDump.m_ptr[i]) ? ascDump.m_ptr[i] : '.');
28+
}
29+
30+
return stream;
31+
}
32+
33+
inline AscDump ascdump(const void* ptr, size_t size) { return AscDump(ptr, size); }
34+
35+
template<class Container>
36+
inline AscDump ascdump(const Container& container) { return AscDump(container.data(), container.size() * sizeof(*container.data())); }
37+
38+
template<class T, size_t N>
39+
inline AscDump ascdump(const T (&arr)[N]) { return AscDump(arr, N * sizeof(*arr)); }
40+
}

samples/AscDump/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_executable(AscDump Main.cpp)
2+
target_link_libraries(AscDump plog)
3+
set_target_properties(AscDump PROPERTIES FOLDER Samples)

samples/AscDump/Main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// AscDump - shows how to use plog::ascdump to dump binary buffers into ASCII.
3+
//
4+
5+
#include <plog/Log.h>
6+
#include <plog/Init.h>
7+
#include <plog/Formatters/TxtFormatter.h>
8+
#include <plog/Appenders/ColorConsoleAppender.h>
9+
#include <plog/Helpers/AscDump.h>
10+
11+
#include <vector>
12+
13+
int main()
14+
{
15+
static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
16+
plog::init(plog::verbose, &consoleAppender);
17+
18+
std::vector<short> v;
19+
v.push_back(0x6548);
20+
v.push_back(0x6c6c);
21+
v.push_back(0x216f);
22+
v.push_back(0);
23+
v.push_back(-1);
24+
25+
PLOGI << "v: " << plog::ascdump(v);
26+
27+
unsigned char arr[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xff};
28+
PLOGI << "arr: " << plog::ascdump(arr);
29+
30+
void* p = malloc(100);
31+
PLOGI << "p: " << plog::ascdump(p, 100);
32+
33+
return 0;
34+
}

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ elseif(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGXX)
4040
endif()
4141

4242
add_subdirectory(Android)
43+
add_subdirectory(AscDump)
4344
add_subdirectory(Chained)
4445
add_subdirectory(ColorConsole)
4546
add_subdirectory(CustomAppender)

0 commit comments

Comments
 (0)