log.ts 1.39 KB
Newer Older
1
import { Message, MessageCode } from "./message";
2

Mathias Chouet's avatar
Mathias Chouet committed
3
// tslint:disable-next-line:class-name
4
export class cLog {
5
    private _messages: Message[] = [];
6

Mathias Chouet's avatar
Mathias Chouet committed
7
8
9
10
11
12
13
    /**
     * efface tous les messages
     */
    public clear() {
        this._messages = [];
    }

14
15
16
    /**
     * insère un message en fin de liste
     */
17
    public add(m: Message) {
18
19
20
        this._messages.push(m);
    }

21
22
23
    /**
     * insère un log entier en fin de liste
     */
24
    public addLog(l: cLog) {
Mathias Chouet's avatar
Mathias Chouet committed
25
        for (const m of l.messages) {
26
            this._messages.push(m);
27
        }
28
29
30
    }

    /**
Mathias Chouet's avatar
Mathias Chouet committed
31
     * insère un message en début de liste
32
     */
33
    public insert(m: Message) {
Mathias Chouet's avatar
Mathias Chouet committed
34
        this._messages.unshift(m);
35
36
    }

37
38
39
40
41
42
43
44
45
    /**
     * insère un log entier en début de liste
     */
    public insertLog(l: cLog) {
        for (let i = l.messages.length - 1; i >= 0; i--) {
            this._messages.unshift(l.messages[i]);
        }
    }

46
47
48
    public get messages() {
        return this._messages;
    }
49
50

    public toString(): string {
Mathias Chouet's avatar
Mathias Chouet committed
51
        return this._messages.join("\n");
52
    }
53
54
55
56
57
58
59
60
61
62
63
64
65

    /**
     * @param mc message code you're looking for
     * @returns true if log contains at least one occurrence of given message code
     */
    public contains(mc: MessageCode): boolean {
        for (const m of this.messages) {
            if (m.code === mc) {
                return true;
            }
        }
        return false;
    }
66
}