testtrace.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
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; /* for test time tracing */
00031 static int    known_offset; /* for test time tracing */
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     /* without the hex output, we can fit more on screen */
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       /* hex not disabled, show it */
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       /* check for 0D0A; if found, skip past and start a new line of output */
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       /* check again for 0D0A, to avoid an extra \n if it's at width */
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); /* newline */
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);  /* not thread safe but we don't care */
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: /* in case a new one is introduced to shock us */
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 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06