Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "test.h"
00024 #include "testutil.h"
00025 #include "testtrace.h"
00026 #include "memdebug.h"
00027
00028 struct libtest_trace_cfg libtest_debug_config;
00029
00030 static time_t epoch_offset;
00031 static int known_offset;
00032
00033 static
00034 void libtest_debug_dump(const char *timebuf, const char *text, FILE *stream,
00035 const unsigned char *ptr, size_t size, int nohex)
00036 {
00037 size_t i;
00038 size_t c;
00039
00040 unsigned int width = 0x10;
00041
00042 if(nohex)
00043
00044 width = 0x40;
00045
00046 fprintf(stream, "%s%s, %d bytes (0x%x)\n", timebuf, text,
00047 (int)size, (int)size);
00048
00049 for(i = 0; i < size; i += width) {
00050
00051 fprintf(stream, "%04x: ", (int)i);
00052
00053 if(!nohex) {
00054
00055 for(c = 0; c < width; c++)
00056 if(i+c < size)
00057 fprintf(stream, "%02x ", ptr[i+c]);
00058 else
00059 fputs(" ", stream);
00060 }
00061
00062 for(c = 0; (c < width) && (i+c < size); c++) {
00063
00064 if(nohex &&
00065 (i+c+1 < size) && (ptr[i+c] == 0x0D) && (ptr[i+c+1] == 0x0A)) {
00066 i += (c+2-width);
00067 break;
00068 }
00069 fprintf(stream, "%c", ((ptr[i+c] >= 0x20) && (ptr[i+c] < 0x80)) ?
00070 ptr[i+c] : '.');
00071
00072 if(nohex &&
00073 (i+c+2 < size) && (ptr[i+c+1] == 0x0D) && (ptr[i+c+2] == 0x0A)) {
00074 i += (c+3-width);
00075 break;
00076 }
00077 }
00078 fputc('\n', stream);
00079 }
00080 fflush(stream);
00081 }
00082
00083 int libtest_debug_cb(CURL *handle, curl_infotype type,
00084 unsigned char *data, size_t size,
00085 void *userp)
00086 {
00087
00088 struct libtest_trace_cfg *trace_cfg = userp;
00089 const char *text;
00090 struct timeval tv;
00091 struct tm *now;
00092 char timebuf[20];
00093 char *timestr;
00094 time_t secs;
00095
00096 (void)handle;
00097
00098 timebuf[0] = '\0';
00099 timestr = &timebuf[0];
00100
00101 if(trace_cfg->tracetime) {
00102 tv = tutil_tvnow();
00103 if(!known_offset) {
00104 epoch_offset = time(NULL) - tv.tv_sec;
00105 known_offset = 1;
00106 }
00107 secs = epoch_offset + tv.tv_sec;
00108 now = localtime(&secs);
00109 snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
00110 now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
00111 }
00112
00113 switch(type) {
00114 case CURLINFO_TEXT:
00115 fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
00116 default:
00117 return 0;
00118
00119 case CURLINFO_HEADER_OUT:
00120 text = "=> Send header";
00121 break;
00122 case CURLINFO_DATA_OUT:
00123 text = "=> Send data";
00124 break;
00125 case CURLINFO_SSL_DATA_OUT:
00126 text = "=> Send SSL data";
00127 break;
00128 case CURLINFO_HEADER_IN:
00129 text = "<= Recv header";
00130 break;
00131 case CURLINFO_DATA_IN:
00132 text = "<= Recv data";
00133 break;
00134 case CURLINFO_SSL_DATA_IN:
00135 text = "<= Recv SSL data";
00136 break;
00137 }
00138
00139 libtest_debug_dump(timebuf, text, stderr, data, size, trace_cfg->nohex);
00140 return 0;
00141 }
00142