Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.38 KB)

1
//==========================================================================
2
// nederror.h  -
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
#ifndef __NEDERROR_H
18
#define __NEDERROR_H
19

    
20
#include <vector>
21
#include <exception>
22
#include <stdexcept>
23
#include "nedxmldefs.h"
24

    
25
NAMESPACE_BEGIN
26

    
27
class NEDElement;
28

    
29
// Note: this cannot be inner type because of swig wrapping
30
enum NEDErrorSeverity {NED_SEVERITY_INFO, NED_SEVERITY_WARNING, NED_SEVERITY_ERROR};
31

    
32
/**
33
 * Stores error messages
34
 */
35
class NEDXML_API NEDErrorStore
36
{
37
    private:
38
        struct Entry {
39
            NEDElement *context;
40
            int severity;
41
            std::string location;
42
            std::string message;
43
        };
44

    
45
        std::vector<Entry> entries;
46
        bool doprint;
47

    
48
     private:
49
        void doAdd(NEDElement *context, const char *loc, int severity, const char *message);
50

    
51
     public:
52
        /**
53
         * Ctor
54
         */
55
        NEDErrorStore() {doprint = false;}
56
        ~NEDErrorStore() {}
57

    
58
        /**
59
         * If set, errors get dumped to stderr as well as stored
60
         */
61
        void setPrintToStderr(bool p) {doprint = p;}
62

    
63
        /**
64
         * Add an error message with the severity ERROR.
65
         */
66
        void addError(NEDElement *context, const char *messagefmt, ...);
67

    
68
        /**
69
         * Add an error message with the severity ERROR.
70
         */
71
        void addError(const char *location, const char *messagefmt, ...);
72

    
73
        /**
74
         * Add an error message with the severity WARNING.
75
         */
76
        void addWarning(NEDElement *context, const char *messagefmt, ...);
77

    
78
        /**
79
         * Add an error message with the severity WARNING.
80
         */
81
        void addWarning(const char *location, const char *messagefmt, ...);
82

    
83
        /**
84
         * Add an error message.
85
         */
86
        void add(NEDElement *context, int severity, const char *messagefmt, ...);
87

    
88
        /**
89
         * Add an error message.
90
         */
91
        void add(const char *location, int severity, const char *messagefmt, ...);
92

    
93
        /**
94
         * Return true if there are no messages stored.
95
         */
96
        bool empty() const {return entries.empty();}
97

    
98
        /**
99
         * Total number of error, warning and info messages.
100
         */
101
        int numMessages() const {return entries.size();}
102

    
103
        /**
104
         * Returns true if there is an error or fatal error stored.
105
         */
106
        bool containsError() const;
107

    
108
        /**
109
         * Discard all messages stored.
110
         */
111
        void clear() {entries.clear();}
112

    
113
        /** @name Returns properties of the ith message stored (i=0..numMessages-1) */
114
        //@{
115
        const char *errorSeverity(int i) const;
116
        int errorSeverityCode(int i) const;
117
        const char *errorLocation(int i) const;
118
        NEDElement *errorContext(int i) const;
119
        const char *errorText(int i) const;
120
        //@}
121

    
122
        /**
123
         * Return the first message with index >= startIndex whose
124
         * context is the given node. Returns -1 if none found.
125
         */
126
        int findFirstErrorFor(NEDElement *node, int startIndex) const;
127

    
128
        /**
129
         * Convert severities from numeric to textual form.
130
         */
131
        static const char *severityName(int severity);
132
};
133

    
134

    
135
#define INTERNAL_ERROR0(context,msg) NEDInternalError(__FILE__,__LINE__,context,msg)
136
#define INTERNAL_ERROR1(context,msg,arg1) NEDInternalError(__FILE__,__LINE__,context,msg,arg1)
137
#define INTERNAL_ERROR2(context,msg,arg1,arg2)   NEDInternalError(__FILE__,__LINE__,context,msg,arg1,arg2)
138
#define INTERNAL_ERROR3(context,msg,arg1,arg2,arg3) NEDInternalError(__FILE__,__LINE__,context,msg,arg1,arg2,arg3)
139

    
140
/**
141
 * Called when an internal error occurs. It prints an appropriate message, then calls abort().
142
 * This method is typically used via the INTERNAL_ERROR0()...INTERNAL_ERROR4() macros that
143
 * add the __FILE__, __LINE__ args implicitly.
144
 */
145
void NEDInternalError(const char *file, int line, NEDElement *context, const char *messagefmt, ...);
146

    
147

    
148
NAMESPACE_END
149

    
150

    
151
#endif
152