不少用户提到《定时执行专家》没有任务执行的日志,最近有些时间增加了日志输出的功能。大致的需求如下:

(图1-1,定时执行专家 - 日志输出文件)
wxWidgets类库里面实现日志输入需要使用wxLog相关的日志处理Class。这里大致整理一下本次实现的思路。
首先,在主窗口的构造函数里,初始化wxLog。
- //
- // logging handling - leo 220724
- //
- wxDateTime dtNow = wxDateTime::Now();
- m_nCurrWDay = dtNow.GetWeekDay();
- wxString strFileName = wxString::Format(_T("log_%d.txt"), m_nCurrWDay); // sun:0, mon:1...
- wxString strFullPath = wxGetApp().GetAppPath() + _T("\\") + strFileName;
-
- wxFileName wfn(strFullPath);
- wxFileOffset nOffSet = 0;
- wxString strMode = _T("at+");
- if(wfn.FileExists())
- {
- wxFFile wxf;
- if(wxf.Open(strFullPath))
- {
- nOffSet = wxf.Length();
- wxf.Close();
-
- if(nOffSet > N_LOG_FILE_MAX_SIZE)
- strMode = _T("w+");
- }
- }
-
- //
- if (m_pLogFile == NULL)
- {
- m_pLogFile = fopen(strFullPath, strMode);
- delete wxLog::SetActiveTarget(new wxLogStderr(m_pLogFile));
- wxLog::SetTimestamp(STR_LOG_TIMESTAMP);
- }
-
- // logging timer
- m_timerLogging.SetOwner(this, ID_TIMER_LOGGING);
- m_timerLogging.Start(N_LOG_TIMER_MILLISECONDS); // 1000*60*5 = 5 minutes
其次,在主窗口类里面添加 wxTimer 成员变量,实现 wxTimer 的触发函数。
- // logging handling
- void TimingExecuterFrame::Onm_timerLogging(wxTimerEvent& event)
- {
- wxLogMessage(wxString::Format(_T("*** On logging handling check timer trigger, every %zd ms ***"), N_LOG_TIMER_MILLISECONDS));
-
- // 1 - check curr wday whether changed
- // 2 - if changed, open new file and check size whether over 2MB
- // 3 - clear old log and write new log
- wxDateTime::WeekDay nWDay = wxDateTime::Now().GetWeekDay();
- if(nWDay != m_nCurrWDay)
- {
- m_nCurrWDay = nWDay;
- wxString strFileName = wxString::Format(_T("log_%d.txt"), m_nCurrWDay); // sun:0, mon:1...
- wxLogMessage(_T("*** Log file will change to: ") + strFileName + _T(" ***"));
-
- wxString strFullPath = wxGetApp().GetAppPath() + _T("\\") + strFileName;
- wxFileName wfn(strFullPath);
- wxFileOffset nOffSet = 0;
- wxString strMode = _T("at+");
- if(wfn.FileExists())
- {
- wxFFile wxf;
- if(wxf.Open(strFullPath))
- {
- nOffSet = wxf.Length();
- wxf.Close();
-
- if(nOffSet > N_LOG_FILE_MAX_SIZE)
- {
- strMode = _T("w+");
- wxLogMessage(wxString::Format(_T("### Log file size over: %zd bytes ###"), N_LOG_FILE_MAX_SIZE));
- }
- }
- }
-
- wxLogMessage(wxString::Format(_T("### %s's size: %zd bytes ###"), strFileName, nOffSet));
-
- //
- delete wxLog::SetActiveTarget(NULL);
- if(m_pLogFile != NULL)
- fclose(m_pLogFile);
-
- m_pLogFile = fopen(strFullPath, strMode);
- delete wxLog::SetActiveTarget(new wxLogStderr(m_pLogFile));
- wxLog::SetTimestamp(STR_LOG_TIMESTAMP);
-
- wxLogMessage(_T(">>> Log file changed to: ") + strFileName + _T(" >>>"));
- }
- }
最后,在主窗口析构函数里,关闭wxLog的输出。
- //
- // logging handling - leo 220724
- //
- m_timerLogging.Stop();
-
- wxLogMessage(_T("<<< TimingExecutor End <<<"));
-
- delete wxLog::SetActiveTarget(NULL);
- if(m_pLogFile != NULL)
- fclose(m_pLogFile);
【关键字/Keyword】
PC定时执行专家,定时执行专家,自动定时任务执行,自动定时执行任务,自动执行任务,定时执行任务,定时任务执行工具,定时任务执行,任务定时执行,定时任务系统,定时任务管理,计划任务管理,任务计划程序,定时关机,自动关机,自动关机软件,关机软件,定时任务,定时任务软件,任务管理,自动截屏,自动屏幕截图,屏幕截图,无察觉截屏,隐身执行,毫秒级精度,毫秒级定时执行,毫秒级执行,秒级定时执行,秒级执行,定时任务每秒执行,定时任务每分钟执行,定时任务每小时执行,定时任务每天执行,定时任务每周执行,定时任务每月执行,定时任务每年执行,Cron定时执行,定时任务重复执行,超级网搜,超级网际搜索,全网搜索,代码统计分析工具,boomworks,boomwxsoft