如何只打印函数和类名而不返回类型? [英] How to print only function and class name without return type?
问题描述
25-01-17 10:37:34 [info]:| b.cpp | | 7 | void __cdecl A :: Func(void)调用Func
25-01-17 10:37:34 [info]:| C.cpp | | 6 | void __cdecl A :: Func1(void)Func1 Start
我需要这样的输出:
25-01-17 10:37: 34 [info]:| C.cpp | | 6 | A :: Func1 Func1开始
.cpp
---
你能帮帮我吗?
< pre lang =c ++> #include simpleLogger.h
namespace logging = boost :: log;
namespace sinks = boost :: log :: sinks;
namespace src = boost :: log :: sources;
namespace expr = boost :: log :: expressions;
namespace attrs = boost :: log :: attributes;
namespace keywords = boost :: log :: keywords;
// 定义全局记录器初始化例程
BOOST_LOG_GLOBAL_LOGGER_INIT( my_logger,logger_t)
{
logger_t lg;
logging :: add_common_attributes();
logging :: add_file_log(
keywords :: file_name = SYS_LOGFILE,
keywords :: rotation_size = 1024 * < span class =code-digit> 1024 * 20 , // 兆字节
keywords :: time_based_rotation = boost :: log :: sinks :: file :: rotation_at_time_point( 0 , 0 , 0 ),
keywords :: auto_flush = true ,
keywords :: format =(
expr :: stream<< expr :: format_date_time< boost :: posix_time :: ptime>( TimeStamp, %d-%m-%y%H:%M:%S)
<< [<< expr :: attr< boost :: log :: trivial :: severity_level&g吨; ( 严重性)<< ]:
<< expr :: smessage));
logging :: add_console_log(
std :: cout,
keywords :: format =(
expr :: stream<< |<< expr :: format_date_time< boost :: posix_time :: ptime>( TimeStamp, %d-%m-%y%H:%M:%S)<< |
<< [<< ; expr :: attr< boost :: log :: trivial :: severity_level>( 严重性 )<< ]:
<< expr :: smessage) );
logging :: core :: get() - > set_filter
(
logging :: trivial :: severity> = logging :: trivial :: info
);
return lg;
}
我的尝试:
#ifndef simpleLogger_h__
#define simpleLogger_h__
#include < crtdefs.h >
# include < boost / log / expressions.hpp >
#include < boost / log / expressions / formatters / named_scope.hpp >
#include < boost / log / sources / global_logger_storage.hpp >
#include < boost / log / support / date_time.hpp >
#include < boost / log / trivial.hpp >
#include < boost / log / utility / setup.hpp >
#include < string.h >
#include < boost / log / sinks / debug_output_backend。 hpp >
#define FILE(strrchr(__ FILE __,'/') )? strrchr(__ FILE __,'/')+ 1:strrchr(__ FILE __,'\\')? strrchr(__ FILE __,'\\')+ 1:__ FILE__)
#define INFO BOOST_LOG_SEV(my_logger :: get(),boost :: log :: trivial :: info)<< | << FILE<< |<<<<|<< __LINE__<< |<< __ FUNCSIG__
#define WARN BOOST_LOG_SEV(my_logger :: get(),boost :: log :: trivial: :警告)<< | << FILE<< |<<<<|<< __LINE__<< |<< __ FUNCSIG__
#define ERROR BOOST_LOG_SEV(my_logger :: get(),boost :: log :: trivial :: error)< ;< | << FILE<< |<<<<|<< __LINE__<< |<< __ FUNCSIG__
#define SYS_LOGFILEC:\\Users \\janardhanreddyn \\ Documents \\LogFile\\Logfile.log
// Nar-char线程安全记录器。
typedef boost :: log :: sources :: severity_logger_mt< boost :: log ::琐碎:: severity_level> logger_t;
// 声明一个带有自定义初始化的全局记录器
BOOST_LOG_GLOBAL_LOGGER(my_logger,logger_t)
class A
{
public :
void Func();
void Func1();
};
#endif
如果你只想要平原函数名称使用__ FUNCTION __
而不是__ FUNCSIG __
。
请参阅预定义宏 [ ^ ]用于定义的函数名称宏和Microsoft编译器[/ EDIT]。
与其他编译器一起检查编译器文档中是否有函数名称的预定义宏。
大多数应该提供__ func __
宏但你必须检查打印的内容。
[/ EDIT]
25-01-17 10:37:34 [info]: |b.cpp| |7| void __cdecl A::Func(void) Calling Func
25-01-17 10:37:34 [info]: |C.cpp| |6| void __cdecl A::Func1(void) Func1 Start
I need ouput like this:
25-01-17 10:37:34 [info]: |C.cpp| |6| A::Func1 Func1 Start
.cpp
---
can you please help me?
#include "simpleLogger.h"
namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
{
logger_t lg;
logging::add_common_attributes();
logging::add_file_log(
keywords::file_name = SYS_LOGFILE,
keywords::rotation_size = 1024 * 1024 * 20, // megabytes
keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point (0, 0, 0),
keywords::auto_flush = true,
keywords::format = (
expr::stream << expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%d-%m-%y %H:%M:%S")
<< " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
<< expr::smessage) );
logging::add_console_log(
std::cout,
keywords::format = (
expr::stream <<"|"<< expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%d-%m-%y %H:%M:%S")<<"|"
<< " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
<< expr::smessage ));
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
return lg;
}
What I have tried:
#ifndef simpleLogger_h__
#define simpleLogger_h__
#include <crtdefs.h>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/formatters/named_scope.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#include <string.h>
#include <boost/log/sinks/debug_output_backend.hpp>
#define FILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << "|" << FILE<< "| " <<" "<<"|"<< __LINE__ << "| "<<__FUNCSIG__
#define WARN BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning) << "|" << FILE<< "| " <<" "<<"|"<< __LINE__ << "| "<<__FUNCSIG__
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error) << "|" << FILE<< "| " <<" "<<"|"<< __LINE__ << "| "<<__FUNCSIG__
#define SYS_LOGFILE "C:\\Users\\janardhanreddyn\\Documents\\LogFile\\Logfile.log"
//Narrow-char thread-safe logger.
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;
//declares a global logger with a custom initialization
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)
class A
{
public:
void Func();
void Func1();
};
#endif
If you only want the plain function name use__FUNCTION__
instead of__FUNCSIG__
.
See Predefined Macros[^] for the defined function name macros [EDIT] with the Microsoft compiler[/EDIT].
[EDIT]
With other compilers check the compiler documentation for predefined macros for function names.
Most should provide the__func__
macro but you have to check what is printed.
[/EDIT]
这篇关于如何只打印函数和类名而不返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!