Monday, February 15, 2010

Pantheios 1.0.1 beta 194 released: new pantheios::slice inserter

The first update of Pantheios in six months, 1.0.1 (beta 194), has just been released. It contains various mods and fixes, but the main changes are:
  • addition of the new pantheios::slice inserter
  • widestring compatibility (of source, not yet of makefile build targets)
The slice inserter is for situations such as the following:

void fn(char const* str, size_t len)
{
  . . . 

In such a case, you cannot just pass str to a log statement, because that assumes it will be nul-terminated, and the semantics of fn() are clearly that it's not. In most circumstances you will get a benign overrun. But that is not guaranteed, and it's possible to get an access violation by overrunning into an invalid area of address space. (Imperfect C++ discusses ways in which this can happen.)

So, instead, use the new pantheios::slice inserter:


// assumes inclusion of pantheios/pan.hpp
// assumes inclusion of pantheios/inserter/slice.hpp
 
void fn(char const* str, size_t len)
{
  pan::log_DEBUG("fn(", pan::slice(str, len), ")");
  . . . 


Now only the requisite slice will be included.


But there's more to it than just that. I often use the convention of naming parameters when logging function entry, as in:



void fn2(std::string const& path1, std::string const& path2)
{
  pan::log_DEBUG(path1=", path1, ", path2=", path2, ")");
  . . . 


Obviously this is done explicitly, with names contained in the literal strings that are used to separate the function parameters in such cases. The question with pantheios::slice, then, is how we would achieve the same?


To accomodate this, pantheios::slice has several overloads that facilitate naming. Assuming str is "abcdefghijklm" and len is 5, consider the following examples:



void fn(char const* str, size_t len)
{
  // gives "fn(abcde)"
  pan::log_DEBUG("fn(", pan::slice(str, len), ")");

  // gives "fn(str=abcde, len=5)"
  pan::log_DEBUG("fn(", pan::slice(str, len, "str", "len"), ")");

  // gives "fn(s;abcde;l:5)"
  pan::log_DEBUG("fn(", pan::slice(str, len, "s", "l", ":", ";"), ")");

  . . .

No comments:

Post a Comment