testtrace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. #define _MPRINTF_REPLACE /* use our functions only */
  24. #include <curl/mprintf.h>
  25. #include "testutil.h"
  26. #include "testtrace.h"
  27. #include "memdebug.h"
  28. struct libtest_trace_cfg libtest_debug_config;
  29. static time_t epoch_offset; /* for test time tracing */
  30. static int known_offset; /* for test time tracing */
  31. static
  32. void libtest_debug_dump(const char *timebuf, const char *text, FILE *stream,
  33. const unsigned char *ptr, size_t size, int nohex)
  34. {
  35. size_t i;
  36. size_t c;
  37. unsigned int width = 0x10;
  38. if(nohex)
  39. /* without the hex output, we can fit more on screen */
  40. width = 0x40;
  41. fprintf(stream, "%s%s, %d bytes (0x%x)\n", timebuf, text,
  42. (int)size, (int)size);
  43. for(i = 0; i < size; i += width) {
  44. fprintf(stream, "%04x: ", (int)i);
  45. if(!nohex) {
  46. /* hex not disabled, show it */
  47. for(c = 0; c < width; c++)
  48. if(i+c < size)
  49. fprintf(stream, "%02x ", ptr[i+c]);
  50. else
  51. fputs(" ", stream);
  52. }
  53. for(c = 0; (c < width) && (i+c < size); c++) {
  54. /* check for 0D0A; if found, skip past and start a new line of output */
  55. if(nohex &&
  56. (i+c+1 < size) && (ptr[i+c] == 0x0D) && (ptr[i+c+1] == 0x0A)) {
  57. i += (c+2-width);
  58. break;
  59. }
  60. fprintf(stream, "%c", ((ptr[i+c] >= 0x20) && (ptr[i+c] < 0x80)) ?
  61. ptr[i+c] : '.');
  62. /* check again for 0D0A, to avoid an extra \n if it's at width */
  63. if(nohex &&
  64. (i+c+2 < size) && (ptr[i+c+1] == 0x0D) && (ptr[i+c+2] == 0x0A)) {
  65. i += (c+3-width);
  66. break;
  67. }
  68. }
  69. fputc('\n', stream); /* newline */
  70. }
  71. fflush(stream);
  72. }
  73. int libtest_debug_cb(CURL *handle, curl_infotype type,
  74. unsigned char *data, size_t size,
  75. void *userp)
  76. {
  77. struct libtest_trace_cfg *trace_cfg = userp;
  78. const char *text;
  79. struct timeval tv;
  80. struct tm *now;
  81. char timebuf[20];
  82. char *timestr;
  83. time_t secs;
  84. (void)handle;
  85. timebuf[0] = '\0';
  86. timestr = &timebuf[0];
  87. if(trace_cfg->tracetime) {
  88. tv = tutil_tvnow();
  89. if(!known_offset) {
  90. epoch_offset = time(NULL) - tv.tv_sec;
  91. known_offset = 1;
  92. }
  93. secs = epoch_offset + tv.tv_sec;
  94. now = localtime(&secs); /* not thread safe but we don't care */
  95. snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d.%06ld ",
  96. now->tm_hour, now->tm_min, now->tm_sec, (long)tv.tv_usec);
  97. }
  98. switch (type) {
  99. case CURLINFO_TEXT:
  100. fprintf(stderr, "%s== Info: %s", timestr, (char *)data);
  101. default: /* in case a new one is introduced to shock us */
  102. return 0;
  103. case CURLINFO_HEADER_OUT:
  104. text = "=> Send header";
  105. break;
  106. case CURLINFO_DATA_OUT:
  107. text = "=> Send data";
  108. break;
  109. case CURLINFO_SSL_DATA_OUT:
  110. text = "=> Send SSL data";
  111. break;
  112. case CURLINFO_HEADER_IN:
  113. text = "<= Recv header";
  114. break;
  115. case CURLINFO_DATA_IN:
  116. text = "<= Recv data";
  117. break;
  118. case CURLINFO_SSL_DATA_IN:
  119. text = "<= Recv SSL data";
  120. break;
  121. }
  122. libtest_debug_dump(timebuf, text, stderr, data, size, trace_cfg->nohex);
  123. return 0;
  124. }