CodeLink.hh 1.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// (C) Copyright 2016 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk,  Inc.,  and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of  this information  by  anyone  other  than  authorized
// employees of Autodesk, Inc.  is granted  only under a written
// non-disclosure agreement,  expressly  prescribing  the  scope
// and manner of such use.

#ifndef BASE_CODELINK_HH_INCLUDED
#define BASE_CODELINK_HH_INCLUDED

#include <string.h>

namespace Base {

//! Convenient wrapper around a preprocessor source code link
struct CodeLink
{
  CodeLink(
    const char* _fnct,
    const char* _file,
    const int _line
    ) 
    : fnct(_fnct), file(_file), line(_line) 
  {}
  
  const char* fnct;
  const char* file;
  int line;
};

// TODO: Replace StreamT w. IOutputStream, move to a .cc file, hide <string.h>!
template <class StreamT>
StreamT& operator<<(StreamT& _os, const CodeLink& _lnk)
{
#ifdef WIN32
  const char path_sep = '\\';
#else//!WIN32
  const char path_sep = '/';
#endif//WIN32
  
  auto flnm_pntr = strrchr(_lnk.file, path_sep); 
  if (flnm_pntr == nullptr) 
    flnm_pntr = _lnk.file;
  else
    ++flnm_pntr;

  return _os << " @ [" << _lnk.fnct << "() in " 
    << flnm_pntr << ":" << _lnk.line << "]";
}

}//namespace Base

#define BASE_CODELINK Base::CodeLink(__FUNCTION__, __FILE__, __LINE__)

#endif//BASE_CODELINK_HH_INCLUDED