IOutputStream.cc 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// (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.

#include "Base/Security/Mandatory.hh"
#include "IOutputStream.hh"
#include "Base/Code/CodeLink.hh"
#include "Base/Utils/ThrowError.hh"

#include <boost/filesystem.hpp>
#include <string>

namespace Base {

21
IOutputStream& operator<<(IOutputStream& _os, const int _i)
22
{
23
  return _os.print(_i);
24 25
}

26
IOutputStream& operator<<(IOutputStream& _os, const double _d)
27
{
28
  return _os.print(_d);
29 30
}

31
IOutputStream& operator<<(IOutputStream& _os, const char* const _s)
32
{
33
  return _os.print(_s);
34 35
}

36
IOutputStream& operator<<(IOutputStream& _os, const char _c)
37
{
38
  return _os.print(_c);
39 40
}

41
IOutputStream& operator<<(IOutputStream& _os, const size_t _i)
42
{
43
  return _os.print(_i);
44 45
}

46
IOutputStream& operator<<(IOutputStream& _os, const unsigned int _i)
47
{
48
  return _os.print(size_t(_i));
49 50
}

51
IOutputStream& operator<<(IOutputStream& _os, const float _f)
52
{
53
  return _os.print((double)_f);
54 55
}

56
IOutputStream& operator<<(IOutputStream& _os, const std::string& _s)
57
{
58
  return _os.print(_s.c_str());
59 60
}

61
IOutputStream& operator<<(IOutputStream& _os, const Command& _co)
62
{
63
  return _os.print(_co);
64 65
}

66 67 68 69 70 71
IOutputStream& operator<<(IOutputStream& _os, const Command::Type _cmd_type)
{
  return _os.print(Command(_cmd_type));
}


72
IOutputStream& operator<<(IOutputStream& _os, const boost::filesystem::path& _path)
73
{
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  return _os << '\"' << _path.string().c_str() << '\"';
}

IOutputStream& filter_function_name(IOutputStream& _os, const char* _fnct)
{
  // The goal is to filter unstable text from the function name, e.g., lambdas.
  // MSVC Lambda function name: <lambda_136f4d101172d40b57aea5f0078ce711>
  // Multiple lambdas could be in the same function?
  // TODO: this naming pattern is compiler/platform dependent
  // gcc lambda name is <lambdaX>

  const char lmbd[] = "<lambda";
  const char* fnct = _fnct;
  for(;;)
  {
    const char* lmbd_pos = strstr(fnct, lmbd);
    if (lmbd_pos == nullptr)
    {// print the rest of the function name
      _os << fnct;
      break;
    }
    // print everything until here (char by char, not optimal, but easy)
    for (; fnct != lmbd_pos; ++fnct)
      _os << *fnct;
    _os << lmbd; // now print lambda
    // and skip until > or end of string
    for (; *fnct != '>' && *fnct != '\0'; ++fnct)
      ;
  } 
  return _os;
}

IOutputStream& operator<<(IOutputStream& _os, IOutputStream& /*_os*/)
{
  return _os;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

IOutputStream& operator<<(IOutputStream& _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;

125
  return _os << "@ [" << filter_function_name(_os, _lnk.fnct) << "() in " 
126 127 128 129 130 131 132 133
    << flnm_pntr << ":" << _lnk.line << "]";
}

}//namespace Base

#ifdef DEB_ON
namespace Debug {

134
Base::IOutputStream& operator<<(Base::IOutputStream& _os, 
135 136
  const ThrowInfo& _thrw_info)
{
137
  return _os << "thrown in " << _thrw_info.modl << " " << _thrw_info.lnk;
138 139 140 141
}

}//namespace Debug
#endif//DEB_ON