lurumdare
2019-09-07 3b1577ab7eb2000a3477a0aa489b888fb1256434
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
/*
 * EventRegistry.cpp
 *
 *  Created on: Mar 30, 2014
 *      
 */
 
#include "EventRegistry.h"
#include <cstddef>
#include <string.h>
#include <algorithm>
 
using namespace std;
 
namespace license {
EventRegistry::EventRegistry() {
}
 
EventRegistry& operator<<(EventRegistry& eventRegistry,
        AuditEvent& securityEvent) {
    eventRegistry.logs.push_back(securityEvent);
    return eventRegistry;
}
 
EventRegistry& operator<<(EventRegistry& eventRegistry1,
        EventRegistry& otherRegistry) {
    eventRegistry1.append(otherRegistry);
    return eventRegistry1;
}
 
void EventRegistry::append(const EventRegistry& eventRegistry) {
    logs.insert(logs.end(), eventRegistry.logs.begin(),
            eventRegistry.logs.end());
}
 
void EventRegistry::turnLastEventIntoError() {
    if (logs.size() > 0) {
        logs.back().severity = SVRT_ERROR;
    }
}
 
bool EventRegistry::turnEventIntoError(EVENT_TYPE event) {
    bool eventFound = false;
    for (auto it = logs.begin(); it != logs.end(); ++it) {
        if (it->event_type == event) {
            it->severity = SVRT_ERROR;
            eventFound = true;
        }
    }
    return eventFound;
}
 
AuditEvent const * EventRegistry::getLastFailure() const {
    const AuditEvent* result = NULL;
    if (logs.size() == 0) {
        return result;
    }
    auto it = logs.end();
    do {
        --it;
        if (it->severity == SVRT_ERROR) {
            result = &(*it);
            break;
        }
    } while (it != logs.begin());
    return result;
 
}
 
bool EventRegistry::isGood() const {
    bool isGood = true;
    for (auto it = logs.begin(); it != logs.end(); ++it) {
        if (it->severity == SVRT_ERROR) {
            isGood = false;
            break;
        }
    }
    return isGood;
}
 
void EventRegistry::addError(EVENT_TYPE event) {
    this->addEvent(event, SVRT_ERROR);
}
 
void EventRegistry::addEvent(EVENT_TYPE event, SEVERITY severity) {
    AuditEvent audit;
    audit.severity = severity;
    audit.event_type = event;
    audit.param1[0] = '\0';
    audit.param2[0] = '\0';
    logs.push_back(audit);
}
 
void EventRegistry::addEvent(EVENT_TYPE event, SEVERITY severity,
        const string& eventParameter) {
    AuditEvent audit;
    audit.severity = severity;
    audit.event_type = event;
    strncpy(audit.param1, eventParameter.c_str(), 255);
    audit.param2[0] = '\0';
    logs.push_back(audit);
}
 
bool EventRegistry::turnErrosIntoWarnings() {
    bool eventFound = false;
    for (auto it = logs.begin(); it != logs.end(); ++it) {
        if (it->severity == SVRT_ERROR) {
            it->severity = SVRT_WARN;
            eventFound = true;
        }
    }
    return eventFound;
}
 
void EventRegistry::exportLastEvents(AuditEvent* auditEvents, int nlogs) {
    const int sizeToCopy = min(nlogs, (int) logs.size());
    std::copy(logs.begin(), logs.begin() + sizeToCopy, auditEvents);
}
}