Unified logging system wxLog

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
tonto
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Oct 29, 2010 12:47 am

Unified logging system wxLog

Postby tonto » Sat Jan 25, 2014 6:18 am

Hey all
I thought this might be of interest. I created a sort of unified logging system with several wxLog classes, wxLogStderr for logfile, wxLogWindow for logwindow, and wxLogGui for interactive responses

I have a github gist for it here https://gist.github.com/tonto/8612301 and a blog post http://searchvoidstar.tumblr.com/post/7 ... -wxwidgets

Setup code

Code: Select all

/**
* @brief create log window, logfile, and integrated gui notifications for wxlog settings
* @param parent
* @return
*/
MyMainFrame::MyMainFrame( wxWindow* parent ):
MainFrame( parent )
{
  wxLog *temp;
  wxFFile *test=new wxFFile("yourLogFile.log","a");
  //create logfile with wxLogStderr. contrary to the name, itll log everything
  temp=new wxLogStderr(test->fp());
  wxLog::SetActiveTarget(temp);
  //create wxLogGui and use wxLogChain it
  temp=new wxLogChain(new wxLogGuiMod());
  wxLog::SetActiveTarget(temp);
  //create logwindow
  //note:no need to usesetactivetarget or wxlogchain on wxLogWindow
  temp=new wxLogWindow(this, wxS("Log messages"),true);
  //set verbose levels globally (these are static functions)
  wxLog::SetLogLevel(wxLOG_Max);
  wxLog::SetVerbose();
}



Modified wxLogGui class to ignore verbose messages

Code: Select all

/*
* @brief subclass of wxLogGui to ignore verbose(wxLOG_INFO) messages
*/
class wxLogGuiMod :public wxLogGui {
protected:
  virtual void DoLogRecord(wxLogLevel level,
    const wxString& msg,
    const wxLogRecordInfo& info);
};
 
/*
* @brief override default log record from wxLogGui to ignore verbose(wxLOG_INFO) messages
* @param
* @return
*/
void wxLogGuiMod::DoLogRecord(wxLogLevel level,
  const wxString& msg,
  const wxLogRecordInfo& info)
{
  if(level==wxLOG_Info ) return;
  else wxLogGui::DoLogRecord(level,msg,info);
}


Test code

Code: Select all

wxLogMessage("Message");//produces messagebox and logs to file/window
wxLogError("Error");//produces error message and logs to file/window
wxLogStatus("Status");//creates message on app status bar and logs to file/window
wxLogVerbose("Verbose");//does not create message (due to wxLogGuiMod) and logs to file/window

Return to “The Code Dump”

Who is online

Users browsing this forum: No registered users and 2 guests