#include "activityLog.h" ActivityLog::ActivityLog() { // Create the new instances of the logs. normalLog = new QPlainTextEdit(); cmdLineLog = new QPlainTextEdit(); debugLog = new QPlainTextEdit(); clientLog = new QPlainTextEdit(); // Create the tab widget and add logs to it. logTabs = new QTabWidget(); logTabs->addTab( normalLog, "Normal" ); logTabs->addTab( clientLog, "Client" ); logTabs->addTab( debugLog, "Debug" ); logTabs->addTab( cmdLineLog, "Command Line" ); // Create the layout and add the widget to the layout. layout = new QHBoxLayout; layout->addWidget( logTabs ); // Generate the file names. QString time = timeStamp(); QString path = "logs/"; QString nFile = path; nFile.append( time ); nFile.append( "_Normal_Log_" ); QString fileNum; fileNum.setNum( 1 ); nFile.append( fileNum ); QString cLFile = path; cLFile.append( time ); cLFile.append( "_Command_Line_Log_" ); cLFile.append( fileNum ); QString dFile = path; dFile.append( time ); dFile.append( "_Debug_Log_" ); dFile.append( fileNum ); QString cFile = path; cFile.append( time ); cFile.append( "_Client_Data_Log_" ); cFile.append( fileNum ); // Create the new instances of the files and open them. normalFile = new QFile( nFile ); normalFile->open( QFile::WriteOnly | QIODevice::Truncate ); cmdLineFile = new QFile( cLFile ); cmdLineFile->open( QFile::WriteOnly | QIODevice::Truncate ); debugFile = new QFile( dFile ); debugFile->open( QFile::WriteOnly | QIODevice::Truncate ); clientFile = new QFile( cFile ); clientFile->open( QFile::WriteOnly | QIODevice::Truncate ); // Initialize the file size and number counters. normalSize = 0; normalNum = 1; clientSize = 0; clientNum = 1; debugSize = 0; debugNum = 1; cmdLineSize = 0; cmdLineNum = 1; } ActivityLog::~ActivityLog() { // Delete the pointers cleanly. delete logTabs; logTabs = NULL; delete normalLog; normalLog = NULL; delete cmdLineLog; cmdLineLog = NULL; delete debugLog; debugLog = NULL; delete clientLog; clientLog = NULL; normalFile->close(); delete normalFile; normalFile = NULL; cmdLineFile->close(); delete cmdLineFile; cmdLineFile = NULL; debugFile->close(); delete debugFile; debugFile = NULL; clientFile->close(); delete clientFile; clientFile = NULL; } void ActivityLog::write( QString cLine, int lType ) { QString cTime = getCurrTime(); // Get the current system time in a QString. // Write cTime and cLine to the correct log and file. switch( lType ) { case NORMAL : writeToLog( cLine, cTime, normalLog ); writeToFile( cLine, cTime, normalFile ); normalSize++; break; case DEBUG : writeToLog( cLine, cTime, debugLog ); writeToFile( cLine, cTime, debugFile ); debugSize++; break; case CLIENT : writeToLog( cLine, cTime, clientLog ); writeToFile( cLine, cTime, clientFile ); clientSize++; break; case CMD : writeToLog( cLine, cTime, cmdLineLog ); writeToFile( cLine, cTime, cmdLineFile ); cmdLineSize++; break; default : writeToLog( cLine, cTime, normalLog ); writeToFile( cLine, cTime, normalFile ); normalSize++; break; } // Check the size of each file. checkFileSizes(); } void ActivityLog::writeToLog( QString textLine, QString timeLine, QPlainTextEdit* log ) { // Write time and text to the log. //log->appendPlainText( timeLine ); log->appendPlainText( textLine ); } void ActivityLog::writeToFile( QString textLine, QString timeLine, QFile* file ) { // Write text to the file. QTextStream stream( file ); //stream << timeLine; //stream << "\n"; stream << textLine; stream << "\n"; } void ActivityLog::checkFileSizes() { // Check if the size is greater than the max allowed size. // If so, close the current file and create a new file. if( normalSize >= MAXLOGFILESIZE ) { normalFile->close(); delete normalFile; normalFile = NULL; QString fName = timeStamp(); fName.append( "_Normal_Log_" ); normalNum++; QString num; num.setNum( normalNum ); fName.append( num ); normalFile = new QFile( fName ); normalFile->open( QFile::WriteOnly | QFile::Truncate ); } else if( debugSize >= MAXLOGFILESIZE ) { debugFile->close(); delete debugFile; debugFile = NULL; QString fName = timeStamp(); fName.append( "_Debug_Log_" ); debugNum++; QString num; num.setNum( debugNum ); fName.append( num ); debugFile = new QFile( fName ); debugFile->open( QFile::WriteOnly | QFile::Truncate ); } else if( clientSize >= MAXLOGFILESIZE ) { clientFile->close(); delete clientFile; clientFile = NULL; QString fName = timeStamp(); fName.append( "_Client_Data_Log_" ); clientNum++; QString num; num.setNum( clientNum ); fName.append( num ); clientFile = new QFile( fName ); clientFile->open( QFile::WriteOnly | QFile::Truncate ); } else if( cmdLineSize >= MAXLOGFILESIZE ) { cmdLineFile->close(); delete cmdLineFile; cmdLineFile = NULL; QString fName = timeStamp(); fName.append( "_Command_Line_Log_" ); cmdLineNum++; QString num; num.setNum( normalNum ); fName.append( num ); cmdLineFile = new QFile( fName ); cmdLineFile->open( QFile::WriteOnly | QFile::Truncate ); } } QString ActivityLog::getCurrTime() { time_t rawtime; struct tm* timeInfo; char buffer[80]; time( &rawtime ); timeInfo = localtime( &rawtime ); strftime( buffer, 80, "%Y%m%d_%H%M%S", timeInfo ); strftime( buffer, 80, "%H:%M:%S", timeInfo ); QString time = buffer; return time; } QPlainTextEdit* ActivityLog::getLogPointer( int logType ) { switch( logType ) { case NORMAL : return normalLog; case DEBUG : return debugLog; case CLIENT : return clientLog; case CMD : return cmdLineLog; default : return normalLog; break; } } QGroupBox* ActivityLog::getGroupBox( QString title ) { // Create the group box, title it, and set the layout. logBox = new QGroupBox( tr( title.toStdString().c_str() ) ); logBox->setLayout( layout ); return logBox; } QString ActivityLog::timeStamp() { time_t rawtime; struct tm* timeInfo; char buffer[80]; time( &rawtime ); timeInfo = localtime( &rawtime ); strftime( buffer, 80, "%Y%m%d_%H%M%S", timeInfo ); QString output = buffer; return output; }