curl_log.c 5.6 KB

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