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