summaryrefslogtreecommitdiff
path: root/src/coolkey/log.cpp
blob: 1e44cf1e94051b4c81051615df45ac3a98e184fe (plain)
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
/* ***** BEGIN COPYRIGHT BLOCK *****
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * 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 version
 * 2.1 of the License.
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 * ***** END COPYRIGHT BLOCK *****/

#include <string>
#include <string.h>
#include <stdlib.h>
#include "mypkcs11.h"
#include <assert.h>
#include <stdio.h>
#include "log.h"
#include <cstdarg>
#include "PKCS11Exception.h"
#ifndef _WIN32
#include "syslog.h"
#endif


FileLog::FileLog(const char *filename )
{
    file = fopen(filename, "at");
    if( file == NULL ) {
        throw PKCS11Exception(CKR_GENERAL_ERROR, "Failed to open logfile");
    }
}

void
FileLog::log(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);

    vfprintf(file, msg, ap);
    fflush(file);

    va_end(ap);
}

#define ROW_LENGTH 16 
// dump the contents of an CKYBuffer, both in hex and  'ascii'.
// change ROW_LENGTH above to change how many bytes are printed on a single
// line
void
Log::dump(CKYBuffer *buf)
{
    CKYSize i;
    CKYSize size = CKYBuffer_Size(buf);
    char string[ROW_LENGTH+1];
    char *bp = &string[0];
    CKYByte c;

    for (i=0; i < size; i++) {
	if (i && ((i % (ROW_LENGTH-1)) == 0) ) {
	    *bp = 0;
	    log(" %s\n",string);
	    bp = &string[0];
	}
	c = CKYBuffer_GetChar(buf, i);
	log("%02x ",c);
	*bp++ =  (c < ' ') ? '.' : ((c & 0x80) ? '*' : c);
    }
    *bp = 0;
    for (i= (i % (ROW_LENGTH-1)); i && (i < ROW_LENGTH); i++) {
	log("   ");
    }
    log(" %s\n",string);
}

FileLog::~FileLog()
{
    fclose(file);
}


void
SysLog::log(const char *msg, ...)
{
#ifndef _WIN32

#define COOLKEY_PREFIX "libcoolkey:"
    va_list ap;
    char *msg2;

    va_start(ap, msg);
   
    msg2 = (char *)malloc(strlen(msg)+sizeof(COOLKEY_PREFIX));
    if (msg2) {
	strcpy(msg2, COOLKEY_PREFIX);
	strcat(msg2, msg); 

	vsyslog(LOG_WARNING,  msg2, ap);
	free(msg2);
   } else {
	vsyslog(LOG_WARNING,  msg, ap);
   }

    va_end(ap);
#endif
}