2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 DEFINE_MTYPE_STATIC(LIB
, ERRINFO
, "error information")
40 * Thread-specific key for temporary storage of allocated ferr.
42 static pthread_key_t errkey
;
44 static void ferr_free(void *arg
)
46 XFREE(MTYPE_ERRINFO
, arg
);
49 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
50 static void err_key_init(void)
52 pthread_key_create(&errkey
, ferr_free
);
55 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
56 static void err_key_fini(void)
58 pthread_key_delete(errkey
);
62 * Global shared hash table holding reference text for all defined errors.
64 static pthread_mutex_t refs_mtx
= PTHREAD_MUTEX_INITIALIZER
;
67 static bool ferr_hash_cmp(const void *a
, const void *b
)
69 const struct log_ref
*f_a
= a
;
70 const struct log_ref
*f_b
= b
;
72 return f_a
->code
== f_b
->code
;
75 static inline unsigned int ferr_hash_key(const void *a
)
77 const struct log_ref
*f
= a
;
82 void log_ref_add(struct log_ref
*ref
)
86 pthread_mutex_lock(&refs_mtx
);
88 while (ref
[i
].code
!= END_FERR
) {
89 hash_get(refs
, &ref
[i
], hash_alloc_intern
);
93 pthread_mutex_unlock(&refs_mtx
);
96 struct log_ref
*log_ref_get(uint32_t code
)
98 struct log_ref holder
;
102 pthread_mutex_lock(&refs_mtx
);
104 ref
= hash_lookup(refs
, &holder
);
106 pthread_mutex_unlock(&refs_mtx
);
111 void log_ref_display(struct vty
*vty
, uint32_t code
, bool json
)
114 struct json_object
*top
= NULL
, *obj
= NULL
;
115 struct list
*errlist
;
119 top
= json_object_new_object();
121 pthread_mutex_lock(&refs_mtx
);
123 errlist
= code
? list_new() : hash_to_list(refs
);
125 pthread_mutex_unlock(&refs_mtx
);
128 ref
= log_ref_get(code
);
131 listnode_add(errlist
, ref
);
134 for (ALL_LIST_ELEMENTS_RO(errlist
, ln
, ref
)) {
138 snprintf(key
, sizeof(key
), "%"PRIu32
, ref
->code
);
139 obj
= json_object_new_object();
140 json_object_string_add(obj
, "title", ref
->title
);
141 json_object_string_add(obj
, "description",
143 json_object_string_add(obj
, "suggestion",
145 json_object_object_add(top
, key
, obj
);
150 snprintf(pbuf
, sizeof(pbuf
), "\nError %"PRIu32
" - %s",
151 ref
->code
, ref
->title
);
152 memset(ubuf
, '=', strlen(pbuf
));
153 ubuf
[strlen(pbuf
)] = '\0';
155 vty_out(vty
, "%s\n%s\n", pbuf
, ubuf
);
156 vty_out(vty
, "Description:\n%s\n\n", ref
->description
);
157 vty_out(vty
, "Recommendation:\n%s\n", ref
->suggestion
);
162 const char *str
= json_object_to_json_string_ext(
163 top
, JSON_C_TO_STRING_PRETTY
);
164 vty_out(vty
, "%s\n", str
);
165 json_object_free(top
);
168 list_delete(&errlist
);
171 DEFUN_NOSH(show_error_code
,
173 "show error <(1-4294967296)|all> [json]",
175 "Information on errors\n"
176 "Error code to get info about\n"
177 "Information on all errors\n"
180 bool json
= strmatch(argv
[argc
-1]->text
, "json");
183 if (!strmatch(argv
[2]->text
, "all"))
184 arg
= strtoul(argv
[2]->arg
, NULL
, 10);
186 log_ref_display(vty
, arg
, json
);
190 void log_ref_init(void)
192 pthread_mutex_lock(&refs_mtx
);
194 refs
= hash_create(ferr_hash_key
, ferr_hash_cmp
,
195 "Error Reference Texts");
197 pthread_mutex_unlock(&refs_mtx
);
200 void log_ref_fini(void)
202 pthread_mutex_lock(&refs_mtx
);
204 hash_clean(refs
, NULL
);
208 pthread_mutex_unlock(&refs_mtx
);
211 void log_ref_vty_init(void)
213 install_element(VIEW_NODE
, &show_error_code_cmd
);
217 const struct ferr
*ferr_get_last(ferr_r errval
)
219 struct ferr
*last_error
= pthread_getspecific(errkey
);
220 if (!last_error
|| last_error
->kind
== 0)
225 ferr_r
ferr_clear(void)
227 struct ferr
*last_error
= pthread_getspecific(errkey
);
229 last_error
->kind
= 0;
233 static ferr_r
ferr_set_va(const char *file
, int line
, const char *func
,
234 enum ferr_kind kind
, const char *pathname
,
235 int errno_val
, const char *text
, va_list va
)
237 struct ferr
*error
= pthread_getspecific(errkey
);
240 error
= XCALLOC(MTYPE_ERRINFO
, sizeof(*error
));
242 pthread_setspecific(errkey
, error
);
250 error
->unique_id
= jhash(text
, strlen(text
),
251 jhash(file
, strlen(file
), 0xd4ed0298));
253 error
->errno_val
= errno_val
;
255 snprintf(error
->pathname
, sizeof(error
->pathname
), "%s",
258 error
->pathname
[0] = '\0';
260 vsnprintf(error
->message
, sizeof(error
->message
), text
, va
);
264 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
265 enum ferr_kind kind
, const char *text
, ...)
270 rv
= ferr_set_va(file
, line
, func
, kind
, NULL
, 0, text
, va
);
275 ferr_r
ferr_set_internal_ext(const char *file
, int line
, const char *func
,
276 enum ferr_kind kind
, const char *pathname
,
277 int errno_val
, const char *text
, ...)
282 rv
= ferr_set_va(file
, line
, func
, kind
, pathname
, errno_val
, text
, va
);
287 #define REPLACE "$ERR"
288 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...)
290 char tmpmsg
[512], *replacepos
;
291 const struct ferr
*last_error
= ferr_get_last(err
);
295 vsnprintf(tmpmsg
, sizeof(tmpmsg
), msg
, va
);
298 replacepos
= strstr(tmpmsg
, REPLACE
);
300 vty_out(vty
, "%s\n", tmpmsg
);
302 replacepos
[0] = '\0';
303 replacepos
+= sizeof(REPLACE
) - 1;
304 vty_out(vty
, "%s%s%s\n", tmpmsg
,
305 last_error
? last_error
->message
: "(no error?)",