错误记录 C++ 预处理器宏 __LINE__、__FUNCTION__
我试图将一个简单的错误日志合并到我现有的应用程序中,目前它仅使用 cout
报告错误,因此我希望使用 <<
保持类似的界面> 运算符。 不过,我希望它记录发生错误的行和函数,但我不想每次需要记录时都必须键入 __LINE__, __FUNCTION__ 。 有谁知道我可以使用一个技巧来允许在另一个函数中使用 __LINE__ 宏,而不是报告调用行? 希望这是有道理的。
class myLogClass {
uint8_t level;
public:
bool operator<<( const char * input );
};
bool myLogClass::operator<<( const char * input ) {
logItInSQL( input );
return true;
}
而不是每次
myLogClass << "Line No: " << __LINE__
<< " Function: " << __FUNCTION__
<< " Error: " << "This is my error to be logged";
我都希望能够这样做:
myLogClass << "This is my error to be logged";
bool myLogClass::operator<<( const char * input ) {
logItInSQL( " Line No: __LINE__" );
logItInSQL( " Function: __FUNCTION__" );
logItInSQL( " Error: " + input );
return true;
}
I trying to incorporate a simple error logging into my existing app, at the moment it reports errors just using cout
so I was hoping to keep a similar interface using the <<
operator. However I want it to log the line and function the error occurred, but I don't want to have to type __LINE__, __FUNCTION__
every time I need to log. Does anyone know a trick I can use to allow the __LINE__
macro to be used inside another function, reporting the calling line instead? Hope that makes sense.
class myLogClass {
uint8_t level;
public:
bool operator<<( const char * input );
};
bool myLogClass::operator<<( const char * input ) {
logItInSQL( input );
return true;
}
Instead of this every time
myLogClass << "Line No: " << __LINE__
<< " Function: " << __FUNCTION__
<< " Error: " << "This is my error to be logged";
I would like to just be able to do:
myLogClass << "This is my error to be logged";
bool myLogClass::operator<<( const char * input ) {
logItInSQL( " Line No: __LINE__" );
logItInSQL( " Function: __FUNCTION__" );
logItInSQL( " Error: " + input );
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于您的
运算符<<
,链接将不起作用,因为它返回一个bool
。通常按如下方式定义流插入:
这样做:
另外,您可以将宏包装在
do-while
循环中:然后您可以愉快地编写:
With your
operator <<
chaining will not work since it returns abool
.It is customary to define stream insertion as follows:
Do this:
Additionally, you can wrap the macro in a
do-while
loop:Then you can happily write:
在 ANSI C 中(我认为它也应该在 C++ 中工作),您可以使用可变参数函数和预处理器宏来做到这一点。 请参阅下面的示例:
In ANSI C (which should also work in C++ I assume), you can do that using variadic functions and preprocessor macros. See example below:
不,这就是使用宏完成日志记录的原因。
__LINE__
需要由预处理器在相关行上进行扩展,而不是在常见的日志记录函数中进行扩展。No, this is why logging is done with macros.
__LINE__
needs to be expanded by the preprocessor on the line in question, not in a common logging function.正如 Adam Mitz 提到的,您需要在调用位置使用 __LINE__ 。
我建议您添加一个额外的参数,例如“additionalInfo”,并创建一个宏,该宏将使用 __LINE__ 和 __FUNCTION__ 生成此“additionalInfo”。
As Adam Mitz mentioned you need to use __LINE__ in the place of call.
I recommend to you to add an extra parameter like "additionalInfo" and create a macro that will generate this "additionalInfo" using __LINE__ and __FUNCTION__.
我无法在第一个答案中获取要编译的代码。 我使用这个简单的宏很好地完成了任务:
#define qlog(s) std::cerr << __函数__ << “::”<< __LINE__ << “\t”<< s << 结束
I could not get the code in the first answer to compile. I use this simple macro which accomplishes the task well:
#define qlog(s) std::cerr << __FUNCTION__ << "::" << __LINE__ << "\t" << s << endl