【问题标题】:Macro to obtain current namespace and function name (but not full signature)?获取当前命名空间和函数名(但不是完整签名)的宏?
【发布时间】:2012-02-03 06:59:25
【问题描述】:

是否有获取当前命名空间和函数名的 C++ 宏?示例:

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

将打印foo::bar::baz。我知道__FUNCTION__,但它没有给出命名空间。而BOOST_CURRENT_FUNCTION 给出了整个签名,包括。参数和返回类型:

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

也许,是否可以编写一个从BOOST_CURRENT_FUNCTION 中提取命名空间和函数名的宏?

我希望它用于记录目的,以获得类似的记录字符串

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

【问题讨论】:

  • 我认为没有任何独立于平台的方式来获取函数的完整签名。但是,为什么不能使用 __FILE____LINE__ 的组合呢?它会给你几乎相同的效果;此外,与搜索函数相比,从日志中找到代码的速度更快!
  • @iammilind:其实我想用宏内容作为log4cxx中的logger名字。这些记录器名称是分层的,您可以为不同的名称空间设置不同的日志级别,等等。所以我真正要做的是log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("foo.bar.baz")); LOG4CXX_INFO(logger, "blah logging message blah"); MACRO 应该填写foo.bar.baz 部分。我不想在我的帖子中让它变得那么复杂。

标签: c++ boost macros


【解决方案1】:

据我所知,这是不可能的(不可移植)。但是,您可以从完整签名中提取这些参数。当然它需要解析所说的签名......这并不容易:x

这是我目前使用的功能:

// 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

及其伴随的测试:

#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

它与 gcc 的 __func__ 宏结合使用。

StringRef 类类似于llvm::StringRef

如果您负担得起额外的解析,我认为它应该可以满足您的需求。它非常快:没有回溯和动态分配,所以应该不是问题(尤其是与写入文件相比......)。

希望对你有帮助。

【讨论】:

  • 谢谢!我会尽量让它编译。 consumeType 是如何定义的?
  • @Frank:我不太明白这个问题......它的定义是在答案中写的?
  • @MatthieuM。 __func__ 似乎已标准化为不包含全名和签名,您现在需要使用 GCC 的 __PRETTY_FUNCTION__。 C++20 有/将有一个非宏解决方案std::source_location::function_name
猜你喜欢
  • 1970-01-01
  • 2011-03-24
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多