You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.7 KiB
150 lines
3.7 KiB
15 years ago
|
|
||
|
#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;
|
||
|
}
|
||
|
}
|
||
|
|