summaryrefslogtreecommitdiff
path: root/application/agent/src/LogManager.cpp
blob: ad53c0a06f842883e71f9f96e97b12bb073276ec (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
#ifndef _LogManager_cpp
#define _LogManager_cpp

#include "LogManager.h"
#include <iostream>

#include "syslog.h"

using namespace std;

LogManager* LogManager::singleton = 0;

LogManager::LogManager()
{
    list.size = 0;
    list.beginning = 0;
    list.nextIndex = 0;
}

LogManager* LogManager::instance()
{
    if (singleton == 0)
        singleton = new LogManager();

    return singleton;
}

LogManager::~LogManager()
{
    if (!singleton) {
        delete singleton;
    }
}

int LogManager::storeLog(const char* info)
{
    unsigned int position;

    (list.beginning + list.size)>=SIZE_LOG?(position = list.beginning + list.size - SIZE_LOG):(position = list.beginning + list.size);

    strcpy(list.containers[position].information, info);
    list.containers[position].index = list.nextIndex;

    list.nextIndex<MAX_INDEX?list.nextIndex++:list.nextIndex=0;

    if (list.size<SIZE_LOG)
        list.size++;
    else
        if (list.beginning>=SIZE_LOG-1)
            list.beginning = 0;
        else
            list.beginning++;

    return 0;
}

int LogManager::getLogs(logContainer* fill_me[SIZE_LOG], int* size)
{
    *size = list.size;

    unsigned int position = list.beginning;

    for (int i = 0; i < list.size; i++)
    {
        if (position + i >= SIZE_LOG)
            fill_me[i] = &list.containers[position + i - SIZE_LOG];
        else
            fill_me[i] = &list.containers[position + i];
    }

    return 0;
}

int LogManager::emptyLog()
{
    list.size = 0;
    list.beginning = 0;
    list.nextIndex = 0;

    return 0;
}

#endif