Created
July 12, 2013 00:42
-
-
Save SanAndreaP/5980514 to your computer and use it in GitHub Desktop.
A class which allows for using a Rich Text Control Box from the wxWidgets library to be used as logging output. It also supports logging into a file and saving the previous log file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wxRTLogger.hpp" | |
wxRTLogger::wxRTLogger(std::string name, wxRichTextCtrl* wxRTBox) { | |
this->richTextBox = wxRTBox; | |
this->progName = name; | |
this->richTextBox->BeginTextColour(wxColour(0,0,0)); | |
this->richTextBox->WriteText(_(this->progName + "\n\n")); | |
this->richTextBox->EndTextColour(); | |
} | |
wxRTLogger::~wxRTLogger() { | |
this->richTextBox = NULL; | |
this->logFile.close(); | |
} | |
void wxRTLogger::info(std::string msg) { this->log(std::string("[INFO] ") + msg, wxRTLogger::LogLevel::INFO); } | |
void wxRTLogger::notice(std::string msg) { this->log(std::string("[NOTICE] ") + msg, wxRTLogger::LogLevel::NOTICE); } | |
void wxRTLogger::warning(std::string msg) { this->log(std::string("[WARNING] ") + msg, wxRTLogger::LogLevel::WARNING); } | |
void wxRTLogger::severe(std::string msg) { this->log(std::string("[SEVERE] ") + msg, wxRTLogger::LogLevel::SEVERE); } | |
void wxRTLogger::formatLine(wxRichTextCtrl* wxRTBox, wxRTLogger::LogLevel lvl) { | |
std::string box = " "; | |
wxRichTextAttr attr; | |
attr.SetFlags(wxTEXT_ATTR_BACKGROUND_COLOUR); | |
if( lvl == wxRTLogger::LogLevel::INFO ) { | |
wxRTBox->BeginTextColour(wxColour(0,0,0)); | |
} else if( lvl == wxRTLogger::LogLevel::NOTICE ) { | |
wxRTBox->BeginItalic(); | |
wxRTBox->BeginTextColour(wxColour(96,32,192)); | |
} else if( lvl == wxRTLogger::LogLevel::WARNING ) { | |
attr.SetBackgroundColour(wxColour(255,255,128)); | |
wxRTBox->BeginStyle(attr); | |
wxRTBox->BeginTextColour(wxColour(0,0,192)); | |
} else if( lvl == wxRTLogger::LogLevel::SEVERE ) { | |
attr.SetBackgroundColour(wxColour(255,0,0)); | |
wxRTBox->BeginBold(); | |
wxRTBox->BeginItalic(); | |
attr.SetBackgroundColour(wxColour(255,0,0)); | |
wxRTBox->BeginStyle(attr); | |
wxRTBox->BeginTextColour(wxColour(255,255,255)); | |
} | |
} | |
const std::string wxRTLogger::currentDateTime() { | |
time_t now = time(0); | |
struct tm tstruct; | |
char buf[80]; | |
tstruct = *localtime(&now); | |
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tstruct); | |
return std::string(buf); | |
} | |
void wxRTLogger::log(std::string msg, LogLevel lvl) { | |
std::string line = this->currentDateTime() + " " + msg + "\n"; | |
this->richTextBox->SetInsertionPoint(this->richTextBox->GetLastPosition()); | |
this->formatLine(this->richTextBox, lvl); | |
this->richTextBox->WriteText(_(line)); | |
this->richTextBox->EndAllStyles(); | |
if( this->logFile.is_open() ) { | |
this->logFile << line; | |
this->logFile.flush(); | |
} | |
} | |
void wxRTLogger::setLogFile(std::string fileName) { | |
std::ifstream logFile; | |
logFile.open(std::string(fileName + ".txt").c_str()); | |
if( logFile.good() ) { | |
std::ofstream prevLog; | |
prevLog.open(std::string(fileName + ".prev.txt").c_str(), std::ios::out | std::ios::trunc); | |
prevLog << logFile.rdbuf(); | |
prevLog.close(); | |
} | |
logFile.close(); | |
this->logFile.open(std::string(fileName + ".txt").c_str(), std::ios::out | std::ios::trunc); | |
} | |
void wxRTLogger::openLogFile(std::string fileName, wxRichTextCtrl* wxRTBox) { | |
wxRTBox->Clear(); | |
std::ifstream ifile(std::string(fileName + ".txt").c_str()); | |
if( ifile.is_open() ) { | |
while( ifile.good() ) { | |
std::string line; | |
wxColour clr; | |
std::getline(ifile, line); | |
if( line.find_first_of('[') != std::string::npos && line.find_first_of(']') != std::string::npos ) { | |
std::string type(line.substr(line.find_first_of('[')+1, line.find_first_of(']')-line.find_first_of('[')-1)); | |
this->formatLine(wxRTBox, | |
type == "SEVERE" ? wxRTLogger::LogLevel::SEVERE : | |
type == "WARNING" ? wxRTLogger::LogLevel::WARNING : | |
type == "NOTICE" ? wxRTLogger::LogLevel::NOTICE : | |
wxRTLogger::LogLevel::INFO); | |
} | |
wxRTBox->BeginTextColour(clr); | |
wxRTBox->WriteText(_(line + "\n")); | |
wxRTBox->EndAllStyles(); | |
} | |
} | |
ifile.close(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WXRTLOGGER_HPP | |
#define WXRTLOGGER_HPP | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <stdio.h> | |
#include <time.h> | |
#include <wx/richtext/richtextctrl.h> | |
#include <wx/richtext/richtextbuffer.h> | |
class wxRTLogger | |
{ | |
public: | |
wxRTLogger(std::string name, wxRichTextCtrl* wxRTBox); | |
virtual ~wxRTLogger(); | |
virtual void info(std::string msg); | |
virtual void notice(std::string msg); | |
virtual void warning(std::string msg); | |
virtual void severe(std::string msg); | |
virtual void setLogFile(std::string fileName); | |
virtual void openLogFile(std::string fileName, wxRichTextCtrl* wxRTBox); | |
enum LogLevel { INFO, NOTICE, WARNING, SEVERE }; | |
private: | |
wxRichTextCtrl* richTextBox; | |
std::string progName; | |
std::ofstream logFile; | |
virtual void log(std::string msg, LogLevel lvl); | |
virtual const std::string currentDateTime(); | |
virtual void formatLine(wxRichTextCtrl* wxRTBox, wxRTLogger::LogLevel lvl); | |
}; | |
#endif // WXRTLOGGER_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment