Click
Loading...
Searching...
No Matches
error.hh
Go to the documentation of this file.
1// -*- related-file-name: "../../lib/error.cc" -*-
2#ifndef CLICK_ERROR_HH
3#define CLICK_ERROR_HH
4#include <click/string.hh>
5#if defined(CLICK_USERLEVEL) || defined(CLICK_TOOL) || defined(CLICK_MINIOS)
6# include <stdio.h>
7#endif
8#if CLICK_BSDMODULE
9# include <machine/stdarg.h>
10#else
11# include <stdarg.h>
12#endif
13
14#if HAVE_ADDRESSABLE_VA_LIST
15# define VA_LIST_REF_T va_list *
16# define VA_LIST_DEREF(val) (*(val))
17# define VA_LIST_REF(val) (&(val))
18#else
19# define VA_LIST_REF_T va_list
20# define VA_LIST_DEREF(val) (val)
21# define VA_LIST_REF(val) (val)
22#endif
23#if __GNUC__ <= 3
24# define ERRH_SENTINEL
25#else
26# define ERRH_SENTINEL __attribute__((sentinel))
27#endif
28CLICK_DECLS
29
90class ErrorHandler { public:
91
98 enum Level {
99 el_abort = -999,
100 el_fatal = -1,
107 el_notice = 5,
110 el_debug = 7
111 };
112
114 static const char e_abort[],
115 e_fatal[],
116 e_emergency[],
117 e_alert[],
118 e_critical[],
119 e_error[],
120 e_warning[],
121 e_warning_annotated[],
122 e_notice[],
123 e_info[],
124 e_debug[];
125
128 : _nerrors(0) {
129 }
130
131 virtual ~ErrorHandler() {
132 }
133
134
149
154 static void static_cleanup();
155
156
160 return the_default_handler;
161 }
162
168 static void set_default_handler(ErrorHandler *errh);
169
172 return the_silent_handler;
173 }
174
175
176 static const int ok_result;
178 static const int error_result;
180
181
186 void debug(const char *fmt, ...);
188 void message(const char *fmt, ...);
193 int warning(const char *fmt, ...);
196 int error(const char *fmt, ...);
201 int fatal(const char *fmt, ...);
202
204 void ldebug(const String &landmark, const char *fmt, ...);
206 void lmessage(const String &landmark, const char *fmt, ...);
208 int lwarning(const String &landmark, const char *fmt, ...);
210 int lerror(const String &landmark, const char *fmt, ...);
212 int lfatal(const String &landmark, const char *fmt, ...);
213
214
226 int xmessage(const String &str);
232 int xmessage(const String &anno, const String &str) {
233 return xmessage(combine_anno(str, anno));
234 }
241 int xmessage(const String &anno, const char *fmt, va_list val) {
242 return xmessage(anno, vformat(fmt, val));
243 }
251 int xmessage(const String &landmark, const String &anno,
252 const String &str) {
253 return xmessage(combine_anno(anno, make_landmark_anno(landmark)), str);
254 }
264 int xmessage(const String &landmark, const String &anno,
265 const char *fmt, va_list val) {
266 return xmessage(landmark, anno, vformat(fmt, val));
267 }
268
269
286 int nerrors() const {
287 return _nerrors;
288 }
289
290
349 static String xformat(int default_flags, const char *fmt, ...);
351 static String vxformat(int default_flags, const char *fmt, va_list val);
353 static String xformat(const char *fmt, ...);
355 static String vxformat(const char *fmt, va_list val) {
356 return vxformat(0, fmt, val);
357 }
358
359
369 virtual String vformat(const char *fmt, va_list val);
370
379 String format(const char *fmt, ...);
380
393 virtual String decorate(const String &str);
394
424 virtual void *emit(const String &str, void *user_data, bool more);
425
438 virtual void account(int level) {
439 if (level <= el_error)
440 ++_nerrors;
441 }
442
446 virtual void clear() {
447 _nerrors = 0;
448 }
449
450
466 static String make_anno(const char *name, const String &value);
467
482 static String combine_anno(const String &str, const String &anno);
483
516 static const char *parse_anno(const String &str,
517 const char *begin, const char *end, ...) ERRH_SENTINEL;
518
527 static const char *skip_anno(const char *begin, const char *end) {
528 String name, value;
529 const char *x = begin;
530 do {
531 x = skip_anno(String(), x, end, &name, &value, false);
532 } while (name);
533 return x;
534 }
535
536
544 if (x && x[0] == '{')
545 return x;
546 else if (x)
547 return make_anno("l", x);
548 else
549 return String();
550 }
551
559 static String clean_landmark(const String &landmark, bool colon = false);
560
561
562 // error conversions
563 struct Conversion;
564 typedef String (*ConversionFunction)(int flags, VA_LIST_REF_T);
577 static Conversion *add_conversion(const String &name, ConversionFunction func);
578 static int remove_conversion(Conversion *conversion);
579
580 private:
581
582 int _nerrors;
583
584 static ErrorHandler *the_default_handler;
585 static ErrorHandler *the_silent_handler;
586
587 static const char *skip_anno(const String &str,
588 const char *begin, const char *end,
589 String *name_result, String *value_result,
590 bool raw);
591
592};
593
594
600class SilentErrorHandler : public ErrorHandler { public:
601
603 }
604
605};
606
607
618class ErrorVeneer : public ErrorHandler { public:
619
626 : _errh(errh) {
627 }
628
629 String vformat(const char *fmt, va_list val);
630 String decorate(const String &str);
631 void *emit(const String &str, void *user_data, bool more);
632 void account(int level);
633
634 private:
635
636 ErrorHandler *_errh;
637
638};
639
640
641#if defined(CLICK_USERLEVEL) || defined(CLICK_TOOL) || defined(CLICK_MINIOS)
648class FileErrorHandler : public ErrorHandler { public:
649
653 FileErrorHandler(FILE *f, const String &prefix = String());
654
655 void set_default_flags(int default_flags) {
656 _default_flags = default_flags;
657 }
658
659 String vformat(const char *fmt, va_list val);
660 void *emit(const String &str, void *user_data, bool more);
661 void account(int level);
662
663 private:
664
665 FILE *_f;
666 String _context;
667 int _default_flags;
668
669};
670#endif
671
672
687class LocalErrorHandler : public ErrorVeneer { public:
688
691 : ErrorVeneer(errh) {
692 }
693
694};
695
696
737class ContextErrorHandler : public ErrorVeneer { public:
738
745 ContextErrorHandler(ErrorHandler *errh, const char *fmt, ...);
746
748 bool context_printed() const {
749 return _context_printed;
750 }
751
753 void set_context_printed(bool x) {
754 _context_printed = x;
755 }
756
758 void set_context(const String &str) {
759 _context = str;
760 }
761
767 void set_indent(const String &str) {
768 _indent = str;
769 }
770
779 void set_context_landmark(const String &str) {
780 _context_landmark = make_landmark_anno(str);
781 }
782
783 String decorate(const String &str);
784
785 private:
786
787 String _context;
788 String _indent;
789 String _context_landmark;
790 bool _context_printed;
791
792};
793
794
808class PrefixErrorHandler : public ErrorVeneer { public:
809
813 PrefixErrorHandler(ErrorHandler *errh, const String &prefix);
814
815 String decorate(const String &str);
816
817 private:
818
819 String _prefix;
820
821};
822
823
839class LandmarkErrorHandler : public ErrorVeneer { public:
840
844 LandmarkErrorHandler(ErrorHandler *errh, const String &landmark);
845
847 void set_landmark(const String &landmark) {
848 _landmark = make_landmark_anno(landmark);
849 }
850
851 String decorate(const String &str);
852
853 private:
854
855 String _landmark;
856
857};
858
859
860#if defined(CLICK_USERLEVEL) || defined(CLICK_TOOL) || defined(CLICK_MINIOS)
866class BailErrorHandler : public ErrorVeneer { public:
867
874 BailErrorHandler(ErrorHandler *errh, int level = el_error);
875
876 void account(int level);
877
878 private:
879
880 int _level;
881
882};
883#endif
884
885#undef ERRH_SENTINEL
886CLICK_ENDDECLS
887#endif
A stackable ErrorHandler that exits when errors occur.
Definition error.hh:866
void account(int level)
Account for an error message at level level.
Definition error.cc:1240
A stackable ErrorHandler that prints context lines.
Definition error.hh:737
void set_context(const String &str)
Set the context string to str.
Definition error.hh:758
void set_context_landmark(const String &str)
Set the context landmark to str.
Definition error.hh:779
void set_context_printed(bool x)
Set whether the context has been printed.
Definition error.hh:753
void set_indent(const String &str)
Set the indent string to str.
Definition error.hh:767
String decorate(const String &str)
Decorate an error message.
Definition error.cc:1168
bool context_printed() const
Return true iff the context has already been printed.
Definition error.hh:748
Error reporting class.
Definition error.hh:90
int xmessage(const String &anno, const char *fmt, va_list val)
Format and print an error message, adding annotations.
Definition error.hh:241
static String xformat(int default_flags, const char *fmt,...)
Format an error string.
Definition error.cc:764
ConversionFlags
Definition error.hh:565
@ cf_signed
Set for conversions of signed numbers.
Definition error.hh:573
@ cf_alternate_form
Set for conversions using the '#' flag.
Definition error.hh:570
@ cf_space_positive
Set for conversions using the ' ' flag.
Definition error.hh:568
@ cf_utf8
Set to use UTF-8 characters on output.
Definition error.hh:575
@ cf_negative
Set for conversions of negative numbers.
Definition error.hh:574
@ cf_uppercase
Set for 'X' conversions (not 'x').
Definition error.hh:572
@ cf_zero_pad
Set for conversions using the '0' flag.
Definition error.hh:566
@ cf_left_just
Set for conversions using the '-' flag.
Definition error.hh:569
@ cf_plus_positive
Set for conversions using the '+' flag.
Definition error.hh:567
@ cf_singlequote
Set for conversions using the '\'' flag.
Definition error.hh:571
static void static_cleanup()
Tear down the ErrorHandler implementation.
Definition error.cc:1091
virtual String decorate(const String &str)
Decorate an error message.
Definition error.cc:927
int error(const char *fmt,...)
Print an error message (level el_error).
Definition error.cc:825
virtual void clear()
Clear accumulated error state.
Definition error.hh:446
static String make_anno(const char *name, const String &value)
Create an error annotation.
Definition error.cc:98
static ErrorHandler * silent_handler()
Return the global silent ErrorHandler.
Definition error.hh:171
static const char * skip_anno(const char *begin, const char *end)
Skip a string's error annotations.
Definition error.hh:527
Level
Error level constants.
Definition error.hh:98
@ el_error
Error level for normal error conditions.
Definition error.hh:105
@ el_abort
Error level that triggers abort().
Definition error.hh:99
@ el_alert
Alert error level: action must be taken.
Definition error.hh:103
@ el_fatal
Definition error.hh:100
@ el_emergency
Emergency error level: system is unusable.
Definition error.hh:102
@ el_debug
Error level for debug messages.
Definition error.hh:110
@ el_critical
Error level for critical conditions.
Definition error.hh:104
@ el_info
Error level for informational messages.
Definition error.hh:109
@ el_warning
Error level for warning conditions.
Definition error.hh:106
@ el_notice
Definition error.hh:107
int nerrors() const
Return the number of errors reported via this handler.
Definition error.hh:286
int xmessage(const String &str)
Print an annotated error message.
Definition error.cc:898
virtual void * emit(const String &str, void *user_data, bool more)
Output an error message line.
Definition error.cc:933
int xmessage(const String &landmark, const String &anno, const String &str)
Print an error message, adding landmark and other annotations.
Definition error.hh:251
static const char e_abort[]
Error level indicators.
Definition error.hh:114
static ErrorHandler * static_initialize(ErrorHandler *errh)
Initialize the ErrorHandler implementation.
Definition error.cc:1074
virtual void account(int level)
Account for an error message at level level.
Definition error.hh:438
virtual String vformat(const char *fmt, va_list val)
Format an error string.
Definition error.cc:921
int fatal(const char *fmt,...)
Print a fatal error message (level el_fatal).
Definition error.cc:835
ErrorHandler()
Construct an ErrorHandler.
Definition error.hh:127
static const int ok_result
Definition error.hh:176
int warning(const char *fmt,...)
Print a warning message (level el_warning).
Definition error.cc:815
static String make_landmark_anno(const String &x)
Return a landmark annotation equal to x.
Definition error.hh:543
void message(const char *fmt,...)
Print an informational message (level el_info).
Definition error.cc:806
int xmessage(const String &landmark, const String &anno, const char *fmt, va_list val)
Format and print an error message, adding landmark and other annotations.
Definition error.hh:264
int xmessage(const String &anno, const String &str)
Print an error message, adding annotations.
Definition error.hh:232
static const char * parse_anno(const String &str, const char *begin, const char *end,...) ERRH_SENTINEL
Parse error annotations from a string.
Definition error.cc:200
static String vxformat(const char *fmt, va_list val)
Definition error.hh:355
static String combine_anno(const String &str, const String &anno)
Apply annotations from anno to every line in str.
Definition error.cc:238
static ErrorHandler * default_handler()
Return the default ErrorHandler.
Definition error.hh:159
void lmessage(const String &landmark, const char *fmt,...)
Print an informational message with a landmark annotation.
Definition error.cc:855
void ldebug(const String &landmark, const char *fmt,...)
Print a debug message with a landmark annotation.
Definition error.cc:845
static void set_default_handler(ErrorHandler *errh)
Set the default ErrorHandler to errh.
Definition error.cc:1104
static String vxformat(int default_flags, const char *fmt, va_list val)
Definition error.cc:385
static String clean_landmark(const String &landmark, bool colon=false)
Clean the landmark.
Definition error.cc:301
static const int error_result
Definition error.hh:178
int lerror(const String &landmark, const char *fmt,...)
Print an error message with a landmark annotation.
Definition error.cc:876
int lfatal(const String &landmark, const char *fmt,...)
Print a fatal error message with a landmark annotation.
Definition error.cc:887
int lwarning(const String &landmark, const char *fmt,...)
Print a warning message with a landmark annotation.
Definition error.cc:865
void debug(const char *fmt,...)
Print a debug message (level el_debug).
Definition error.cc:797
String format(const char *fmt,...)
Format an error string.
Definition error.cc:784
Base class for ErrorHandlers that forward messages.
Definition error.hh:618
void account(int level)
Account for an error message at level level.
Definition error.cc:1142
String decorate(const String &str)
Decorate an error message.
Definition error.cc:1124
ErrorVeneer(ErrorHandler *errh)
Construct an ErrorVeneer.
Definition error.hh:625
String vformat(const char *fmt, va_list val)
Format an error string.
Definition error.cc:1115
void * emit(const String &str, void *user_data, bool more)
Output an error message line.
Definition error.cc:1133
An ErrorHandler that prints error messages to a given file.
Definition error.hh:648
void * emit(const String &str, void *user_data, bool more)
Output an error message line.
Definition error.cc:962
void account(int level)
Account for an error message at level level.
Definition error.cc:975
String vformat(const char *fmt, va_list val)
Format an error string.
Definition error.cc:956
A stackable ErrorHandler that adds a default landmark to error messages.
Definition error.hh:839
String decorate(const String &str)
Decorate an error message.
Definition error.cc:1222
void set_landmark(const String &landmark)
Set the default landmark applied to error messages.
Definition error.hh:847
A convenience stackable ErrorHandler.
Definition error.hh:687
LocalErrorHandler(ErrorHandler *errh)
Construct a LocalErrorHandler.
Definition error.hh:690
A stackable ErrorHandler that adds a prefix to error messages.
Definition error.hh:808
String decorate(const String &str)
Decorate an error message.
Definition error.cc:1206
An ErrorHandler that does not report messages.
Definition error.hh:600
A string of characters.
Definition string.hh:22
Click's String class.