4

我试图将一个简单的错误记录合并到我现有的应用程序中,目前它报告错误只是使用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;
}
4

5 回答 5

10
myLogClass << "Line No: " << __LINE__ ...

使用您的operator <<链接将不起作用,因为它返回一个bool.

bool myLogClass::operator << (const char * input)

通常将流插入定义如下:

std::ostream& myLogClass::operator << (std::ostream& o, const char * input) {
    // do something
    return o;
}

做这个:

#define log(o, s) o << "Line No: " << __LINE__ << \
                   " Function: " << __FUNCTION__ << \
                   " Error: " << s // note I leave ; out

此外,您可以将宏包装在一个do-while循环中:

#define log(o, s) do { o << "Line No: " << __LINE__ << \
                   " Function: " << __FUNCTION__ << \
                   " Error: " << s; \ 
                  } while(0) // here, I leave ; out

然后就可以愉快的写了:

 myLogClass myLogger; // do this

 // use it
log(myLogger, "This is my error to be logged"); // note the ;
于 2009-03-07T17:03:20.143 回答
2

在 ANSI C(我假设它也应该在 C++ 中工作)中,您可以使用可变参数函数和预处理器宏来做到这一点。请参见下面的示例:

#include <stdio.h>
#include <stdarg.h>

#define MAXMSIZE 256
#define MyDebug(...) MyInternalDebug(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)

void MyInternalDebug( char *file, char *function, const int line, const char *format, ... )
{
    char message[MAXMSIZE];
    // Variable argument list (VA)
    va_list ap;
    // Initialize VA
    // args : Name of the last named parameter in the function definition.
    // The arguments extracted by subsequent calls to va_arg are those after 'args'.
    va_start(ap, format);

    // Composes a string with the same text that would be printed if 'format' was used on printf,
    // but using the elements in the variable argument list identified by 'ap' instead of
    // additional function arguments and storing the resulting content as a C string in the buffer pointed by 'message'.
    // * The state of arg is likely to be altered by the call.
    vsprintf(message, format, ap);
    // Custom print function
    printf("%s\t%s\t%d\t%s\n",file, function, line, message);

    // Finzalize use of VA
    va_end(ap);
}

int main ()
{  
    MyInternalDebug(__FILE__, __FUNCTION__, __LINE__, "An error occured with message = '%s'", "Stack Overflow");
    MyDebug("Another error occured with code = %d", 666);

    return 0;
}
于 2015-04-13T11:37:47.217 回答
1

不,这就是使用宏完成日志记录的原因。 __LINE__需要由有问题的行上的预处理器进行扩展,而不是在常见的日志记录功能中。

于 2009-03-07T16:59:05.770 回答
0

正如 Adam Mitz 提到的,您需要在呼叫位置使用 __LINE__。
我建议您添加一个额外的参数,如“additionalInfo”,并创建一个宏,该宏将使用 __LINE__ 和 __FUNCTION__ 生成此“additionalInfo”。

于 2009-03-07T17:03:16.617 回答
0

我无法在第一个答案中获得代码进行编译。我使用这个简单的宏来很好地完成任务:

#define qlog(s) std::cerr << __FUNCTION__ << "::" << __LINE__ << "\t" << s << endl

于 2010-02-09T18:55:36.600 回答