File: log.c

package info (click to toggle)
linux-minidisc 0.9.13-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,096 kB
  • ctags: 1,530
  • sloc: ansic: 6,345; cpp: 2,569; python: 2,451; perl: 866; sh: 22; makefile: 8
file content (101 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (4)
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
/*
 * log.c
 *
 * This file is part of libnetmd, a library for accessing Sony NetMD devices.
 *
 * Copyright (C) 2004 Bertrik Sikken
 * Copyright (C) 2011 Alexander Sulfrian
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

#include "log.h"

static netmd_loglevel trace_level = 0;

void netmd_set_log_level(netmd_loglevel level)
{
    trace_level = level;
}


void netmd_log_hex(netmd_loglevel level, const unsigned char* const buf, const size_t len)
{
    size_t i;
    size_t j = 0;
    int breakpoint = 0;

    if (level > trace_level) {
        return;
    }

    for (i = 0; i < len; i++)
    {
        printf("%02x ", buf[i] & 0xff);
        breakpoint++;
        if(!((i + 1)%16) && i)
        {
            printf("\t\t");
            for(j = ((i+1) - 16); j < ((i+1)/16) * 16; j++)
            {
                if(buf[j] < 30)
                    printf(".");
                else
                    printf("%c", buf[j]);
            }
            printf("\n");
            breakpoint = 0;
        }
    }

    if(breakpoint == 16)
    {
        printf("\n");
        return;
    }

    for(; breakpoint < 16; breakpoint++)
    {
        printf("   ");
    }
    printf("\t\t");

    for(j = len - (len%16); j < len; j++)
    {
        if(buf[j] < 30)
            printf(".");
        else
            printf("%c", buf[j]);
    }
    printf("\n");
}


void netmd_log(netmd_loglevel level, const char* const fmt, ...)
{
    va_list arg;

    if (level > trace_level) {
        return;
    }

    va_start(arg, fmt);
    vprintf(fmt, arg);
    va_end(arg);
}