diff options
author | Werner Almesberger <werner@almesberger.net> | 2016-08-17 21:37:15 -0300 |
---|---|---|
committer | Werner Almesberger <werner@almesberger.net> | 2016-08-17 21:37:15 -0300 |
commit | 30d665115a8f7d72f659a1e77b6eb14e727fbe57 (patch) | |
tree | f0ff40e1a6b654571745dc5c289a8ed6cfd103ca /misc | |
parent | ae27fe967c09bf0d685952dc7a943d1535eb6af4 (diff) | |
download | eeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.tar.gz eeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.tar.bz2 eeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.zip |
eeshow/: util.h and diag.c go to misc/
Diffstat (limited to 'misc')
-rw-r--r-- | misc/diag.c | 86 | ||||
-rw-r--r-- | misc/diag.h | 71 | ||||
-rw-r--r-- | misc/util.h | 53 |
3 files changed, 210 insertions, 0 deletions
diff --git a/misc/diag.c b/misc/diag.c new file mode 100644 index 0000000..908deb8 --- /dev/null +++ b/misc/diag.c @@ -0,0 +1,86 @@ +/* + * misc/diag.h - Diagnostics + * + * Written 2016 by Werner Almesberger + * Copyright 2016 by Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + + +#include "misc/diag.h" + + +unsigned verbose = 0; + + +/* ----- Specialized diagnostic functions ---------------------------------- */ + + +void diag_pfatal(const char *s) +{ + fatal("%s: %s\n", s, strerror(errno)); +} + + +void diag_perror(const char *s) +{ + error("%s: %s\n", s, strerror(errno)); +} + + +/* ----- General diagnostic functions -------------------------------------- */ + +void fatal(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); /* @@@ for now ... */ +} + + +void error(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + + +void warning(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fprintf(stderr, "warning: "); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + + +void progress(unsigned level, const char *fmt, ...) +{ + va_list ap; + + if (level > verbose) + return; + va_start(ap, fmt); + fprintf(stderr, "%*s", level * 2, ""); + vfprintf(stderr, fmt, ap); + va_end(ap); +} diff --git a/misc/diag.h b/misc/diag.h new file mode 100644 index 0000000..025b791 --- /dev/null +++ b/misc/diag.h @@ -0,0 +1,71 @@ +/* + * misc/diag.h - Diagnostics + * + * Written 2016 by Werner Almesberger + * Copyright 2016 by Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef MISC_DIAG_H +#define MISC_DIAG_H + +/* + * 0: no progress indications + * 1: reasonable progress indications + * 2: verbose output + * > 2: go wild ! + */ + +extern unsigned verbose; + + +/* ----- Specialized diagnostic functions ---------------------------------- */ + + +/* perror, based on "fatal" or "error" */ + +void diag_pfatal(const char *s); +void diag_perror(const char *s); + + +/* ----- General diagnostic functions -------------------------------------- */ + + +/* + * Terminate immediately. Further execution makes no sense. + * E.g., out of memory. + */ + +void __attribute__((noreturn)) fatal(const char *fmt, ...) + __attribute__((format(printf, 1, 2))); + +/* + * Operation has failed, but the program as a whole may still be able to + * continue. E.g., a schematics component was not found. + */ + +void error(const char *fmt, ...) + __attribute__((format(printf, 1, 2))); + +/* + * A minor operation has failed or some other issue was detected. This may + * be (or lead to) a more serious problem, but does not immediately affect + * operation. + */ + +void warning(const char *fmt, ...) + __attribute__((format(printf, 1, 2))); + +/* + * Progress message, used mainly for debugging. "level" is the minimum + * verbosity level required. + */ + +void progress(unsigned level, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); + +#endif /* !MISC_DIAG_H */ diff --git a/misc/util.h b/misc/util.h new file mode 100644 index 0000000..d0c5610 --- /dev/null +++ b/misc/util.h @@ -0,0 +1,53 @@ +/* + * misc/util.h - Common utility functions + * + * Written 2016 by Werner Almesberger + * Copyright 2016 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef MISC_UTIL_H +#define MISC_UTIL_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +#define alloc_size(s) \ + ({ void *alloc_size_tmp = malloc(s); \ + if (!alloc_size_tmp) { \ + perror("malloc"); \ + exit(1); \ + } \ + alloc_size_tmp; }) + +#define alloc_type(t) ((t *) alloc_size(sizeof(t))) +#define alloc_type_n(t, n) ((t *) alloc_size(sizeof(t) * (n))) + +#define stralloc(s) \ + ({ char *stralloc_tmp = strdup(s); \ + if (!stralloc_tmp) { \ + perror("strdup"); \ + exit(1); \ + } \ + stralloc_tmp; }) + + +#define ARRAY_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) +#define ARRAY_END(a) ((a) + ARRAY_ELEMENTS(a)) + + +#define swap(a, b) \ + ({ typeof(a) _tmp = (a); a = (b); b = _tmp; }) + + +#define unsupported(s) \ + fprintf(stderr, __FILE__ ":%d: unsupported: " s "\n", __LINE__) + +#endif /* !MISC_UTIL_H */ |