日志类包括了全部程序输出,不仅仅是非交互消息。
对于使用者而言,不必了解wxLog类,仅需知道wxLogXXX()函数就够了。
wxLogXXX()方法与printf()和vprintf()函数语法相同,即第一个参数为格式化字符串,后续参数为输出内容的变量。
相关类如下:wxLog--基类,定义了标准接口
// wxFile.Open() normally complains if file can't be opened, we don't want it
wxLogStderr
wxLogStream
wxLogTextCtrl
wxLogWindow
wxLogGui
wxLogNull
wxLogChain
wxLogPassThrough
wxStreamToTextRedirector
/////wxLogXXX()系列函数\\\\\
- wxLogFatalError:类似于wxLogError,使用abort()函数终止程序,退出码为3;
- wxLogError:适用于错误消息必须显示给用户。默认弹出消息框通知用户。
- wxLogWarning:适用于警告消息,弹出对话框,但不终止程序。
- wxLogMessage:消息输出。默认为消息框,但允许改变。
- wxLogVerbose:细节输出。通常不被启动。
- wxLogStatus:状态消息,进入指定或激活的wxFrame【作为第一个参数】中的状态条【若有的话】
- wxLogSysError:wxWidgets自身用的最多的。
- wxLogDebug:适用于DEBUG输出。仅当_WXDEBUG_定义时起作用
- wxLogTrace:类似于wxLogDebug,仅在调试状态下有效
wxLog的优点:
- 轻便Portability:如在UNIX下printf()仍然有效,但是在WINDOWS下就无效了。可以使用wxLogMessage()作为printf()的替代物。如下代码将cout作为当前输出:
wxLog *logger = new wxLogStream(&cout);
wxLog::SetActiveTarget(logger);
也可以重定向到wxTextCtrl控件【使用wxStreamToTextRedirector类】
- 灵活Flexibility:根据各自的重要性,wxLog函数的输出支持重定向或隐藏
- 全面Completeness:Usually, an error message should be presented to the user when some operation fails. Let's take a quite simple but common case of a file error: suppose that you're writing your data file on disk and there is not enough space. The actual error might have been detected inside wxWidgets code (say, in wxFile::Write), so the calling function doesn't really know the exact reason of the failure, it only knows that the data file couldn't be written to the disk. However, as wxWidgets uses wxLogError() in this situation, the exact error code (and the corresponding error message) will be given to the user together with "high level" message about data file writing error.
日志目标log target类:为wxLog的子类
- 在某个时刻只能激活一个日志目标【使用wxLogXXX()方法】
- 日志对象(目标)为wxLog的某个子类
- 使用SetActiveTarget()方法激活
- 被wxLogXXX()方法自动调用
创建一个新的Log日志目标类
- 继承wxLog类
- 实现DoLog()和DoLogString()方法之一或全部
- DoLog:适用于自行区别消息类型
- DoLogString:适用于使用标准方法,如追加Error,Warning等
预定义日志目标类:
- wxLogStderr:记录消息到FILE*中,使用stderr作为默认目标
- wxLogStream:与wxLogStderr类似,只是使用ostream和cerr替代FILE*HE STDERR
- wxLogGui:wxWidgets应用的标准日志目标
- wxLogWindow:该目标提供一个日志面板,收集全部的消息
- wxLogNull:用于隐藏日志,如
// wxFile.Open() normally complains if file can't be opened, we don't want it
{
wxLogNull logNo;
if ( !file.Open("bar") )
... process error ourselves ...
} // ~wxLogNull called, old log sink restored
wxLogMessage("..."); // ok
日志目标也可以组合:参见wxLogChain和wxLogPassThrough
没有评论:
发表评论