我需要将输出格式更改为wxWidgets应用程序中的日志文件。例如,现在日志文件的输出如下所示:
10:13:19: there is wxLogDebug message
我想添加日期和日志级别:
05:06:2022-10:13:19 [INFO] : there is wxLogDebug message
我知道应该使用wxLogFormatter,但我不明白具体如何使用。
例如,如果您创建最简单的应用程序:
// Application Class
class MyGUI : public wxApp {
public:
virtual bool OnInit();
private:
// method, where I will configure logging
void makeLOG();
};
// Frame Class
class MyGuiFrame : public wxFrame {
public:
MyGuiFrame(const wxString& title = wxString(wxT("wxApplication")));
};
wxIMPLEMENT_APP(MyGUI);
bool MyGUI::OnInit() {
makeLOG(); // <- here I want to configure logging
MyGuiFrame* GuiFrame = new MyGuiFrame();
GuiFrame->Show(true);
return true;
}
MyGuiFrame::MyGuiFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800, 600)) {
wxLogDebug(wxLOG_Warning, "there is wxLogDebug message");
wxLogMessage(wxLOG_Warning, "there is wxLogMessage");
}
类MyGUI有一个makeLOG()方法,我想在其中配置日志记录。我应该如何使用wxLogFormatter更改日志格式?
您需要从wxLogFormatter
派生您的自定义格式化器类:
class MyFormatter : public wxLogFormatter {
public:
wxString Format(wxLogLevel level,
const wxString& msg,
const wxLogRecordInfo& info) const override {
... do whatever you want here ...
}
};
然后你需要告诉wxLog
使用它:
wxLog::GetActiveTarget()->SetFormatter(new MyFormatter);
和wxWidgets API一样,库获得堆分配指针的所有权,也就是说,你不需要删除格式化器。