# HG changeset patch # User thomas_-_s # Date 1246312263 0 # Node ID 8a8e96d6aad4d0a90ad6ec0f9b68ad590b35a55e # Parent 4f83900d6d121299dfaebf6730da4a9e4d22bd7c Prepared the logger class to be able to log early messages from the settingspath class. TODO: Implement a message queue until the logging file init is fully completed. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@971 59b1889a-e5ac-428c-b0c7-476e01d41282 diff -r 4f83900d6d12 -r 8a8e96d6aad4 src/UiGuiLogger.cpp --- a/src/UiGuiLogger.cpp Mon Jun 29 21:49:39 2009 +0000 +++ b/src/UiGuiLogger.cpp Mon Jun 29 21:51:03 2009 +0000 @@ -46,42 +46,11 @@ */ UiGuiLogger::UiGuiLogger() : QDialog() { setupUi(this); +#ifdef _DEBUG + verboseLevel = QtDebugMsg; +#else verboseLevel = QtWarningMsg; - - // On different systems it may be that "QDir::tempPath()" ends with a "/" or not. So check this. - // Remove any trailing slashes. - QString tempPath = QFileInfo( SettingsPaths::getTempPath() ).absolutePath(); - while ( tempPath.right(1) == "/" ) { - tempPath.chop(1); - } - - // To make the temporary log file invulnerable against file symbolic link hacks - // append the current date and time up to milliseconds to its name and a random character. - QString logFileName = "UiGUI_log_" + QDateTime::currentDateTime().toString("yyyyMMdd"); - logFileName += "-" + QDateTime::currentDateTime().toString("hhmmsszzz"); - // By random decide whether to append a number or an upper or lower case character. - qsrand( time(NULL) ); - unsigned char randomChar; - switch ( qrand() % 3 ) { - // Append a number from 0 to 9. - case 0: - randomChar = qrand() % 10 + '0'; - break; - // Append a upper case characer between A and Z. - case 1: - randomChar = qrand() % 26 + 'A'; - break; - // Append a lower case characer between a and z. - default: - randomChar = qrand() % 26 + 'a'; - break; - } - logFileName += "_" + QString(randomChar) + ".html"; - - logFile.setFileName( tempPath + "/" + logFileName ); - - // Set the tooltip of the open log file folder button to show the unique name of the log file. - openLogFileFolderToolButton->setToolTip( openLogFileFolderToolButton->toolTip() + " (" + logFileName + ")" ); +#endif connect( openLogFileFolderToolButton, SIGNAL(clicked()), this, SLOT(openLogFileFolder()) ); @@ -134,11 +103,7 @@ instance->logTextEdit->append( message ); // Write/append the log message to the log file. - if ( instance->logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append) ) { - QTextStream out(&instance->logFile); - out << message << "\n"; - instance->logFile.close(); - } + instance->writeToLogFile( message ); // In case of a fatal error abort the application. if ( type == QtFatalMsg ) @@ -177,3 +142,56 @@ void UiGuiLogger::openLogFileFolder() { QDesktopServices::openUrl( QFileInfo( logFile ).absolutePath() ); } + + +/*! + \brief Writes the \a message to the used log file. + */ +void UiGuiLogger::writeToLogFile(const QString message) { + + QString testname = logFile.fileName(); + if ( logFile.fileName().isEmpty() ) { + // On different systems it may be that "QDir::tempPath()" ends with a "/" or not. So check this. + // Remove any trailing slashes. + QString tempPath = QFileInfo( SettingsPaths::getTempPath() ).absolutePath(); + while ( tempPath.right(1) == "/" ) { + tempPath.chop(1); + } + + // To make the temporary log file invulnerable against file symbolic link hacks + // append the current date and time up to milliseconds to its name and a random character. + QString logFileName = "UiGUI_log_" + QDateTime::currentDateTime().toString("yyyyMMdd"); + logFileName += "-" + QDateTime::currentDateTime().toString("hhmmsszzz"); + // By random decide whether to append a number or an upper or lower case character. + qsrand( time(NULL) ); + unsigned char randomChar; + switch ( qrand() % 3 ) { + // Append a number from 0 to 9. + case 0: + randomChar = qrand() % 10 + '0'; + break; + // Append a upper case characer between A and Z. + case 1: + randomChar = qrand() % 26 + 'A'; + break; + // Append a lower case characer between a and z. + default: + randomChar = qrand() % 26 + 'a'; + break; + } + logFileName += "_" + QString(randomChar) + ".html"; + + logFile.setFileName( tempPath + "/" + logFileName ); + + // Set the tooltip of the open log file folder button to show the unique name of the log file. + openLogFileFolderToolButton->setToolTip( openLogFileFolderToolButton->toolTip() + " (" + logFileName + ")" ); + + } + + // Write/append the log message to the log file. + if ( logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append) ) { + QTextStream out(&logFile); + out << message << "\n"; + logFile.close(); + } +} diff -r 4f83900d6d12 -r 8a8e96d6aad4 src/UiGuiLogger.h --- a/src/UiGuiLogger.h Mon Jun 29 21:49:39 2009 +0000 +++ b/src/UiGuiLogger.h Mon Jun 29 21:51:03 2009 +0000 @@ -51,6 +51,7 @@ private: UiGuiLogger(); + void writeToLogFile(const QString message); static UiGuiLogger* instance; QtMsgType verboseLevel;