/
common.c
127 lines (102 loc) · 2.38 KB
/
common.c
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
/*
* Copyright (c) 2008 Rainer Clasen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms described in the file LICENSE included in this
* distribution.
*
*/
#include "common.h"
#include <stdarg.h>
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <ctype.h>
const char *srmio_version = PACKAGE_STRING;
void srmio_dumphexv(srmio_logfunc_t func, void *data,
const unsigned char *buf, size_t blen,
const char *fmt, va_list ap )
{
struct timeval tv;
size_t i;
char msg[SRMIO_ERROR_MSG_SIZE];
int len;
int r;
assert( ! blen || buf );
assert( fmt );
#ifndef DEBUG
if( ! func )
return;
#endif
if( 0 != gettimeofday( &tv, NULL ))
return;
len = snprintf( msg, SRMIO_ERROR_MSG_SIZE,
"%lu.%03lu ", tv.tv_sec, (unsigned long)tv.tv_usec / 1000 );
if( 0 > len )
return;
r = vsnprintf( msg+len, SRMIO_ERROR_MSG_SIZE - len, fmt, ap );
if( 0 > r || r > SRMIO_ERROR_MSG_SIZE - len )
return;
len += r;
r = snprintf( msg+len, SRMIO_ERROR_MSG_SIZE - len, " %u:",
(unsigned)blen );
if( 0 > r || r > SRMIO_ERROR_MSG_SIZE - len )
return;
len +=r;
for( i=0; i < blen; ++i ){
unsigned char c = buf[i];
unsigned char p = c;
if( ! isprint(c) )
p=' ';
r = snprintf( msg+len, SRMIO_ERROR_MSG_SIZE - len,
" 0x%02x/%c", c, p );
if( 0 > r || r > SRMIO_ERROR_MSG_SIZE - len )
return;
len +=r;
}
#ifdef DEBUG
fprintf( stderr, "%s\n", msg );
if( func )
#endif
(*func)( msg, data );
}
void srmio_dumphex(srmio_logfunc_t func, void *data,
const unsigned char *buf, size_t blen,
const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
srmio_dumphexv( func, data, buf, blen, fmt, ap );
va_end( ap );
}
void srmio_debugv( srmio_logfunc_t func, void *data, const char *fmt, va_list ap )
{
struct timeval tv;
char msg[SRMIO_ERROR_MSG_SIZE];
int len;
assert(fmt);
#ifndef DEBUG
if( ! func )
return;
#endif
if( 0 != gettimeofday( &tv, NULL ))
return;
len = snprintf( msg, SRMIO_ERROR_MSG_SIZE, "%lu.%03lu ",
tv.tv_sec, (unsigned long)tv.tv_usec / 1000 );
if( 0 > len )
return;
if( 0 > vsnprintf( msg+len, SRMIO_ERROR_MSG_SIZE - len, fmt, ap ))
return;
#ifdef DEBUG
fprintf( stderr, "%s\n", msg );
if( func )
#endif
(*func)( msg, data );
}
void srmio_debug( srmio_logfunc_t func, void *data, const char *fmt, ... )
{
va_list ap;
va_start( ap, fmt );
srmio_debugv( func, data, fmt, ap );
va_end( ap );
}