diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:09:31 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:09:31 +0000 |
commit | f2cfda2a54780868dfe0af7bd652fcd4906547da (patch) | |
tree | c6ac23545528f5701818424f2af5f79ce3665e6c /src/logger.cpp | |
download | soundkonverter-f2cfda2a54780868dfe0af7bd652fcd4906547da.tar.gz soundkonverter-f2cfda2a54780868dfe0af7bd652fcd4906547da.zip |
Added KDE3 version of SoundKonverter
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/soundkonverter@1097614 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/logger.cpp')
-rwxr-xr-x | src/logger.cpp | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100755 index 0000000..7b19fff --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,149 @@ + +#include "logger.h" + +#include <klocale.h> +#include <kstandarddirs.h> + +#include <cstdlib> +#include <ctime> + + +LoggerItem::LoggerItem() +{} + +LoggerItem::~LoggerItem() +{} + + +Logger::Logger() +{ + QValueList<LoggerItem*>::Iterator item = processes.append( new LoggerItem() ); + (*item)->filename = "soundKonverter"; + (*item)->id = 1000; + (*item)->completed = true; + (*item)->state = 1; + (*item)->file.setName( locateLocal("data",QString("soundkonverter/log/%1.log").arg((*item)->id)) ); + // TODO error handling + (*item)->file.open( IO_WriteOnly ); + (*item)->textStream.setDevice( &((*item)->file) ); + srand( (unsigned)time(NULL) ); +} + +Logger::~Logger() +{} + +void Logger::cleanUp() +{ + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + if( (*it)->id != 1000 ) { + emit removedProcess( (*it)->id ); + (*it)->file.close(); + (*it)->file.remove(); + delete *it; + } + } + processes.clear(); +} + +int Logger::registerProcess( const QString& filename ) +{ + // NOTE ok, it works now, but why prepend() and not append()? + QValueList<LoggerItem*>::Iterator item = processes.append( new LoggerItem() ); + (*item)->filename = filename; + (*item)->id = getNewID(); + (*item)->completed = false; + (*item)->file.setName( locateLocal("data",QString("soundkonverter/log/%1.log").arg((*item)->id)) ); + // TODO error handling + (*item)->file.open( IO_WriteOnly ); + (*item)->textStream.setDevice( &((*item)->file) ); + + emit updateProcess( (*item)->id ); + + return (*item)->id; +} + +void Logger::log( int id, const QString& data ) +{ + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + if( (*it)->id == id ) { + (*it)->data.append( data ); + if( (*it)->data.count() > 100000 ) (*it)->data.erase( (*it)->data.at(0) ); + (*it)->textStream << data; + (*it)->textStream << "\n"; + (*it)->file.flush(); + if( id == 1000 ) emit updateProcess( 1000 ); + return; + } + } +} + +int Logger::getNewID() +{ + bool ok; + int id; + + do { + id = rand(); + ok = true; + + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + if( (*it)->id == id ) ok = false; + } + + } while( !ok ); + + return id; +} + +LoggerItem* Logger::getLog( int id ) +{ + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + if( (*it)->id == id ) return *it; + } + + return 0; +} + +QValueList<LoggerItem*> Logger::getLogs() +{ +/* QValueList<LoggerItem*> items; + + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + if( (*it)->completed ) items.append( *it ); + } + + return items;*/ + return processes; +} + +void Logger::processCompleted( int id, int state ) +{ + LoggerItem* item = 0; + QTime time = QTime::currentTime(); + bool remove = false; + + for( QValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) { + // TODO test + if( time >= (*it)->time && (*it)->completed && (*it)->state == 0 ) { + time = (*it)->time; + item = *it; + remove = true; + } + else if( (*it)->id == id ) { + (*it)->state = state; + (*it)->completed = true; + (*it)->time = (*it)->time.currentTime(); + (*it)->textStream << i18n("Finished logging"); + (*it)->file.close(); + emit updateProcess( id ); + } + } + + if( remove && processes.count() > 11 ) { + emit removedProcess( item->id ); + item->file.remove(); + processes.remove( item ); + delete item; + } +} + |