File: beep-log.c

package info (click to toggle)
beep 1.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 524 kB
  • sloc: ansic: 863; sh: 228; python: 26; makefile: 10
file content (148 lines) | stat: -rw-r--r-- 4,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* beep-log.c - implement logging and message output
 * Copyright (C) 2018-2019 Hans Ulrich Niedermann
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "beep-log.h"


int log_level = 0;


const char *progname = "beep-log";


/** beep-log internal function to actually print a message */
static
void log_internal_vf(const char *levelstr, const char *const format, va_list args)
    __attribute__ ((nonnull (1,2)));


static
void log_internal_vf(const char *levelstr, const char *const format, va_list args)
{
    fprintf(stdout, "%s: %s: ", progname, levelstr);
    vfprintf(stdout, format, args);
    fputc('\n', stdout);
}


void log_output(const char *const format, ...)
{
    va_list args;

    va_start(args, format);
    vfprintf(stdout, format, args);
    va_end(args);
}


void log_error(const char *const format, ...)
{
    va_list args;

    va_start(args, format);
    log_internal_vf("Error", format, args);
    va_end(args);
}


void log_warning(const char *const format, ...)
{
    va_list args;

    va_start(args, format);
    log_internal_vf("Warning", format, args);
    va_end(args);
}

void log_verbose(const char *const format, ...)
{
    va_list args;

    if (log_level > 0) {
        va_start(args, format);
        log_internal_vf("Verbose", format, args);
        va_end(args);
    }
}


/** Log a range of data */
void log_data(const void *const buf, const size_t start_ofs, const size_t size)
{
    if (log_level <= 1) {
        return;
    }
    const unsigned char *const ucbuf = buf;
    static const char hexchar[] = "0123456789abcdef";
    char linebuf[80] =
        "0000  __ __ __ __ __ __ __ __-__ __ __ __ __ __ __ __  ........-........";
    for (size_t line_ofs=0, remaining=size; line_ofs<size; line_ofs+=16, remaining-=16) {
        linebuf[0]    = hexchar[(line_ofs>>12) & 0xf];
        linebuf[1]    = hexchar[(line_ofs>> 8) & 0xf];
        linebuf[2]    = hexchar[(line_ofs>> 4) & 0xf];
        linebuf[3]    = hexchar[(line_ofs>> 0) & 0xf];
        const size_t line_cols = (remaining<16)? remaining : 16;
        for (size_t col_ofs=0; col_ofs<line_cols; ++col_ofs) {
            const size_t ofs = start_ofs + line_ofs + col_ofs;
            const unsigned char ch = ucbuf[ofs];
            const char hex_hi = hexchar[(ch >> 4) & 0x0f];
            const char hex_lo = hexchar[(ch >> 0) & 0x0f];
            linebuf[6+3*col_ofs+0]    = hex_hi;
            linebuf[6+3*col_ofs+1]    = hex_lo;
            linebuf[6+3*16+1+col_ofs+(col_ofs/8)] = ((0x20 <= ch) && (ch <= 0x7f)) ? (char)ch : '.';
        }
        for (size_t col_ofs=line_cols; col_ofs<16; ++col_ofs) {
            linebuf[6+3*col_ofs+0]    = ' ';
            linebuf[6+3*col_ofs+1]    = ' ';
            linebuf[6+3*16+1+col_ofs+(col_ofs/8)] = ' ';
        }
        if (line_cols <= 8) {
            linebuf[6+3*8-1]    = ' ';
            linebuf[6+3*16+1+8] = ' ';
        }
        fprintf(stdout, "%s: %s: %s\n", progname, "Data", linebuf);
    }
}


void log_init(const int argc, char *const argv[]) {
    /* if argv[0] is "./foo/bar/beep", set progname to "beep" */
    if (argc >= 1) {
        const char *last_slash = strrchr(argv[0], '/');
        if (last_slash) {
            const char *post_last_slash = last_slash+1;
            if (post_last_slash) {
                progname = post_last_slash;
            }
        } else {
            progname = argv[0];
        }
    }
}


/*
 * Local Variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */