summaryrefslogtreecommitdiff
path: root/src/debug.c
blob: 9ef62378ee573d3b3b77ac33876ba93ba804fdda (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
179
180
181
182
183
184
185
186
187
188
189
/* debug.c - helpful output in desperate situations
   Copyright (C) 2000 Werner Koch (dd9jn)
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH

   This file is part of Assuan.

   Assuan 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.

   Assuan 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 program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#ifndef HAVE_DOSISH_SYSTEM
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
#include <assert.h>

#include "assuan-defs.h"
#include "debug.h"


/* Log the formatted string FORMAT at debug category CAT higher.  */
void
_assuan_debug (assuan_context_t ctx, unsigned int cat, const char *format, ...)
{
  va_list arg_ptr;
  int saved_errno;
  char *msg;
  int res;

  /* vasprintf is an expensive operation thus we first check whether
     the callback has enabled CAT for logging.  */
  if (!ctx
      || !ctx->log_cb
      || !(*ctx->log_cb) (ctx, ctx->log_cb_data, cat, NULL))
    return;

  saved_errno = errno;
  va_start (arg_ptr, format);
  res = gpgrt_vasprintf (&msg, format, arg_ptr);
  va_end (arg_ptr);
  if (res < 0)
    return;
  ctx->log_cb (ctx, ctx->log_cb_data, cat, msg);
  free (msg);
  gpg_err_set_errno (saved_errno);
}


/* Start a new debug line in *LINE, logged at level LEVEL or higher,
   and starting with the formatted string FORMAT.  */
void
_assuan_debug_begin (assuan_context_t ctx,
		     void **line, unsigned int cat, const char *format, ...)
{
  va_list arg_ptr;
  int res;

  *line = NULL;
  /* Probe if this wants to be logged based on category.  */
  if (! ctx
      || ! ctx->log_cb
      || ! (*ctx->log_cb) (ctx, ctx->log_cb_data, cat, NULL))
    return;

  va_start (arg_ptr, format);
  res = gpgrt_vasprintf ((char **) line, format, arg_ptr);
  va_end (arg_ptr);
  if (res < 0)
    *line = NULL;
}


/* Add the formatted string FORMAT to the debug line *LINE.  */
void
_assuan_debug_add (assuan_context_t ctx, void **line, const char *format, ...)
{
  va_list arg_ptr;
  char *toadd;
  char *result;
  int res;

  if (!*line)
    return;

  va_start (arg_ptr, format);
  res = gpgrt_vasprintf (&toadd, format, arg_ptr);
  va_end (arg_ptr);
  if (res < 0)
    {
      free (*line);
      *line = NULL;
    }
  res = gpgrt_asprintf (&result, "%s%s", *(char **) line, toadd);
  free (toadd);
  free (*line);
  if (res < 0)
    *line = NULL;
  else
    *line = result;
}


/* Finish construction of *LINE and send it to the debug output
   stream.  */
void
_assuan_debug_end (assuan_context_t ctx, void **line, unsigned int cat)
{
  if (!*line)
    return;

  /* Force logging here by using category ~0.  */
  _assuan_debug (ctx, ~0, "%s", *line);
  free (*line);
  *line = NULL;
}


#define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a'))

void
_assuan_debug_buffer (assuan_context_t ctx, unsigned int cat,
		      const char *const fmt, const char *const func,
		      const char *const tagname, void *tag,
		      const char *const buffer, size_t len)
{
  int idx = 0;
  int j;

  /* Probe if this wants to be logged based on category.  */
  if (!ctx
      || ! ctx->log_cb
      || ! (*ctx->log_cb) (ctx, ctx->log_cb_data, cat, NULL))
    return;

  while (idx < len)
    {
      char str[51];
      char *strp = str;
      char *strp2 = &str[34];

      for (j = 0; j < 16; j++)
	{
	  unsigned char val;
	  if (idx < len)
	    {
	      val = buffer[idx++];
	      *(strp++) = TOHEX (val >> 4);
	      *(strp++) = TOHEX (val % 16);
	      *(strp2++) = isprint (val) ? val : '.';
	    }
	  else
	    {
	      *(strp++) = ' ';
	      *(strp++) = ' ';
	    }
	  if (j == 7)
	    *(strp++) = ' ';
	}
      *(strp++) = ' ';
      *(strp2++) = '\n';
      *(strp2) = '\0';

      _assuan_debug (ctx, cat, fmt, func, tagname, tag, str);
    }
}