Debian Codegraph @

lib/ast/error.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
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 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 /*
12 * standalone mini error implementation
13 */
14
15 #include "config.h"
16
17 #include <ast/ast.h>
18 #include <ast/error.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <util/exit.h>
23
24 Error_info_t error_info;
25
26 void setErrorLine (int line) { error_info.line = line; }
27 void setErrorFileLine (char* src, int line) {
28 error_info.file = src;
29 error_info.line = line;
30 }
31 void setErrorId (char* id) { error_info.id = id; }
32 void setErrorErrors (int errors) { error_info.errors = errors; }
33 int getErrorErrors (void) { return error_info.errors; }
34 void setTraceLevel (int i) { error_info.trace = i; }
35
36 void errorv(const char *id, int level, const char *s, va_list ap)
37 {
38 int flags;
39
40 if (level < error_info.trace) return;
41 if (level < 0)
42 flags = 0;
43 else {
44 flags = level & ~ERROR_LEVEL;
45 level &= ERROR_LEVEL;
46 }
47 const char *prefix;
48 if (level && ((prefix = error_info.id) || (prefix = id))) {
49 if (flags & ERROR_USAGE)
50 fprintf(stderr, "Usage: %s ", prefix);
51 else
52 fprintf(stderr, "%s: ", prefix);
53 }
54 if (flags & ERROR_USAGE)
55 /*nop */ ;
56 else if (level < 0) {
57 int i;
58 for (i = 0; i < error_info.indent; i++)
59 fprintf(stderr, " ");
60 fprintf(stderr, "debug%d: ", level);
61 } else if (level) {
62 if (level == ERROR_WARNING) {
63 fprintf(stderr, "warning: ");
64 error_info.warnings++;
65 } else {
66 error_info.errors++;
67 if (level == ERROR_PANIC)
68 fprintf(stderr, "panic: ");
69 }
70 if (error_info.line) {
71 if (error_info.file && *error_info.file)
72 fprintf(stderr, "\"%s\", ", error_info.file);
73 fprintf(stderr, "line %d: ", error_info.line);
74 }
75 }
76 vfprintf(stderr, s, ap);
77 fprintf(stderr, "\n");
78 if (level >= ERROR_FATAL)
79 graphviz_exit(level - ERROR_FATAL + 1);
80 }
81
82 void error(int level, const char *s, ...)
83 {
84 va_list ap;
85
86 va_start(ap, s);
87 errorv(NULL, level, s, ap);
88 va_end(ap);
89 }
90
91 void errorf(void *handle, void *discipline, int level, const char *s, ...)
92 {
93 va_list ap;
94
95 va_start(ap, s);
96 errorv((discipline
97 && handle) ? *((char **) handle) : (char *) handle, level, s, ap);
98 va_end(ap);
99 }