Statistics
| Branch: | Revision:

root / src / nedxml / nederror.cc @ 68da4f12

History | View | Annotate | Download (4.71 KB)

1
//==========================================================================
2
// nederror.cc -
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8

    
9
/*--------------------------------------------------------------*
10
  Copyright (C) 2002-2008 Andras Varga
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 <stdarg.h>
19
#include <stdlib.h>
20
#include "commonutil.h"
21
#include "nederror.h"
22
#include "nedelement.h"
23

    
24
NAMESPACE_BEGIN
25

    
26

    
27
void NEDErrorStore::doAdd(NEDElement *context, const char *loc, int severity, const char *message)
28
{
29
    entries.push_back(Entry());
30
    Entry& e = entries.back();
31

    
32
    if (!loc && context)
33
        loc = context->getSourceLocation();
34

    
35
    e.context = context;
36
    e.location = loc ? loc : "";
37
    e.severity = severity;
38
    e.message = message;
39

    
40
    if (doprint)
41
    {
42
        const char *severitytext = severityName(severity);
43
        if (loc)
44
            fprintf(stderr, "%s: %s: %s\n", loc, severitytext, message);
45
        else if (context)
46
            fprintf(stderr, "<%s>: %s: %s\n", context->getTagName(), severitytext, message);
47
        else
48
            fprintf(stderr, "%s: %s\n", severitytext, message);
49
   }
50
}
51

    
52
#define BUFLEN  1024
53

    
54
void NEDErrorStore::addError(NEDElement *context, const char *messagefmt, ...)
55
{
56
    char message[BUFLEN];
57
    VSNPRINTF(message, BUFLEN, messagefmt);
58
    doAdd(context, NULL, NED_SEVERITY_ERROR, message);
59
}
60

    
61
void NEDErrorStore::addError(const char *location, const char *messagefmt, ...)
62
{
63
    char message[BUFLEN];
64
    VSNPRINTF(message, BUFLEN, messagefmt);
65
    doAdd(NULL, location, NED_SEVERITY_ERROR, message);
66
}
67

    
68
void NEDErrorStore::addWarning(NEDElement *context, const char *messagefmt, ...)
69
{
70
    char message[BUFLEN];
71
    VSNPRINTF(message, BUFLEN, messagefmt);
72
    doAdd(context, NULL, NED_SEVERITY_WARNING, message);
73
}
74

    
75
void NEDErrorStore::addWarning(const char *location, const char *messagefmt, ...)
76
{
77
    char message[BUFLEN];
78
    VSNPRINTF(message, BUFLEN, messagefmt);
79
    doAdd(NULL, location, NED_SEVERITY_WARNING, message);
80
}
81

    
82
void NEDErrorStore::add(NEDElement *context, int severity, const char *messagefmt, ...)
83
{
84
    char message[BUFLEN];
85
    VSNPRINTF(message, BUFLEN, messagefmt);
86
    doAdd(context, NULL, severity, message);
87
}
88

    
89
void NEDErrorStore::add(const char *location, int severity, const char *messagefmt, ...)
90
{
91
    char message[BUFLEN];
92
    VSNPRINTF(message, BUFLEN, messagefmt);
93
    doAdd(NULL, location, severity, message);
94
}
95

    
96
bool NEDErrorStore::containsError() const
97
{
98
    for (int i=0; i<(int)entries.size(); i++)
99
        if (entries[i].severity == NED_SEVERITY_ERROR)
100
            return true;
101
    return false;
102
}
103

    
104
const char *NEDErrorStore::errorSeverity(int i) const
105
{
106
    if (i<0 || i>=(int)entries.size()) return NULL;
107
    return severityName(entries[i].severity);
108
}
109

    
110
int NEDErrorStore::errorSeverityCode(int i) const
111
{
112
    if (i<0 || i>=(int)entries.size()) return -1;
113
    return entries[i].severity;
114
}
115

    
116
const char *NEDErrorStore::errorLocation(int i) const
117
{
118
    if (i<0 || i>=(int)entries.size()) return NULL;
119
    return entries[i].location.c_str();
120
}
121

    
122
NEDElement *NEDErrorStore::errorContext(int i) const
123
{
124
    if (i<0 || i>=(int)entries.size()) return NULL;
125
    return entries[i].context;
126
}
127

    
128
const char *NEDErrorStore::errorText(int i) const
129
{
130
    if (i<0 || i>=(int)entries.size()) return NULL;
131
    return entries[i].message.c_str();
132
}
133

    
134
int NEDErrorStore::findFirstErrorFor(NEDElement *node, int startIndex) const
135
{
136
    for (int i=startIndex; i<(int)entries.size(); i++)
137
        if (entries[i].context==node)
138
            return i;
139
    return -1;
140
}
141

    
142
const char *NEDErrorStore::severityName(int severity)
143
{
144
    switch (severity)
145
    {
146
        case NED_SEVERITY_INFO:    return "Info";
147
        case NED_SEVERITY_WARNING: return "Warning";
148
        case NED_SEVERITY_ERROR:   return "Error";
149
        default:                   return "???";
150
    }
151
}
152

    
153
//---
154

    
155
void NEDInternalError(const char *file, int line, NEDElement *context, const char *messagefmt, ...)
156
{
157
    char message[BUFLEN];
158
    VSNPRINTF(message, BUFLEN, messagefmt);
159

    
160
    const char *loc = context ? context->getSourceLocation() : NULL;
161
    if (loc)
162
        fprintf(stderr, "INTERNAL ERROR: %s:%d: %s: %s\n", file, line, loc, message);
163
    else if (context)
164
        fprintf(stderr, "INTERNAL ERROR: %s:%d: <%s>: %s\n", file, line, context->getTagName(), message);
165
    else
166
        fprintf(stderr, "INTERNAL ERROR: %s:%d: %s\n", file, line, message);
167
    // exit(-1);
168
    //__asm int 3; //FIXME this windows-only
169
}
170

    
171
NAMESPACE_END
172