Project

General

Profile

Statistics
| Branch: | Revision:

root / src / eventlog / eventlogentry.cc @ a787bc0a

History | View | Annotate | Download (7.18 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  EVENTLOGENTRY.CC - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Levente Meszaros
7
//
8
//=========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#include <stdio.h>
18
#include <errno.h>
19
#include <stdlib.h>
20
#include "eventlog.h"
21
#include "eventlogentry.h"
22
#include "eventlogentryfactory.h"
23
24
NAMESPACE_BEGIN
25
26
char EventLogEntry::buffer[128];
27
LineTokenizer EventLogEntry::tokenizer(32768);
28
static const char *currentLine;
29
static int currentLineLength;
30
31
/***********************************************/
32
33
EventLogEntry::EventLogEntry()
34
{
35
    contextModuleId = -1;
36
    level = -1;
37
    event = NULL;
38
    entryIndex = -1;
39
}
40
41
EventLogEntry *EventLogEntry::parseEntry(Event *event, int entryIndex, char *line, int length)
42
{
43
    currentLine = line;
44
    currentLineLength = length;
45
46
    if (*line == '-')
47
    {
48
        EventLogMessageEntry *eventLogMessage = new EventLogMessageEntry(event, entryIndex);
49
        eventLogMessage->parse(line, length);
50
        return eventLogMessage;
51
    }
52
    else
53
    {
54
        EventLogEntryFactory factory;
55
        tokenizer.tokenize(line, length);
56
        Assert(entryIndex >= 0);
57
        return factory.parseEntry(event, entryIndex, tokenizer.tokens(), tokenizer.numTokens());
58
    }
59
}
60
61
bool EventLogEntry::isMessageSend()
62
{
63
    return dynamic_cast<BeginSendEntry *>(this) != NULL;
64
}
65
66
eventnumber_t EventLogEntry::parseEventNumber(const char *str)
67
{
68
    char *end;
69
    return strtoll(str, &end, 10);
70
}
71
72
simtime_t EventLogEntry::parseSimulationTime(const char *str)
73
{
74
    const char *end;
75
    return BigDecimal::parse(str, end);
76
}
77
78
/***********************************************/
79
80
char *EventLogTokenBasedEntry::getToken(char **tokens, int numTokens, const char *sign, bool mandatory)
81
{
82
    for (int i = 1; i < numTokens; i+= 2)
83
        if (!strcmp(tokens[i], sign))
84
            return tokens[i + 1];
85
86
    if (mandatory)
87
        throw opp_runtime_error("Missing mandatory token %s in line %.*s", sign, currentLineLength, currentLine);
88
89
    return NULL;
90
}
91
92
bool EventLogTokenBasedEntry::getBoolToken(char **tokens, int numTokens, const char *sign, bool mandatory, bool defaultValue)
93
{
94
    int value = getIntToken(tokens, numTokens, sign, mandatory, defaultValue);
95
96
    if (value == 0)
97
        return false;
98
    else if (value == 1)
99
        return true;
100
    else
101
        throw opp_runtime_error("Invalid bool value %d in line %.*s", value, currentLineLength, currentLine);
102
}
103
104
int EventLogTokenBasedEntry::getIntToken(char **tokens, int numTokens, const char *sign, bool mandatory, int defaultValue)
105
{
106
    char *token = getToken(tokens, numTokens, sign, mandatory);
107
108
    if (token) {
109
        errno = 0;
110
        int value = atoi(token);
111
        if (errno)
112
            throw opp_runtime_error("Invalid long value %d in line %.*s", value, currentLineLength, currentLine);
113
        return value;
114
    }
115
    else
116
        return defaultValue;
117
}
118
119
short EventLogTokenBasedEntry::getShortToken(char **tokens, int numTokens, const char *sign, bool mandatory, short defaultValue)
120
{
121
    char *token = getToken(tokens, numTokens, sign, mandatory);
122
123
    if (token) {
124
        errno = 0;
125
        short value = atoi(token);
126
        if (errno)
127
            throw opp_runtime_error("Invalid short value %d in line %.*s", value, currentLineLength, currentLine);
128
        return value;
129
    }
130
    else
131
        return defaultValue;
132
}
133
134
long EventLogTokenBasedEntry::getLongToken(char **tokens, int numTokens, const char *sign, bool mandatory, long defaultValue)
135
{
136
    char *token = getToken(tokens, numTokens, sign, mandatory);
137
138
    if (token) {
139
        errno = 0;
140
        long value = atol(token);
141
        if (errno)
142
            throw opp_runtime_error("Invalid long value %ld in line %.*s", value, currentLineLength, currentLine);
143
        return value;
144
    }
145
    else
146
        return defaultValue;
147
}
148
149
int64 EventLogTokenBasedEntry::getInt64Token(char **tokens, int numTokens, const char *sign, bool mandatory, int64 defaultValue)
150
{
151
    char *token = getToken(tokens, numTokens, sign, mandatory);
152
153
    if (token) {
154
        char *end;
155
        errno = 0;
156
        int64 value = strtoll(token, &end, 10);
157
        if (errno)
158
            throw opp_runtime_error("Invalid int64 value "INT64_PRINTF_FORMAT" in line %.*s", value, currentLineLength, currentLine);
159
        return value;
160
    }
161
    else
162
        return defaultValue;
163
}
164
165
eventnumber_t EventLogTokenBasedEntry::getEventNumberToken(char **tokens, int numTokens, const char *sign, bool mandatory, eventnumber_t defaultValue)
166
{
167
    char *token = getToken(tokens, numTokens, sign, mandatory);
168
169
    if (token) {
170
        char *end;
171
        errno = 0;
172
        int64 value = strtoll(token, &end, 10);
173
        if (errno)
174
            throw opp_runtime_error("Invalid event number value %"EVENTNUMBER_PRINTF_FORMAT"d in line %.*s", value, currentLineLength, currentLine);
175
        return value;
176
    }
177
    else
178
        return defaultValue;
179
}
180
181
simtime_t EventLogTokenBasedEntry::getSimtimeToken(char **tokens, int numTokens, const char *sign, bool mandatory, simtime_t defaultValue)
182
{
183
    char *token = getToken(tokens, numTokens, sign, mandatory);
184
185
    if (token) {
186
        const char *end;
187
        BigDecimal value = BigDecimal::parse(token, end);
188
        if (*end != '\0')
189
            throw opp_runtime_error("Invalid simulation time value %s in line %.*s", value.str().c_str(), currentLineLength, currentLine);
190
        return value;
191
    }
192
    else
193
        return defaultValue;
194
}
195
196
const char *EventLogTokenBasedEntry::getStringToken(char **tokens, int numTokens, const char *sign, bool mandatory, const char *defaultValue)
197
{
198
    char *token = getToken(tokens, numTokens, sign, mandatory);
199
    return token ? eventLogStringPool.get(token) : defaultValue;
200
}
201
202
void EventLogTokenBasedEntry::parse(char *line, int length)
203
{
204
    tokenizer.tokenize(line, length);
205
    parse(tokenizer.tokens(), tokenizer.numTokens());
206
}
207
208
/***********************************************/
209
210
EventLogMessageEntry::EventLogMessageEntry(Event *event, int entryIndex)
211
{
212
    this->event = event;
213
    this->entryIndex = entryIndex;
214
    text = NULL;
215
}
216
217
void EventLogMessageEntry::parse(char *line, int length)
218
{
219
    char *s = line + length - 1;
220
    char ch = '\0';
221
222
    if (length > 0) {
223
        ch = *s;
224
        *s = '\0';
225
    }
226
227
    if (length > 1 && *(s - 1) == '\r')
228
        *(s - 1) = '\0';
229
230
    text = eventLogStringPool.get(line + 2);
231
232
    if (length > 0)
233
        *s = ch;
234
235
    if (length > 1)
236
        *(s - 1) = '\r';
237
}
238
239
void EventLogMessageEntry::print(FILE *fout)
240
{
241
    ::fprintf(fout, "- %s\n", text);
242
}
243
244
const std::vector<const char *> EventLogMessageEntry::getAttributeNames() const
245
{
246
    std::vector<const char *> names;
247
    names.push_back("-");
248
249
    return names;
250
}
251
252
const char *EventLogMessageEntry::getAttribute(const char *name) const
253
{
254
    if (!strcmp(name, "type"))
255
        return "-";
256
    else if (!strcmp(name, "-"))
257
        return text;
258
    else
259
        return NULL;
260
}
261
262
NAMESPACE_END