curl_log.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "curl_log.h"
  27. #include "urldata.h"
  28. #include "easyif.h"
  29. #include "cfilters.h"
  30. #include "timeval.h"
  31. #include "multiif.h"
  32. #include "strcase.h"
  33. #include "cf-socket.h"
  34. #include "connect.h"
  35. #include "http2.h"
  36. #include "http_proxy.h"
  37. #include "cf-h1-proxy.h"
  38. #include "cf-h2-proxy.h"
  39. #include "cf-haproxy.h"
  40. #include "cf-https-connect.h"
  41. #include "socks.h"
  42. #include "strtok.h"
  43. #include "vtls/vtls.h"
  44. #include "vquic/vquic.h"
  45. /* The last 3 #include files should be in this order */
  46. #include "curl_printf.h"
  47. #include "curl_memory.h"
  48. #include "memdebug.h"
  49. void Curl_debug(struct Curl_easy *data, curl_infotype type,
  50. char *ptr, size_t size)
  51. {
  52. if(data->set.verbose) {
  53. static const char s_infotype[CURLINFO_END][3] = {
  54. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  55. if(data->set.fdebug) {
  56. bool inCallback = Curl_is_in_callback(data);
  57. Curl_set_in_callback(data, true);
  58. (void)(*data->set.fdebug)(data, type, ptr, size, data->set.debugdata);
  59. Curl_set_in_callback(data, inCallback);
  60. }
  61. else {
  62. switch(type) {
  63. case CURLINFO_TEXT:
  64. case CURLINFO_HEADER_OUT:
  65. case CURLINFO_HEADER_IN:
  66. fwrite(s_infotype[type], 2, 1, data->set.err);
  67. fwrite(ptr, size, 1, data->set.err);
  68. break;
  69. default: /* nada */
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. /* Curl_failf() is for messages stating why we failed.
  76. * The message SHALL NOT include any LF or CR.
  77. */
  78. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  79. {
  80. DEBUGASSERT(!strchr(fmt, '\n'));
  81. if(data->set.verbose || data->set.errorbuffer) {
  82. va_list ap;
  83. int len;
  84. char error[CURL_ERROR_SIZE + 2];
  85. va_start(ap, fmt);
  86. len = mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  87. if(data->set.errorbuffer && !data->state.errorbuf) {
  88. strcpy(data->set.errorbuffer, error);
  89. data->state.errorbuf = TRUE; /* wrote error string */
  90. }
  91. error[len++] = '\n';
  92. error[len] = '\0';
  93. Curl_debug(data, CURLINFO_TEXT, error, len);
  94. va_end(ap);
  95. }
  96. }
  97. /* Curl_infof() is for info message along the way */
  98. #define MAXINFO 2048
  99. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  100. {
  101. DEBUGASSERT(!strchr(fmt, '\n'));
  102. if(data && data->set.verbose) {
  103. va_list ap;
  104. int len;
  105. char buffer[MAXINFO + 2];
  106. va_start(ap, fmt);
  107. len = mvsnprintf(buffer, MAXINFO, fmt, ap);
  108. va_end(ap);
  109. buffer[len++] = '\n';
  110. buffer[len] = '\0';
  111. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  112. }
  113. }
  114. #ifdef DEBUGBUILD
  115. void Curl_log_cf_debug(struct Curl_easy *data, struct Curl_cfilter *cf,
  116. const char *fmt, ...)
  117. {
  118. DEBUGASSERT(cf);
  119. if(data && Curl_log_cf_is_debug(cf, data)) {
  120. va_list ap;
  121. int len;
  122. char buffer[MAXINFO + 2];
  123. len = msnprintf(buffer, MAXINFO, "[%s] ", cf->cft->name);
  124. va_start(ap, fmt);
  125. len += mvsnprintf(buffer + len, MAXINFO - len, fmt, ap);
  126. va_end(ap);
  127. buffer[len++] = '\n';
  128. buffer[len] = '\0';
  129. Curl_debug(data, CURLINFO_TEXT, buffer, len);
  130. }
  131. }
  132. static struct Curl_cftype *cf_types[] = {
  133. &Curl_cft_tcp,
  134. &Curl_cft_udp,
  135. &Curl_cft_unix,
  136. &Curl_cft_tcp_accept,
  137. &Curl_cft_happy_eyeballs,
  138. &Curl_cft_setup,
  139. #ifdef USE_NGHTTP2
  140. &Curl_cft_nghttp2,
  141. #endif
  142. #ifdef USE_SSL
  143. &Curl_cft_ssl,
  144. &Curl_cft_ssl_proxy,
  145. #endif
  146. #if !defined(CURL_DISABLE_PROXY)
  147. #if !defined(CURL_DISABLE_HTTP)
  148. &Curl_cft_h1_proxy,
  149. #ifdef USE_NGHTTP2
  150. &Curl_cft_h2_proxy,
  151. #endif
  152. &Curl_cft_http_proxy,
  153. #endif /* !CURL_DISABLE_HTTP */
  154. &Curl_cft_haproxy,
  155. &Curl_cft_socks_proxy,
  156. #endif /* !CURL_DISABLE_PROXY */
  157. #ifdef ENABLE_QUIC
  158. &Curl_cft_http3,
  159. #endif
  160. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  161. &Curl_cft_http_connect,
  162. #endif
  163. NULL,
  164. };
  165. #ifndef ARRAYSIZE
  166. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  167. #endif
  168. CURLcode Curl_log_init(void)
  169. {
  170. const char *setting = getenv("CURL_DEBUG");
  171. if(setting) {
  172. char *token, *tok_buf, *tmp;
  173. size_t i;
  174. tmp = strdup(setting);
  175. if(!tmp)
  176. return CURLE_OUT_OF_MEMORY;
  177. token = strtok_r(tmp, ", ", &tok_buf);
  178. while(token) {
  179. for(i = 0; cf_types[i]; ++i) {
  180. if(strcasecompare(token, cf_types[i]->name)) {
  181. cf_types[i]->log_level = CURL_LOG_DEBUG;
  182. break;
  183. }
  184. }
  185. token = strtok_r(NULL, ", ", &tok_buf);
  186. }
  187. free(tmp);
  188. }
  189. return CURLE_OK;
  190. }
  191. #else /* DEBUGBUILD */
  192. CURLcode Curl_log_init(void)
  193. {
  194. return CURLE_OK;
  195. }
  196. #if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
  197. void Curl_log_cf_debug(struct Curl_easy *data, struct Curl_cfilter *cf,
  198. const char *fmt, ...)
  199. {
  200. (void)data;
  201. (void)cf;
  202. (void)fmt;
  203. }
  204. #endif
  205. #endif /* !DEBUGBUILD */