libcsdbg  1.28
C++ exception (and generic) stack trace debug library
string * csdbg::filebuf::unique_id ( const i8 fmt)
static

Create a unique ID based on process identifiers arranged as indicated by a printf-style format string.

Parameters
[in]fmtthe format string
Returns
the ID (heap allocated)
Exceptions
std::bad_alloc
csdbg::exception
Note
The ID can be used to name files in an unambiguous way. The specifiers are:

  • %e - executable name
  • %a - executable absolute path
  • %p - process ID
  • %t - thread ID
  • %s - timestamp (in microseconds)

Attention
If fmt is NULL or an empty string then the default format %e_%p_%t_%s is used. All numeric values are hexadecimal

Definition at line 355 of file filebuf.cpp.

References __D_ASSERT, csdbg::string::append(), csdbg::util::exec_path(), likely, path(), csdbg::string::string(), and unlikely.

356 {
357  __D_ASSERT(fmt != NULL);
358  if ( unlikely(fmt == NULL || strlen(fmt) == 0) )
359  fmt = "%e_%p_%t_%s";
360 
361  struct timeval now;
362  gettimeofday(&now, NULL);
363  u64 tstamp = static_cast<u64> (now.tv_sec) * 10e+5 + now.tv_usec;
364 
365  const i8 *path = util::exec_path();
366  string *retval = NULL;
367 
368  /* If an exception occurs, release resources and rethrow it */
369  try {
370  retval = new string();
371 
372  for (u32 i = 0, len = strlen(fmt); likely(i < len); i++) {
373  i8 ch = fmt[i];
374  if ( likely(ch != '%') ) {
375  retval->append(ch);
376  continue;
377  }
378 
379  if ( unlikely(i == len - 1) )
380  throw exception("invalid format '%s' (at %d: no specifier)", fmt, ++i);
381 
382  ch = fmt[++i];
383  switch (ch) {
384  case '%':
385  retval->append(ch);
386  break;
387 
388  case 'a':
389  retval->append(path);
390  break;
391 
392  case 'e':
393  retval->append(basename(path));
394  break;
395 
396  case 'p':
397  retval->append("%x", getpid());
398  break;
399 
400  case 't':
401  retval->append("%lx", pthread_self());
402  break;
403 
404  case 's':
405  retval->append("%lx", tstamp);
406  break;
407 
408  default: {
409  const i8 err[] = "invalid format '%s' (at %d: unknown specifier '%c')";
410  throw exception(err, fmt, i, ch);
411  }}
412  }
413 
414  delete[] path;
415  return retval;
416  }
417 
418  catch (...) {
419  delete[] path;
420  delete retval;
421  throw;
422  }
423 }
static const i8 * exec_path()
Get the absolute path of the executable.
Definition: util.cpp:87
char i8
8-bit signed integer
Definition: config.hpp:72
unsigned long long u64
64-bit unsigned integer
Definition: config.hpp:107
string(u32=0)
Object constructor.
Definition: string.cpp:127
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
virtual const i8 * path() const
Get the output file path.
Definition: filebuf.cpp:96
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268

+ Here is the call graph for this function: