Macro to obtain current namespace and function nam

2020-06-08 13:36发布

Is there a C++ macro that obtains the current namespace and function name? Example:

namespace foo {
  namespace bar {
    void baz(int i, double d) {
      std::cout << MACRO << std::endl;
    }
  }
}

would print foo::bar::baz. I know of __FUNCTION__ but it doesn't give the namespace. And BOOST_CURRENT_FUNCTION gives the whole signature, incl. arguments and return type:

void foo::bar::baz(int, double)

Maybe, is it possible to write a macro that extracts the namespace and function name from BOOST_CURRENT_FUNCTION?

I want that for logging purposes, to get a logging string like

foo::bar::baz -- blah logging message blah

标签: c++ boost macros
1条回答
来,给爷笑一个
2楼-- · 2020-06-08 14:22

As far as I know, it's not possible (not portably). However from the full signature you can extract those arguments. Of course it requires parsing said signature... which is not so easy :x

Here is the function I use at the moment:

// What we want to consume:
//  void
//  signed short
//  unsigned int
//  Test::Bar<T, N>
//
static char const* consumeType(char const* const begin, char const* const end){
  static StringRef const Signed("signed");
  static StringRef const Unsigned("unsigned");

  char const* it = begin;

  if (startsWith(it, Signed)) { it += Signed.size() + 1; }
  else if (startsWith(it, Unsigned)) { it += Unsigned.size() + 1; }

  // jump over the return type
  size_t templateNest = 0;
  while (it != end) {
    if (*it == ' ' and templateNest == 0) { break; }
    if (*it == '<') { ++templateNest; }
    if (*it == '>' and templateNest > 0) { --templateNest; }

    ++it;
  }

  return it;
} // consumeType

//
// \param signature: signature as returned by __func___ on gcc
// \return: full name, included namespace qualifier and class (if any)
//
// void Test::Bar<T, N>::parameterized(U) const
//   [with unsigned int O = 4u, U = Test::Foo,
//    T = Test::Foo, unsigned int N = 3u]
//    -> Test::Bar<T, N>::parameterized
//
StringRef parseFunctionName(StringRef const signature) {
  char const* begin = signature.begin();
  char const* end = signature.end();

  // Jump over the return type
  begin = consumeType(begin, end);
  if (begin == end) { return signature; }

  // skip the space right after the return type
  ++begin;
  if (begin == end) { return signature; }

  // if we encounter a '(' then it means that we return a function,
  // and we once again need to jump over the return type
  if (*begin == '(') {
    begin = consumeType(++begin, end);

    // skip the space
    ++begin;
    if (begin == end) { return signature; }
  }

  // and finally, we got the beginning, and we need to get the end, which is
  // the first opening '('
  char const* e = std::find(begin, end, '(');
  return StringRef(begin, e - begin);
} // parseFunctionName

And its accompagnying tests:

#define UT_FUNCTION_CHECK(Signature_, Name_) \
  UT_CHECK(parseFunctionName(StringRef(Signature_)) == Name_);

void Function() {
  // Regular functions
  UT_FUNCTION_CHECK("int main()", "main")
  UT_FUNCTION_CHECK("int foo(int, double)", "foo")
  UT_FUNCTION_CHECK("unsigned int foo(int, double)", "foo")

  // Templates
  UT_FUNCTION_CHECK("unsigned int Test::Bar<T, N>::print() const"
                    " [with T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::print")
  UT_FUNCTION_CHECK("Test::Bar<T, N> Test::Bar<T, N>::print() const"
                    " [with T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::print")
  UT_FUNCTION_CHECK("void Test::Bar<T, N>::parameterized(U) const"
                    " [with unsigned int O = 4u, U = Test::Foo,"
                    " T = Test::Foo, unsigned int N = 3u]",
                    "Test::Bar<T, N>::parameterized")

  // Functions returning functions
  UT_FUNCTION_CHECK("void (* Test::Foo::func() const)()",
                    "Test::Foo::func")
  UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::method() const)(int)volatile",
                    "Test::Foo::method")
  UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::super())"
                    "(void (Test::Foo::*)(int)volatile)const",
                    "Test::Foo::super")
  } // Function

It works in combination with gcc's __func__ macro.

The StringRef class is similar to llvm::StringRef.

I think it should cover your needs if you can afford the extra parsing. It's quite fast: no backtracking and no dynamic allocation, so should not be an issue (especially compared to writing to a file...).

Hope it helps.

查看更多
登录 后发表回答