~n3npq/lsb/distribution-checker

« back to all changes in this revision

Viewing changes to ptyshell/debug.c

  • Committer: biga
  • Date: 2009-04-24 14:16:44 UTC
  • Revision ID: biga@spidey.linux-foundation.org-20090424141644-7evzd6mjocix7e68
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "debug.h"
 
2
 
 
3
#include <errno.h>
 
4
#include <stdarg.h>
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <sys/types.h>
 
9
#include <unistd.h>
 
10
 
 
11
/*---------------------------------------------------------------------------
 
12
 * The verbosity flag.
 
13
 */
 
14
int verbose = 0;
 
15
 
 
16
/*---------------------------------------------------------------------------
 
17
 * Prints a message in printf-style if the verbosity flag is set.
 
18
 * A newline is added automatically.
 
19
 */
 
20
void notify(const char * message, ...)
 
21
{
 
22
    va_list args;
 
23
 
 
24
    if (verbose) {
 
25
 
 
26
        fputs("ptyshell: ", stderr);
 
27
 
 
28
        va_start(args, message);
 
29
        vfprintf(stderr, message, args);
 
30
        va_end(args);
 
31
 
 
32
        putc('\n', stderr);
 
33
    }
 
34
}
 
35
 
 
36
/*---------------------------------------------------------------------------
 
37
 * Prints an error message with printf-style formatting (a newline is
 
38
 * added automatically).
 
39
 */
 
40
void error(const char * message, ...)
 
41
{
 
42
    va_list args;
 
43
    const char * errno_desc = NULL;
 
44
 
 
45
    /* If errno is set, get its string description. */
 
46
    if (errno > 0) {
 
47
        errno_desc = strerror(errno);
 
48
    }
 
49
 
 
50
    fputs("ptyshell: ", stderr);
 
51
 
 
52
    va_start(args, message);
 
53
    vfprintf(stderr, message, args);
 
54
    va_end(args);
 
55
 
 
56
    /* If errno was set, append the error description. */
 
57
    if (errno_desc) {
 
58
        fputs(" [", stderr);
 
59
        fputs(errno_desc, stderr);
 
60
        putc(']', stderr);
 
61
    }
 
62
 
 
63
    /* Add a newline. */
 
64
    putc('\n', stderr);
 
65
}
 
66
 
 
67
/*
 
68
 * Reports a failed assertion and terminates the program.
 
69
 * This call never returns.
 
70
 */
 
71
void failed_assertion(const char * cond)
 
72
{
 
73
    fprintf(stderr, "ptyshell: assertion failed: %s\n", cond);
 
74
    exit(127);
 
75
}
 
76
 
 
77
/*
 
78
 * Introduces an assertion. If the condition 'x' is not valid
 
79
 * at the run-time, the program is immediately terminated.
 
80
 */
 
81
#define assert(x) if (!(x)) { failed_assertion(#x); }