簡體   English   中英

錯誤記錄 C++ 預處理器宏 __LINE__、__FUNCTION__

[英]Error Logging C++ Preprocessor Macros __LINE__, __FUNCTION__

我試圖將一個簡單的錯誤日志記錄合並到我現有的應用程序中,目前它僅使用cout報告錯誤,所以我希望使用<<運算符保持類似的界面。 但是我希望它記錄該行和 function 發生錯誤,但我不想每次需要記錄時都輸入__LINE__, __FUNCTION__ 有誰知道我可以用來允許__LINE__宏在另一個 function 中使用的技巧,而不是報告調用行? 希望這是有道理的。

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)

習慣上將 stream 插入定義如下:

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__需要由有問題的行上的預處理器擴展,而不是在常見的日志記錄 function 中。

我無法在第一個答案中獲得代碼進行編譯。 我使用這個簡單的宏來很好地完成任務:

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

正如 Adam Mitz 提到的,您需要在呼叫位置使用 __LINE__。
我建議您添加一個額外的參數,如“additionalInfo”,並創建一個宏,該宏將使用 __LINE__ 和 __FUNCTION__ 生成此“additionalInfo”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM