Skip to content

Examples DumpBuffer

Eduard Mishkurov edited this page May 13, 2026 · 7 revisions

DumpBuffer example

DumpBuffer

This example shows how to dump a binary buffer as text.

What it demonstrates

  • Building a small buffer with printable and non-printable bytes
  • Converting the dump to a string with Logme::DumpBuffer(...)
  • C-style logging with %s and .c_str()
  • std::format-style logging when it is enabled
  • Using different offsets and line limits

Notes

  • The example logs to the default channel and does not configure any custom channels.
  • Each dump is preceded by a short description of the format and the selected offset/limit.

Console output

2026-05-13 10:54:20:789   main(): C-style, offset=0, lineLimit=8
 4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f | Lorem ipsum dolo
 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 | r sit amet, cons
 65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 | ectetur adipisci
 6e 67 20 65 6c 69 74 2e 20 00 02 03 04 05 00 07 | ng elit. .......
 ff 09 0a 00 0c 0d 0e ff 00 11 12 13 14 00 ff 17 | ................
 18 19 00 1b 1c ff 1e 00 20 53 65 64 20 64 6f 20 | ........ Sed do 
 65 69 75 73                                     | eius
2026-05-13 10:54:20:789   main(): C-style, offset=4, lineLimit=8
     4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f | Lorem ipsum dolo
     72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 | r sit amet, cons
     65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 | ectetur adipisci
     6e 67 20 65 6c 69 74 2e 20 00 02 03 04 05 00 07 | ng elit. .......
     ff 09 0a 00 0c 0d 0e ff 00 11 12 13 14 00 ff 17 | ................
     18 19 00 1b 1c ff 1e 00 20 53 65 64 20 64 6f 20 | ........ Sed do 
     65 69 75 73                                     | eius
2026-05-13 10:54:20:789   main(): fmt-style, offset=0, lineLimit=8
 4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f | Lorem ipsum dolo
 72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 | r sit amet, cons
 65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 | ectetur adipisci
 6e 67 20 65 6c 69 74 2e 20 00 02 03 04 05 00 07 | ng elit. .......
 ff 09 0a 00 0c 0d 0e ff 00 11 12 13 14 00 ff 17 | ................
 18 19 00 1b 1c ff 1e 00 20 53 65 64 20 64 6f 20 | ........ Sed do 
 65 69 75 73                                     | eius
2026-05-13 10:54:20:789   main(): fmt-style, offset=8, lineLimit=4
         4c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f | Lorem ipsum dolo
         72 20 73 69 74 20 61 6d 65 74 2c 20 63 6f 6e 73 | r sit amet, cons
         65 63 74 65 74 75 72 20 61 64 69 70 69 73 63 69 | ectetur adipisci
         6e 67 20 65 6c 69 74 2e 20 00 02 03 04 05 00 07 | ng elit. .......

Source code

Sources

Repository folder: examples/DumpBuffer

DumpBuffer.cpp

Open in repository

#include <cstring>

#include <Logme/Logme.h>

static void FillBuffer(unsigned char* buffer, size_t size)
{
  const char* text1 =
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

  const char* text2 =
    "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";

  const char* text3 =
    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.";

  size_t pos = 0;

  // Copy first text block
  size_t len1 = strlen(text1);
  for (size_t i = 0; i < len1 && pos < size; i++, pos++)
  {
    buffer[pos] = (unsigned char)text1[i];
  }

  // Insert special / binary block
  for (size_t i = 0; i < 32 && pos < size; i++, pos++)
  {
    if (i % 5 == 0)
    {
      buffer[pos] = 0;              // zero byte
      continue;
    }

    if (i % 7 == 0)
    {
      buffer[pos] = 0xFF;           // non-printable
      continue;
    }

    buffer[pos] = (unsigned char)(i + 1); // small binary values
  }

  // Copy second text block
  size_t len2 = strlen(text2);
  for (size_t i = 0; i < len2 && pos < size; i++, pos++)
  {
    buffer[pos] = (unsigned char)text2[i];
  }

  // Another special characters block
  const char* special = "!@#$%^&*()_+-=[]{}|;':,.<>/?`~";
  size_t lenS = strlen(special);

  for (size_t i = 0; i < lenS && pos < size; i++, pos++)
  {
    buffer[pos] = (unsigned char)special[i];
  }

  // Copy final text block
  size_t len3 = strlen(text3);
  for (size_t i = 0; i < len3 && pos < size; i++, pos++)
  {
    buffer[pos] = (unsigned char)text3[i];
  }

  // Fill remaining with pattern
  for (; pos < size; pos++)
  {
    buffer[pos] = (unsigned char)('.'); // visible filler
  }
}

int main()
{
  unsigned char buffer[100];
  FillBuffer(buffer, sizeof(buffer));

  LogmeI("C-style, offset=0, lineLimit=8\n%s",
    Logme::DumpBuffer(buffer, sizeof(buffer), 0, 8).c_str()
  );

  LogmeI("C-style, offset=4, lineLimit=8\n%s",
    Logme::DumpBuffer(buffer, sizeof(buffer), 4, 8).c_str()
  );

#ifndef LOGME_DISABLE_STD_FORMAT
  fLogmeI("fmt-style, offset=0, lineLimit=8\n{}",
    Logme::DumpBuffer(buffer, sizeof(buffer), 0, 8)
  );

  fLogmeI("fmt-style, offset=8, lineLimit=4\n{}",
    Logme::DumpBuffer(buffer, sizeof(buffer), 8, 4)
  );
#endif

  return 0;
}

Previous: ChannelSetup | Next: DynamicControl

Clone this wiki locally