aboutsummaryrefslogtreecommitdiff
path: root/gacopyz/log.c
blob: 8f8385d9bf6a1f49bb922514e2002da26370cb91 (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* This file is part of gacopyz.
   Copyright (C) 2006, 2007 Sergey Poznyakoff

   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 3, 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 <gacopyz_priv.h>

void (*__gacopyz_log_printer)(int, char *, va_list) =
       gacopyz_syslog_log_printer;

/* Logging */
void
gacopyz_syslog_log_printer(int level, char *fmt, va_list ap)
{
	switch (level) {
	case SMI_LOG_DEBUG:
		level = LOG_DEBUG;
		break;
	case SMI_LOG_INFO:
		level = LOG_INFO;
		break;
	case SMI_LOG_WARN:
		level = LOG_WARNING;
		break;
	case SMI_LOG_ERR:
		level = LOG_ERR;
		break;

	case SMI_LOG_FATAL:
	default:
		level = LOG_EMERG;
	}
	vsyslog(level, fmt, ap); /* FIXME: if absent? */
}

static char *level_name[] = {
	"DEBUG",
	"INFO",
	"WARN",
	"ERR",
	"FATAL"
};

int
gacopyz_string_to_log_level(const char *str)
{
	int i;
	for (i = 0; i < sizeof level_name / sizeof level_name[0]; i++)
		if (strcasecmp (str, level_name[i]) == 0)
			return i;
	return -1;
}

const char *
gacopyz_log_level_to_string(int level)
{
	if (level < 0 || level > sizeof level_name / sizeof level_name[0])
		return NULL;
	return level_name[level];
}

void
gacopyz_stderr_log_printer(int level, char *fmt, va_list ap)
{
	fprintf(stderr, "Gacopyz %s: ", gacopyz_log_level_to_string(level));
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
}


void
gacopyz_logmsg(int level, char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	__gacopyz_log_printer(level, fmt, ap);
	va_end(ap);
}

void
gacopyz_log(gacopyz_conn_t conn, int level, char *fmt, ...)
{
	if (conn->logmask & SMI_LOG_MASK(level)) {
		va_list ap;
		va_start(ap, fmt);
		__gacopyz_log_printer(level, fmt, ap);
		va_end(ap);
	}
}

void
gacopyz_io_log(gacopyz_iod_t iod, int level, char *fmt, ...)
{
	if (iod->logmask & SMI_LOG_MASK(level)) {
		va_list ap;
		va_start(ap, fmt);
		__gacopyz_log_printer(level, fmt, ap);
		va_end(ap);
	}
}

size_t
gacopyz_format_vbuf(char vbuf[GACOPYZ_VBUFSIZE], const char *buf, size_t size)
{
	char *p = vbuf;
	char *q = vbuf + 51;
	int i;
	size_t j = 0;
	
	for (i = 0; i < 16; i++) {
		unsigned c;
		if (j < size) {
			c = *(const unsigned char*)buf++;
			j++;
		} else
			c = 0;
		sprintf(p, "%02X ", c);
		p += 3;
		*q++ = isprint(c) ? c : '.';
		if (i == 7) {
			*p++ = ' ';
			*q++ = ' ';
		}
	}
	*p++ = ' ';
	*p = ' ';
	*q = 0;
	return j;
}

void
_gacopyz_logdump(int logmask, int level, const char *pfx,
		 const char *buf, size_t size)
{
	if (logmask & SMI_LOG_MASK(level)) {
		char vbuf[GACOPYZ_VBUFSIZE];

		while (size) {
			size_t rd = gacopyz_format_vbuf(vbuf, buf, size);
			gacopyz_logmsg(level, "%s: %s", pfx, vbuf);
			size -= rd;
			buf += rd;
		}
	}
}

void
gacopyz_logdump(gacopyz_conn_t conn, int level, const char *pfx,
		const char *buf, size_t size)
{
	return _gacopyz_logdump(conn->logmask, level, pfx, buf, size);
}

void
gacopyz_io_logdump(gacopyz_iod_t iod, int level, const char *pfx,
		   const char *buf, size_t size)
{
	return _gacopyz_logdump(iod->logmask, level, pfx, buf, size);
}

void
gacopyz_set_logger(void (*logger)(int, char *, va_list))
{
	__gacopyz_log_printer = logger;
}

Return to:

Send suggestions and report system problems to the System administrator.