[英]how to split log messages into file and screen in wxwidgets
When I read the WxWidgets documentation, I get the impression that the developers wrote it just for themselves, just to remember what they did 20 years ago.当我阅读 WxWidgets 文档时,我的印象是开发人员只是为自己编写的,只是为了记住他们 20 年前所做的事情。
Regardless, I figured out how to send log messages to a file:无论如何,我想出了如何将日志消息发送到文件:
wxLog::SetActiveTarget(new wxLogStderr(fopen(logPath + "/wxApp.log", "w + ")));
and also I figured out how to change the format of the log messages:而且我还想出了如何更改日志消息的格式:
wxLog::GetActiveTarget()->SetFormatter(new MyLogger);
But I didn't understand anything else.但我什么都不懂。
So I want to ask my question here.所以我想在这里问我的问题。
I want to make a log for my application.我想为我的应用程序做一个日志。
Moreover, I want:此外,我想要:
How can this be done in Windows and Linux in C++?如何在 C++ 中的 Windows 和 Linux 中做到这一点? It's best to show a code example.
最好显示一个代码示例。
I may be missing something but this seems very simple?我可能遗漏了一些东西,但这似乎很简单?
For example, this could be the simplest possible log target which logs some messages into a wxTextCtrl
and all of them into a wxFFile
.例如,这可能是最简单的日志目标,它将一些消息记录到
wxTextCtrl
中,并将所有消息记录到wxFFile
中。
#include <wx/wx.h>
#include <wx/ffile.h>
class MyLogTarget : public wxLog
{
public:
// textCtrl must have longer lifetime than MyLogTarget
MyLogTarget(wxTextCtrl* textCtrl, const wxString& fileName)
: m_textCtrl(textCtrl), m_file(fileName, "a")
{}
protected:
void DoLogTextAtLevel(wxLogLevel level, const wxString& msg) override
{
// preserve debug logging
if ( level == wxLOG_Debug || level == wxLOG_Trace )
wxLog::DoLogTextAtLevel(level, msg);
if ( level == wxLOG_Info || level == wxLOG_Error )
m_textCtrl->AppendText(msg + "\n");
if ( m_file.IsOpened() )
m_file.Write(msg + "\n");
}
private:
wxTextCtrl* m_textCtrl;
wxFFile m_file;
};
class MyFrame : public wxFrame
{
public:
MyFrame(wxWindow* parent = nullptr) : wxFrame(parent, wxID_ANY, "Test")
{
wxTextCtrl* logCtrl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
wxLog::SetActiveTarget(new MyLogTarget(logCtrl, "log.txt"));
wxLogDebug("Debug test");
wxLogMessage("Message test");
wxLogInfo("Info test");
wxLogError("Error test");
}
~MyFrame()
{
delete wxLog::SetActiveTarget(nullptr);
}
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame())->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);
Please notice that this would have to be extended to be usable in a real application, for example, handle the file encoding / flushing / cleaning / error handling (without getting into the endless logging loop), use the full path for the file (eg, obtained with wxStandardPaths
), use a log chain to preserve (some of?) the default wxWidgets logging...请注意,这必须扩展才能在实际应用程序中使用,例如,处理文件编码/刷新/清理/错误处理(不进入无休止的日志循环),使用文件的完整路径(例如,通过
wxStandardPaths
获得),使用日志链来保存(一些?)默认 wxWidgets 日志记录...
If you want to use separate log targets for logging into a text control and a file, it is still very simple except that you have to chain the log targets, as explained in the docs.如果您想使用单独的日志目标来登录文本控件和文件,它仍然非常简单,除了您必须链接日志目标,如文档中所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.