我尝试将简单的错误记录合并到我现有的应用程序中,目前它仅使用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;
}
有帮助吗?

解决方案

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 ;

其他提示

在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;
}

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

正如Adam Mitz所说,你需要在电话中使用__LINE__ 我建议你添加一个额外的参数,如<!> quot; additionalInfo <!> quot;并创建一个宏来生成这个<!> quot; additionalInfo <!> quot;使用__LINE__和__FUNCTION __。

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top