Commit | Line | Data |
---|---|---|
970ed795 EL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2014 Ericsson Telecom AB | |
3 | // All rights reserved. This program and the accompanying materials | |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #ifndef _Common_Error_HH | |
9 | #define _Common_Error_HH | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <string.h> | |
13 | #include <stdarg.h> | |
14 | ||
15 | #include "error.h" | |
16 | ||
17 | namespace Common { | |
18 | ||
19 | class Location; | |
20 | ||
21 | /** | |
22 | * Class Error_Context. :) | |
23 | */ | |
24 | class Error_Context { | |
25 | private: | |
26 | /** Counts errors, warnings. */ | |
27 | static unsigned int error_count, warning_count; | |
28 | /** Indicates whether the actual chain was printed. */ | |
29 | static bool chain_printed; | |
30 | /** The head and tail of the actual chain. */ | |
31 | static Error_Context *head, *tail; | |
32 | ||
33 | /** List-linking pointers. This contains the saved chain in a | |
34 | * restorer element. */ | |
35 | Error_Context *prev, *next; | |
36 | /** Pointer to the location info structure. */ | |
37 | const Location *location; | |
38 | /** The message of the element. */ | |
39 | char *str; | |
40 | /** Indicates whether this element was already printed. */ | |
41 | bool is_printed; | |
42 | /** Indicates whether this element contains an outer chain. */ | |
43 | bool is_restorer; | |
44 | /** Indicates whether the outer chain was printed. */ | |
45 | bool outer_printed; | |
46 | /** Maximum number of errors to report */ | |
47 | static unsigned int max_errors; | |
48 | ||
49 | /** Copy constructor not implemented. */ | |
50 | Error_Context(const Error_Context& p); | |
51 | /** Assignment not implemented */ | |
52 | Error_Context& operator=(const Error_Context& p); | |
53 | private: | |
54 | /** Prints the actual chain of error contexts to fp. */ | |
55 | static void print_context(FILE *fp); | |
56 | public: | |
57 | /** Starts a new chain and preserves/restores the existing chain, | |
58 | * but keeps the n_keep elements of the old chain. */ | |
59 | Error_Context(size_t n_keep = 0); | |
60 | /** Adds a new element to the chain. The message shall be set | |
61 | * later with a call to set_message (e.g. in a conditional | |
62 | * construct). */ | |
63 | Error_Context(const Location *p_location); | |
64 | /** Adds a new element to the chain with the given message. */ | |
65 | Error_Context(const Location *p_location, const char *p_fmt, ...) | |
66 | __attribute__ ((__format__ (__printf__, 3, 4))); | |
67 | /** Destructor. Removes element from the chain. */ | |
68 | ~Error_Context(); | |
69 | ||
70 | /** Assigns the given message to the element */ | |
71 | void set_message(const char *p_fmt, ...) | |
72 | __attribute__ ((__format__ (__printf__, 2, 3))); | |
73 | ||
74 | /** Generic error reporting function. */ | |
75 | static void report_error(const Location *loc, const char *fmt, | |
76 | va_list args); | |
77 | ||
78 | /** Generic warning reporting function. */ | |
79 | static void report_warning(const Location *loc, const char *fmt, | |
80 | va_list args); | |
81 | ||
82 | static void report_note(const Location *loc, const char *fmt, | |
83 | va_list args); | |
84 | ||
85 | /** Returns the number of errors encountered. */ | |
86 | static unsigned int get_error_count() { return error_count; } | |
87 | /** Returns the number of warnings encountered. */ | |
88 | static unsigned int get_warning_count() { return warning_count; } | |
89 | /** Increments the error counter. */ | |
90 | static void increment_error_count(); | |
91 | /** Increments the warning counter. */ | |
92 | static void increment_warning_count(); | |
93 | ||
94 | /** Prints statistics about the number of errors and warnings */ | |
95 | static void print_error_statistics(); | |
96 | ||
97 | /** Set the maximum number of errors */ | |
98 | static void set_max_errors(unsigned int maxe) { | |
99 | max_errors = maxe; | |
100 | } | |
101 | }; | |
102 | ||
103 | } // namespace Common | |
104 | ||
105 | #endif // _Common_Error_HH |