Skip to content

Utility Helpers

Eduard Mishkurov edited this page Jun 6, 2026 · 1 revision

Utility Helpers

This page collects small helper APIs that are public in logme but are not large enough to have separate feature pages.

Most functions are declared in Logme/Utils.h. Conversion helpers are declared in Logme/Convert.h, and procedure argument helpers are declared in Logme/ArgumentList.h.


String helpers

WordSplit

Logme::StringArray words;
Logme::WordSplit("one, two,, three", words, ",", true, true);

WordSplit() splits a string into Logme::StringArray (std::vector<std::string>). The output array is cleared before tokens are added.

Parameters:

  • s — source string
  • words — output array
  • delimiters — characters treated as separators; default is space and tab
  • trim — trims leading and trailing whitespace for each token when true
  • ignoreEmpty — skips empty tokens when true

Other string helpers

std::string s = Logme::TrimSpaces("  text  ");
Logme::ToLowerAsciiInplace(s);
s = Logme::ReplaceAll(s, "old", "new");

Available helpers:

  • SortLines(std::string& s) — trims, removes empty lines, sorts lines, and writes the sorted content back to s
  • TrimSpaces(const std::string& str) — returns a copy without leading/trailing whitespace
  • ToLowerAsciiInplace(std::string& str) — lowercases ASCII characters in place
  • ReplaceAll(const std::string& where, const std::string& what, const std::string& on) — replaces all occurrences of what
  • Join(const StringArray& arr, const std::string separator) — joins strings with a separator

Byte size helpers

Byte-size helpers parse and format byte counts. They are used by JSON configuration and runtime control commands for options such as max-size and queue-byte-limit.

uint64_t value = 0;

if (Logme::ParseByteSize("64Mb", value))
{
  // value == 67108864
}

uint64_t size = Logme::GetByteSize("2Gb", 0);
std::string text = Logme::FormatByteSize(1024 * 1024);

Public functions:

bool Logme::ParseByteSize(const std::string& input, uint64_t& value);
uint64_t Logme::GetByteSize(const std::string& input, uint64_t def);
std::string Logme::FormatByteSize(uint64_t value);

When logme is built with USE_JSONCPP, the JSON wrapper is also available:

uint64_t Logme::GetByteSize(
  const Json::Value& root
  , const char* option
  , uint64_t def
);

Accepted byte-size suffixes are case-insensitive because the parser normalizes input to lowercase:

Suffixes Multiplier
b 1
Kb, Kib, K 1024
Mb, Mib, M 1024 * 1024
Gb, Gib, G 1024 * 1024 * 1024

A value without a suffix is interpreted as bytes.

FormatByteSize() uses the largest exact canonical suffix. It does not round values.

Logme::FormatByteSize(0);                    // "0b"
Logme::FormatByteSize(1024);                 // "1Kb"
Logme::FormatByteSize(1024 * 1024);          // "1Mb"
Logme::FormatByteSize(1024ULL * 1024 * 1024);// "1Gb"
Logme::FormatByteSize(1536);                 // "1536b"

Time interval helpers

Interval helpers parse and format time values in milliseconds. They are used by JSON configuration and runtime control commands for options such as timeout and watchdog periodicity.

uint64_t ms = 0;

if (Logme::ParseInterval("30s", ms))
{
  // ms == 30000
}

uint64_t timeout = Logme::GetInterval("5min", 0);
std::string text = Logme::FormatInterval(60 * 1000);

Public functions:

bool Logme::ParseInterval(const std::string& input, uint64_t& value);
uint64_t Logme::GetInterval(const std::string& input, uint64_t def);
std::string Logme::FormatInterval(uint64_t value);

When logme is built with USE_JSONCPP, the JSON wrapper is also available:

uint64_t Logme::GetInterval(
  const Json::Value& root
  , const char* option
  , uint64_t def
);

Accepted interval suffixes are case-insensitive:

Suffixes Multiplier
ms, millisecond, milliseconds 1
s, sec, second, seconds 1000
m, min, minute, minutes 60 * 1000
h, hour, hours 60 * 60 * 1000
d, day, days 24 * 60 * 60 * 1000
w, week, weeks 7 * 24 * 60 * 60 * 1000

A value without a suffix is interpreted as milliseconds.

FormatInterval() uses the largest exact canonical suffix. It does not round values.

Logme::FormatInterval(0);                    // "0ms"
Logme::FormatInterval(1000);                 // "1s"
Logme::FormatInterval(60 * 1000);            // "1min"
Logme::FormatInterval(60 * 60 * 1000);       // "1h"
Logme::FormatInterval(1500);                 // "1500ms"

Binary dump and integer helpers

std::string dump = Logme::DumpBuffer(data, size, 0, 8);
std::string number = Logme::dword2str(1234);
  • DumpBuffer(const void* buffer, size_t n, size_t offs, size_t lineLimit = 8) formats binary data as hex plus printable text. offs adds leading spaces to each dump line. lineLimit == 0 disables the practical line limit.
  • dword2str(uint32_t value) converts a 32-bit unsigned value to a decimal string.
  • PrintIntJeaiii(char* buffer, size_t size, int value) writes an integer into a caller-provided character buffer and returns the number of printed characters or -1 on failure.

See also DumpBuffer example.


Process and thread helpers

uint32_t pid = Logme::GetCurrentProcessId();
uint64_t tid = Logme::GetCurrentThreadId();

Available helpers:

  • GetCurrentProcessId() — returns the current process id
  • GetCurrentThreadId() — returns the current thread id
  • SetRenameThreadCallback(PRENAMETHREAD p) — installs a callback used by RenameThread()
  • RenameThread(uint64_t threadID, const char* threadName) — calls the configured rename callback

These helpers are intentionally small wrappers used by logme internals and by applications that want consistent process/thread ids in logging-related code.


Level name helpers

std::string name = Logme::GetLevelName(Logme::Level::INFO);

int value = 0;
if (Logme::LevelFromName("warn", value))
{
  // value contains the parsed log level
}
  • GetLevelName(Level level) returns a textual level name.
  • LevelFromName(const std::string& n, int& v) parses a textual level name.

String conversion helpers

Declared in Logme/Convert.h:

std::string narrow = Logme::ToStdString(L"message");
std::wstring wide = Logme::ToStdWString("message");

Available overloads:

std::string Logme::ToStdString(const std::string& src);
std::string Logme::ToStdString(const char* src);
std::string Logme::ToStdString(const std::wstring& src);
std::string Logme::ToStdString(const wchar_t* src);

std::wstring Logme::ToStdWString(const std::string& src);
std::wstring Logme::ToStdWString(const char* src);
std::wstring Logme::ToStdWString(const std::wstring& src);
std::wstring Logme::ToStdWString(const wchar_t* src);

Procedure argument helpers

Declared in Logme/ArgumentList.h:

int count = 10;
std::string name = "worker";

LogmePV(ARGS2(count, name));
LogmePV(ARGS(NAMED(count), name));

Helpers:

  • NAMED(x) — formats an argument as name=value
  • ARGS(...) — formats a variadic argument list
  • ARGS1(arg1) ... ARGS5(arg1, ..., arg5) — convenience macros that automatically wrap arguments with NAMED()

ARGS(...) returns const char* from a temporary std::string, so it is intended to be consumed immediately by a logme macro in the same full expression.

See also Function tracing.

Clone this wiki locally