Root/eeshow/misc/diag.c

1/*
2 * misc/diag.h - Diagnostics
3 *
4 * Written 2016 by Werner Almesberger
5 * Copyright 2016 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdarg.h>
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <errno.h>
19
20
21#include "misc/diag.h"
22
23
24unsigned verbose = 0;
25
26
27/* ----- Specialized diagnostic functions ---------------------------------- */
28
29
30void diag_pfatal(const char *s)
31{
32    fatal("%s: %s\n", s, strerror(errno));
33}
34
35
36void diag_perror(const char *s)
37{
38    error("%s: %s\n", s, strerror(errno));
39}
40
41
42/* ----- General diagnostic functions -------------------------------------- */
43
44void fatal(const char *fmt, ...)
45{
46    va_list ap;
47
48    va_start(ap, fmt);
49    vfprintf(stderr, fmt, ap);
50    va_end(ap);
51    fprintf(stderr, "\n");
52    exit(1); /* @@@ for now ... */
53}
54
55
56void error(const char *fmt, ...)
57{
58    va_list ap;
59
60    va_start(ap, fmt);
61    vfprintf(stderr, fmt, ap);
62    va_end(ap);
63    fprintf(stderr, "\n");
64}
65
66
67void warning(const char *fmt, ...)
68{
69    va_list ap;
70
71    va_start(ap, fmt);
72    fprintf(stderr, "warning: ");
73    vfprintf(stderr, fmt, ap);
74    va_end(ap);
75    fprintf(stderr, "\n");
76}
77
78
79void progress(unsigned level, const char *fmt, ...)
80{
81    va_list ap;
82
83    if (level > verbose)
84        return;
85    va_start(ap, fmt);
86    fprintf(stderr, "%*s", level * 2, "");
87    vfprintf(stderr, fmt, ap);
88    va_end(ap);
89    fprintf(stderr, "\n");
90}
91

Archive Download this file

Branches:
master



interactive