File: tools.c

package info (click to toggle)
acr38 1.7.11-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,804 kB
  • ctags: 574
  • sloc: sh: 9,151; ansic: 4,031; makefile: 52
file content (148 lines) | stat: -rw-r--r-- 3,481 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
/*
 *  tools.c
 *  ifd-CCID
 *
 *  Created by JL on Mon Feb 10 2003.
 *  Copyright (c) 2003 Jean-Luc Giraud. All rights reserved.
 *  See COPYING file for license.
 *
 */

#include "tools.h"
#ifndef TARGET_OSX
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>

LogLevel bGlobalLogLevel = LogLevelMaximum;
BYTE bLogging =  1;
LogType bGlobalLogType =  LogTypeStderr;

// Logging tools
void LogMessage(const char * pcFile, WORD wLine, BYTE bLogLevel, const char* pcFormat,...)
{
    va_list ap;
    int rv;
    char *pcLogMessage = NULL;
    
    // Do not log if log level > 0 and logging is disabled
    if ( (!bLogging) &&  bGlobalLogLevel )
    {
        return;
    }
    if ( bGlobalLogLevel >= bLogLevel )
    {

        va_start(ap, pcFormat);
        rv = sprintf(pcLogMessage, pcFormat, ap);
        if ( rv == -1 )
        {
            pcLogMessage = "Could not allocate buffer for vasprintf";
        }
        va_end(ap);
        
        if ( bGlobalLogType == LogTypeSysLog)
        {
            syslog(LOG_INFO, "%s (%d): %s",  pcFile, (int)wLine, pcLogMessage);
        }
        else
        {
            fprintf(stderr, "%s (%d): %s\n",  pcFile, (int)wLine, pcLogMessage);
        }
        if ( rv != -1 )
        {
            free(pcLogMessage);
        }
    }
}
void LogHexBuffer(const char * pcFile, WORD wLine, BYTE bLogLevel,
                  BYTE * pbData, WORD wLength, const char* pcFormat,...)
{
    WORD index;
    va_list ap;
    int rv, rvHex = -1;
    char *pcLogMessage = NULL;
    char *pcLogHexData = NULL;
    char *pcLogHexCurrent;
    
    if ( (!bLogging) &&  bGlobalLogLevel )
    {
        return;
    }
    if ( bGlobalLogLevel >= bLogLevel )
    {
        va_start(ap, pcFormat);
        rv = sprintf(pcLogMessage, pcFormat, ap);
        if ( rv == -1 )
        {
            pcLogMessage = "Could not allocate buffer for vasprintf";
            
        }
        else
        {
            // Allocate buffer string for printing of data
            // Each byte is 3 chars (1 per nibble + space)
            // Plus 1 for NULL byte
            pcLogHexData = malloc(wLength * 3 * sizeof(char) + 1);
            rvHex = 0;
            if ( pcLogHexData == NULL )
            {
                rvHex = -1;
                pcLogHexData = "Could not allocate buffer to print binary buffer";
            }
            else
            {
                pcLogHexCurrent = pcLogHexData;
                for (index = 0; index < wLength; index++)
                {
                    sprintf(pcLogHexCurrent, "%02X ", pbData[index]);
                    pcLogHexCurrent += 3 * sizeof(char);
                }
            }
        }
        va_end(ap);

        if ( bGlobalLogType == LogTypeSysLog)
        {
            syslog(LOG_INFO, "%s (%d): %s : %s",  pcFile, (int)wLine, pcLogMessage,
                   pcLogHexData);
        }
        else
        {
            fprintf(stderr, "%s (%d): %s : %s\n",  pcFile, (int)wLine, pcLogMessage,
                    pcLogHexData);
        }
        if ( rv != -1 )
        {
            free(pcLogMessage);
        }
        if ( rvHex != -1 )
        {
            free(pcLogHexData);
        }
    }
}

void SetLogLevel(BYTE bLogLevel)
{
    bGlobalLogLevel = bLogLevel;
}

void SetLogType(LogType bLogtype)
{
    bGlobalLogType = bLogtype;
}

void StartLogging()
{
    bLogging =  1;
}

void StopLogging()
{
    bLogging =  0;
}