Chinaunix

标题: 如何得到当前运行函数的函数名? [打印本页]

作者: 郁闷饿    时间: 2013-11-19 13:35
标题: 如何得到当前运行函数的函数名?
如题,想写一个通用的宏来得到当前运行的函数名方便写一些debug信息和log,比如
printf("%s \n", __FUNCTION__) 这样就可以把当前运行的函数打印出来
有没有什么好办法?
作者: 郁闷饿    时间: 2013-11-19 13:36
补充一下,是linux系统下哈~~
作者: yulihua49    时间: 2013-11-19 13:46
本帖最后由 yulihua49 于 2013-11-19 13:46 编辑
郁闷饿 发表于 2013-11-19 13:35
如题,想写一个通用的宏来得到当前运行的函数名方便写一些debug信息和log,比如
printf("%s \n", __FUNCTI ...

不就是这个吗,你还要什么?

  1. ShowLog(1,"%s:open %s err=%d,%s",__FUNCTION__,filename,errno,strerror(errno));
复制代码

作者: 郁闷饿    时间: 2013-11-19 13:50
yulihua49 发表于 2013-11-19 13:46
不就是这个吗,你还要什么?

啊啊啊啊,试了一下直接就可以用啊。。多谢啦
话说糗大啦
作者: linux_c_py_php    时间: 2013-11-20 10:55
一定要用宏,否则是不可能获得调用点的行号和函数名的。
作者: pppStar    时间: 2013-11-20 11:48


#ifndef BOOST_CURRENT_FUNCTION_HPP_INCLUDED
#define BOOST_CURRENT_FUNCTION_HPP_INCLUDED

// MS compatible compilers support #pragma once

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

//
//  boost/current_function.hpp - BOOST_CURRENT_FUNCTION
//
//  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
//  http://www.boost.org/libs/utility/current_function.html
//

namespace boost
{

namespace detail
{

inline void current_function_helper()
{

#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)

# define BOOST_CURRENT_FUNCTION __PRETTY_FUNCTION__

#elif defined(__DMC__) && (__DMC__ >= 0x810)

# define BOOST_CURRENT_FUNCTION __PRETTY_FUNCTION__

#elif defined(__FUNCSIG__)

# define BOOST_CURRENT_FUNCTION __FUNCSIG__

#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500))

# define BOOST_CURRENT_FUNCTION __FUNCTION__

#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)

# define BOOST_CURRENT_FUNCTION __FUNC__

#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)

# define BOOST_CURRENT_FUNCTION __func__

#else

# define BOOST_CURRENT_FUNCTION "(unknown)"

#endif

}

} // namespace detail

} // namespace boost

#endif // #ifndef BOOST_CURRENT_FUNCTION_HPP_INCLUDED



__FUNCTION__没见过:wink:




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2