summaryrefslogtreecommitdiffstats
path: root/src/logger.cpp
blob: 90491eb1ad4d2545a96222c691ec4b91ac12674a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

#include "logger.h"

#include <tdelocale.h>
#include <kstandarddirs.h>

#include <cstdlib>
#include <ctime>


LoggerItem::LoggerItem()
{}

LoggerItem::~LoggerItem()
{}


Logger::Logger()
{
    TQValueList<LoggerItem*>::Iterator item = processes.append( new LoggerItem() );
    (*item)->filename = "soundKonverter";
    (*item)->id = 1000;
    (*item)->completed = true;
    (*item)->state = 1;
    (*item)->file.setName( locateLocal("data",TQString("soundkonverter/log/%1.log").arg((*item)->id)) );
    // TODO error handling
    (*item)->file.open( IO_WriteOnly );
    (*item)->textStream.setDevice( TQT_TQIODEVICE(&((*item)->file)) );
    srand( (unsigned)time(NULL) );
}

Logger::~Logger()
{}

void Logger::cleanUp()
{
    for( TQValueList<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 TQString& filename )
{
    // NOTE ok, it works now, but why prepend() and not append()?
    TQValueList<LoggerItem*>::Iterator item = processes.append( new LoggerItem() );
    (*item)->filename = filename;
    (*item)->id = getNewID();
    (*item)->completed = false;
    (*item)->file.setName( locateLocal("data",TQString("soundkonverter/log/%1.log").arg((*item)->id)) );
    // TODO error handling
    (*item)->file.open( IO_WriteOnly );
    (*item)->textStream.setDevice( TQT_TQIODEVICE(&((*item)->file)) );

    emit updateProcess( (*item)->id );

    return (*item)->id;
}

void Logger::log( int id, const TQString& data )
{
    for( TQValueList<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( TQValueList<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( TQValueList<LoggerItem*>::Iterator it = processes.begin(); it != processes.end(); ++it ) {
        if( (*it)->id == id ) return *it;
    }

    return 0;
}

TQValueList<LoggerItem*> Logger::getLogs()
{
/*    TQValueList<LoggerItem*> items;

    for( TQValueList<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;
    TQTime time = TQTime::currentTime();
    bool remove = false;

    for( TQValueList<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;
    }
}