1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #include "testtrace.h"
28 struct libtest_trace_cfg libtest_debug_config;
30 static time_t epoch_offset; /* for test time tracing */
31 static int known_offset; /* for test time tracing */
34 void libtest_debug_dump(const char *timebuf, const char *text, FILE *stream,
35 const unsigned char *ptr, size_t size, int nohex)
40 unsigned int width = 0x10;
43 /* without the hex output, we can fit more on screen */
46 fprintf(stream, "%s%s, %d bytes (0x%x)\n", timebuf, text,
47 (int)size, (int)size);
49 for(i = 0; i < size; i += width) {
51 fprintf(stream, "%04x: ", (int)i);
54 /* hex not disabled, show it */
55 for(c = 0; c < width; c++)
57 fprintf(stream, "%02x ", ptr[i+c]);
62 for(c = 0; (c < width) && (i+c < size); c++) {
63 /* check for 0D0A; if found, skip past and start a new line of output */
65 (i+c+1 < size) && (ptr[i+c] == 0x0D) && (ptr[i+c+1] == 0x0A)) {
69 fprintf(stream, "%c", ((ptr[i+c] >= 0x20) && (ptr[i+c] < 0x80)) ?
71 /* check again for 0D0A, to avoid an extra \n if it's at width */
73 (i+c+2 < size) && (ptr[i+c+1] == 0x0D) && (ptr[i+c+2] == 0x0A)) {
78 fputc('\n', stream); /* newline */
83 int libtest_debug_cb(CURL *handle, curl_infotype type,
84 unsigned char *data, size_t size,
88 struct libtest_trace_cfg *trace_cfg = userp;
99 timestr = &timebuf[0];
101 if(trace_cfg->tracetime) {
104 epoch_offset = time(NULL) - tv.tv_sec;
107 secs = epoch_offset + tv.tv_sec;
108 now = localtime(&secs); /* not thread safe but we don't care */
109 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
110 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
115 fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
116 default: /* in case a new one is introduced to shock us */
119 case CURLINFO_HEADER_OUT:
120 text = "=> Send header";
122 case CURLINFO_DATA_OUT:
123 text = "=> Send data";
125 case CURLINFO_SSL_DATA_OUT:
126 text = "=> Send SSL data";
128 case CURLINFO_HEADER_IN:
129 text = "<= Recv header";
131 case CURLINFO_DATA_IN:
132 text = "<= Recv data";
134 case CURLINFO_SSL_DATA_IN:
135 text = "<= Recv SSL data";
139 libtest_debug_dump(timebuf, text, stderr, data, size, trace_cfg->nohex);