curl_trc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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_trc.h"
  27. #include "urldata.h"
  28. #include "easyif.h"
  29. #include "cfilters.h"
  30. #include "multiif.h"
  31. #include "cf-socket.h"
  32. #include "connect.h"
  33. #include "doh.h"
  34. #include "http2.h"
  35. #include "http_proxy.h"
  36. #include "cf-h1-proxy.h"
  37. #include "cf-h2-proxy.h"
  38. #include "cf-haproxy.h"
  39. #include "cf-https-connect.h"
  40. #include "cf-ip-happy.h"
  41. #include "socks.h"
  42. #include "curlx/strparse.h"
  43. #include "vtls/vtls.h"
  44. #include "vquic/vquic.h"
  45. /* The last 2 #include files should be in this order */
  46. #include "curl_memory.h"
  47. #include "memdebug.h"
  48. static void trc_write(struct Curl_easy *data, curl_infotype type,
  49. const char *ptr, size_t size)
  50. {
  51. if(data->set.verbose) {
  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, CURL_UNCONST(ptr), size,
  56. data->set.debugdata);
  57. Curl_set_in_callback(data, inCallback);
  58. }
  59. else {
  60. static const char s_infotype[CURLINFO_END][3] = {
  61. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  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. /* max length we trace before ending in '...' */
  76. #define TRC_LINE_MAX 2048
  77. #define CURL_TRC_FMT_IDSC "[x-%" CURL_FORMAT_CURL_OFF_T "] "
  78. #define CURL_TRC_FMT_IDSD "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  79. #define CURL_TRC_FMT_IDSDC "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  80. CURL_FORMAT_CURL_OFF_T "] "
  81. static struct curl_trc_feat Curl_trc_feat_ids = {
  82. "LIB-IDS",
  83. CURL_LOG_LVL_NONE,
  84. };
  85. #define CURL_TRC_IDS(data) \
  86. (Curl_trc_is_verbose(data) && \
  87. Curl_trc_feat_ids.log_level >= CURL_LOG_LVL_INFO)
  88. static size_t trc_print_ids(struct Curl_easy *data, char *buf, size_t maxlen)
  89. {
  90. curl_off_t cid = data->conn ?
  91. data->conn->connection_id : data->state.recent_conn_id;
  92. if(data->id >= 0) {
  93. if(cid >= 0)
  94. return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSDC, data->id, cid);
  95. else
  96. return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSD, data->id);
  97. }
  98. else if(cid >= 0)
  99. return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSC, cid);
  100. else {
  101. return curl_msnprintf(buf, maxlen, "[x-x] ");
  102. }
  103. }
  104. static size_t trc_end_buf(char *buf, size_t len, size_t maxlen, bool addnl)
  105. {
  106. /* make sure we end the trace line in `buf` properly. It needs
  107. * to end with a terminating '\0' or '\n\0' */
  108. if(len >= (maxlen - (addnl ? 2 : 1))) {
  109. len = maxlen - 5;
  110. buf[len++] = '.';
  111. buf[len++] = '.';
  112. buf[len++] = '.';
  113. buf[len++] = '\n';
  114. }
  115. else if(addnl)
  116. buf[len++] = '\n';
  117. buf[len] = '\0';
  118. return len;
  119. }
  120. void Curl_debug(struct Curl_easy *data, curl_infotype type,
  121. const char *ptr, size_t size)
  122. {
  123. if(data->set.verbose) {
  124. static const char s_infotype[CURLINFO_END][3] = {
  125. "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
  126. char buf[TRC_LINE_MAX];
  127. size_t len;
  128. if(data->set.fdebug) {
  129. bool inCallback = Curl_is_in_callback(data);
  130. if(CURL_TRC_IDS(data) && (size < TRC_LINE_MAX)) {
  131. len = trc_print_ids(data, buf, TRC_LINE_MAX);
  132. len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "%.*s",
  133. (int)size, ptr);
  134. len = trc_end_buf(buf, len, TRC_LINE_MAX, FALSE);
  135. Curl_set_in_callback(data, TRUE);
  136. (void)(*data->set.fdebug)(data, type, buf, len, data->set.debugdata);
  137. Curl_set_in_callback(data, inCallback);
  138. }
  139. else {
  140. Curl_set_in_callback(data, TRUE);
  141. (void)(*data->set.fdebug)(data, type, CURL_UNCONST(ptr),
  142. size, data->set.debugdata);
  143. Curl_set_in_callback(data, inCallback);
  144. }
  145. }
  146. else {
  147. switch(type) {
  148. case CURLINFO_TEXT:
  149. case CURLINFO_HEADER_OUT:
  150. case CURLINFO_HEADER_IN:
  151. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  152. if(CURL_TRC_IDS(data)) {
  153. len = trc_print_ids(data, buf, TRC_LINE_MAX);
  154. fwrite(buf, len, 1, data->set.err);
  155. }
  156. #endif
  157. fwrite(s_infotype[type], 2, 1, data->set.err);
  158. fwrite(ptr, size, 1, data->set.err);
  159. break;
  160. default: /* nada */
  161. break;
  162. }
  163. }
  164. }
  165. }
  166. /* Curl_failf() is for messages stating why we failed.
  167. * The message SHALL NOT include any LF or CR.
  168. */
  169. void Curl_failf(struct Curl_easy *data, const char *fmt, ...)
  170. {
  171. DEBUGASSERT(!strchr(fmt, '\n'));
  172. if(data->set.verbose || data->set.errorbuffer) {
  173. va_list ap;
  174. size_t len;
  175. char error[CURL_ERROR_SIZE + 2];
  176. va_start(ap, fmt);
  177. len = curl_mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap);
  178. if(data->set.errorbuffer && !data->state.errorbuf) {
  179. strcpy(data->set.errorbuffer, error);
  180. data->state.errorbuf = TRUE; /* wrote error string */
  181. }
  182. error[len++] = '\n';
  183. error[len] = '\0';
  184. trc_write(data, CURLINFO_TEXT, error, len);
  185. va_end(ap);
  186. }
  187. }
  188. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  189. static void trc_infof(struct Curl_easy *data,
  190. struct curl_trc_feat *feat,
  191. const char *opt_id, int opt_id_idx,
  192. const char * const fmt, va_list ap) CURL_PRINTF(5, 0);
  193. static void trc_infof(struct Curl_easy *data,
  194. struct curl_trc_feat *feat,
  195. const char *opt_id, int opt_id_idx,
  196. const char * const fmt, va_list ap)
  197. {
  198. size_t len = 0;
  199. char buf[TRC_LINE_MAX];
  200. if(CURL_TRC_IDS(data))
  201. len += trc_print_ids(data, buf + len, TRC_LINE_MAX - len);
  202. if(feat)
  203. len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s] ", feat->name);
  204. if(opt_id) {
  205. if(opt_id_idx > 0)
  206. len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s-%d] ",
  207. opt_id, opt_id_idx);
  208. else
  209. len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s] ", opt_id);
  210. }
  211. len += curl_mvsnprintf(buf + len, TRC_LINE_MAX - len, fmt, ap);
  212. len = trc_end_buf(buf, len, TRC_LINE_MAX, TRUE);
  213. trc_write(data, CURLINFO_TEXT, buf, len);
  214. }
  215. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  216. {
  217. DEBUGASSERT(!strchr(fmt, '\n'));
  218. if(Curl_trc_is_verbose(data)) {
  219. va_list ap;
  220. va_start(ap, fmt);
  221. trc_infof(data, data->state.feat, NULL, 0, fmt, ap);
  222. va_end(ap);
  223. }
  224. }
  225. void Curl_trc_cf_infof(struct Curl_easy *data, const struct Curl_cfilter *cf,
  226. const char *fmt, ...)
  227. {
  228. DEBUGASSERT(cf);
  229. if(Curl_trc_cf_is_verbose(cf, data)) {
  230. va_list ap;
  231. va_start(ap, fmt);
  232. trc_infof(data, data->state.feat, cf->cft->name, cf->sockindex, fmt, ap);
  233. va_end(ap);
  234. }
  235. }
  236. struct curl_trc_feat Curl_trc_feat_multi = {
  237. "MULTI",
  238. CURL_LOG_LVL_NONE,
  239. };
  240. struct curl_trc_feat Curl_trc_feat_read = {
  241. "READ",
  242. CURL_LOG_LVL_NONE,
  243. };
  244. struct curl_trc_feat Curl_trc_feat_write = {
  245. "WRITE",
  246. CURL_LOG_LVL_NONE,
  247. };
  248. struct curl_trc_feat Curl_trc_feat_dns = {
  249. "DNS",
  250. CURL_LOG_LVL_NONE,
  251. };
  252. struct curl_trc_feat Curl_trc_feat_timer = {
  253. "TIMER",
  254. CURL_LOG_LVL_NONE,
  255. };
  256. static const char * const Curl_trc_timer_names[]={
  257. "100_TIMEOUT",
  258. "ASYNC_NAME",
  259. "CONNECTTIMEOUT",
  260. "DNS_PER_NAME",
  261. "DNS_PER_NAME2",
  262. "HAPPY_EYEBALLS_DNS",
  263. "HAPPY_EYEBALLS",
  264. "MULTI_PENDING",
  265. "SPEEDCHECK",
  266. "TIMEOUT",
  267. "TOOFAST",
  268. "QUIC",
  269. "FTP_ACCEPT",
  270. "ALPN_EYEBALLS",
  271. "SHUTDOWN",
  272. };
  273. static const char *trc_timer_name(int tid)
  274. {
  275. if((tid >= 0) && ((size_t)tid < CURL_ARRAYSIZE(Curl_trc_timer_names)))
  276. return Curl_trc_timer_names[(size_t)tid];
  277. return "UNKNOWN?";
  278. }
  279. void Curl_trc_timer(struct Curl_easy *data, int tid, const char *fmt, ...)
  280. {
  281. DEBUGASSERT(!strchr(fmt, '\n'));
  282. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_timer)) {
  283. const char *tname = trc_timer_name(tid);
  284. va_list ap;
  285. va_start(ap, fmt);
  286. trc_infof(data, &Curl_trc_feat_timer, tname, 0, fmt, ap);
  287. va_end(ap);
  288. }
  289. }
  290. void Curl_trc_easy_timers(struct Curl_easy *data)
  291. {
  292. if(CURL_TRC_TIMER_is_verbose(data)) {
  293. struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist);
  294. if(e) {
  295. struct curltime now = curlx_now();
  296. while(e) {
  297. struct time_node *n = Curl_node_elem(e);
  298. e = Curl_node_next(e);
  299. CURL_TRC_TIMER(data, n->eid, "expires in %" FMT_TIMEDIFF_T "ns",
  300. curlx_timediff_us(n->time, now));
  301. }
  302. }
  303. }
  304. }
  305. static const char * const Curl_trc_mstate_names[]={
  306. "INIT",
  307. "PENDING",
  308. "SETUP",
  309. "CONNECT",
  310. "RESOLVING",
  311. "CONNECTING",
  312. "TUNNELING",
  313. "PROTOCONNECT",
  314. "PROTOCONNECTING",
  315. "DO",
  316. "DOING",
  317. "DOING_MORE",
  318. "DID",
  319. "PERFORMING",
  320. "RATELIMITING",
  321. "DONE",
  322. "COMPLETED",
  323. "MSGSENT",
  324. };
  325. const char *Curl_trc_mstate_name(int state)
  326. {
  327. if((state >= 0) && ((size_t)state < CURL_ARRAYSIZE(Curl_trc_mstate_names)))
  328. return Curl_trc_mstate_names[(size_t)state];
  329. return "?";
  330. }
  331. void Curl_trc_multi(struct Curl_easy *data, const char *fmt, ...)
  332. {
  333. DEBUGASSERT(!strchr(fmt, '\n'));
  334. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_multi)) {
  335. const char *sname = (data->id >= 0) ?
  336. Curl_trc_mstate_name(data->mstate) : NULL;
  337. va_list ap;
  338. va_start(ap, fmt);
  339. trc_infof(data, &Curl_trc_feat_multi, sname, 0, fmt, ap);
  340. va_end(ap);
  341. }
  342. }
  343. void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...)
  344. {
  345. DEBUGASSERT(!strchr(fmt, '\n'));
  346. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_read)) {
  347. va_list ap;
  348. va_start(ap, fmt);
  349. trc_infof(data, &Curl_trc_feat_read, NULL, 0, fmt, ap);
  350. va_end(ap);
  351. }
  352. }
  353. void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...)
  354. {
  355. DEBUGASSERT(!strchr(fmt, '\n'));
  356. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_write)) {
  357. va_list ap;
  358. va_start(ap, fmt);
  359. trc_infof(data, &Curl_trc_feat_write, NULL, 0, fmt, ap);
  360. va_end(ap);
  361. }
  362. }
  363. void Curl_trc_dns(struct Curl_easy *data, const char *fmt, ...)
  364. {
  365. DEBUGASSERT(!strchr(fmt, '\n'));
  366. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_dns)) {
  367. va_list ap;
  368. va_start(ap, fmt);
  369. trc_infof(data, &Curl_trc_feat_dns, NULL, 0, fmt, ap);
  370. va_end(ap);
  371. }
  372. }
  373. #ifndef CURL_DISABLE_FTP
  374. struct curl_trc_feat Curl_trc_feat_ftp = {
  375. "FTP",
  376. CURL_LOG_LVL_NONE,
  377. };
  378. void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
  379. {
  380. DEBUGASSERT(!strchr(fmt, '\n'));
  381. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ftp)) {
  382. va_list ap;
  383. va_start(ap, fmt);
  384. trc_infof(data, &Curl_trc_feat_ftp, NULL, 0, fmt, ap);
  385. va_end(ap);
  386. }
  387. }
  388. #endif /* !CURL_DISABLE_FTP */
  389. #ifndef CURL_DISABLE_SMTP
  390. struct curl_trc_feat Curl_trc_feat_smtp = {
  391. "SMTP",
  392. CURL_LOG_LVL_NONE,
  393. };
  394. void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...)
  395. {
  396. DEBUGASSERT(!strchr(fmt, '\n'));
  397. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_smtp)) {
  398. va_list ap;
  399. va_start(ap, fmt);
  400. trc_infof(data, &Curl_trc_feat_smtp, NULL, 0, fmt, ap);
  401. va_end(ap);
  402. }
  403. }
  404. #endif /* !CURL_DISABLE_SMTP */
  405. #ifdef USE_SSL
  406. struct curl_trc_feat Curl_trc_feat_ssls = {
  407. "SSLS",
  408. CURL_LOG_LVL_NONE,
  409. };
  410. void Curl_trc_ssls(struct Curl_easy *data, const char *fmt, ...)
  411. {
  412. DEBUGASSERT(!strchr(fmt, '\n'));
  413. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ssls)) {
  414. va_list ap;
  415. va_start(ap, fmt);
  416. trc_infof(data, &Curl_trc_feat_ssls, NULL, 0, fmt, ap);
  417. va_end(ap);
  418. }
  419. }
  420. #endif /* USE_SSL */
  421. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  422. struct curl_trc_feat Curl_trc_feat_ws = {
  423. "WS",
  424. CURL_LOG_LVL_NONE,
  425. };
  426. void Curl_trc_ws(struct Curl_easy *data, const char *fmt, ...)
  427. {
  428. DEBUGASSERT(!strchr(fmt, '\n'));
  429. if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ws)) {
  430. va_list ap;
  431. va_start(ap, fmt);
  432. trc_infof(data, &Curl_trc_feat_ws, NULL, 0, fmt, ap);
  433. va_end(ap);
  434. }
  435. }
  436. #endif /* !CURL_DISABLE_WEBSOCKETS && !CURL_DISABLE_HTTP */
  437. #define TRC_CT_NONE (0)
  438. #define TRC_CT_PROTOCOL (1<<(0))
  439. #define TRC_CT_NETWORK (1<<(1))
  440. #define TRC_CT_PROXY (1<<(2))
  441. #define TRC_CT_INTERNALS (1<<(3))
  442. struct trc_feat_def {
  443. struct curl_trc_feat *feat;
  444. unsigned int category;
  445. };
  446. static struct trc_feat_def trc_feats[] = {
  447. { &Curl_trc_feat_ids, TRC_CT_INTERNALS },
  448. { &Curl_trc_feat_multi, TRC_CT_NETWORK },
  449. { &Curl_trc_feat_read, TRC_CT_NONE },
  450. { &Curl_trc_feat_write, TRC_CT_NONE },
  451. { &Curl_trc_feat_dns, TRC_CT_NETWORK },
  452. { &Curl_trc_feat_timer, TRC_CT_NETWORK },
  453. #ifndef CURL_DISABLE_FTP
  454. { &Curl_trc_feat_ftp, TRC_CT_PROTOCOL },
  455. #endif
  456. #ifndef CURL_DISABLE_DOH
  457. #endif
  458. #ifndef CURL_DISABLE_SMTP
  459. { &Curl_trc_feat_smtp, TRC_CT_PROTOCOL },
  460. #endif
  461. #ifdef USE_SSL
  462. { &Curl_trc_feat_ssls, TRC_CT_NETWORK },
  463. #endif
  464. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  465. { &Curl_trc_feat_ws, TRC_CT_PROTOCOL },
  466. #endif
  467. };
  468. struct trc_cft_def {
  469. struct Curl_cftype *cft;
  470. unsigned int category;
  471. };
  472. static struct trc_cft_def trc_cfts[] = {
  473. { &Curl_cft_tcp, TRC_CT_NETWORK },
  474. { &Curl_cft_udp, TRC_CT_NETWORK },
  475. { &Curl_cft_unix, TRC_CT_NETWORK },
  476. { &Curl_cft_tcp_accept, TRC_CT_NETWORK },
  477. { &Curl_cft_ip_happy, TRC_CT_NETWORK },
  478. { &Curl_cft_setup, TRC_CT_PROTOCOL },
  479. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NGHTTP2)
  480. { &Curl_cft_nghttp2, TRC_CT_PROTOCOL },
  481. #endif
  482. #ifdef USE_SSL
  483. { &Curl_cft_ssl, TRC_CT_NETWORK },
  484. #ifndef CURL_DISABLE_PROXY
  485. { &Curl_cft_ssl_proxy, TRC_CT_PROXY },
  486. #endif
  487. #endif
  488. #ifndef CURL_DISABLE_PROXY
  489. #ifndef CURL_DISABLE_HTTP
  490. { &Curl_cft_h1_proxy, TRC_CT_PROXY },
  491. #ifdef USE_NGHTTP2
  492. { &Curl_cft_h2_proxy, TRC_CT_PROXY },
  493. #endif
  494. { &Curl_cft_http_proxy, TRC_CT_PROXY },
  495. #endif /* !CURL_DISABLE_HTTP */
  496. { &Curl_cft_haproxy, TRC_CT_PROXY },
  497. { &Curl_cft_socks_proxy, TRC_CT_PROXY },
  498. #endif /* !CURL_DISABLE_PROXY */
  499. #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3)
  500. { &Curl_cft_http3, TRC_CT_PROTOCOL },
  501. #endif
  502. #ifndef CURL_DISABLE_HTTP
  503. { &Curl_cft_http_connect, TRC_CT_PROTOCOL },
  504. #endif
  505. };
  506. static void trc_apply_level_by_name(struct Curl_str *token, int lvl)
  507. {
  508. size_t i;
  509. for(i = 0; i < CURL_ARRAYSIZE(trc_cfts); ++i) {
  510. if(curlx_str_casecompare(token, trc_cfts[i].cft->name)) {
  511. trc_cfts[i].cft->log_level = lvl;
  512. break;
  513. }
  514. }
  515. for(i = 0; i < CURL_ARRAYSIZE(trc_feats); ++i) {
  516. if(curlx_str_casecompare(token, trc_feats[i].feat->name)) {
  517. trc_feats[i].feat->log_level = lvl;
  518. break;
  519. }
  520. }
  521. }
  522. static void trc_apply_level_by_category(int category, int lvl)
  523. {
  524. size_t i;
  525. for(i = 0; i < CURL_ARRAYSIZE(trc_cfts); ++i) {
  526. if(!category || (trc_cfts[i].category & category))
  527. trc_cfts[i].cft->log_level = lvl;
  528. }
  529. for(i = 0; i < CURL_ARRAYSIZE(trc_feats); ++i) {
  530. if(!category || (trc_feats[i].category & category))
  531. trc_feats[i].feat->log_level = lvl;
  532. }
  533. }
  534. static CURLcode trc_opt(const char *config)
  535. {
  536. struct Curl_str out;
  537. while(!curlx_str_until(&config, &out, 32, ',')) {
  538. int lvl = CURL_LOG_LVL_INFO;
  539. const char *token = curlx_str(&out);
  540. if(*token == '-') {
  541. lvl = CURL_LOG_LVL_NONE;
  542. curlx_str_nudge(&out, 1);
  543. }
  544. else if(*token == '+')
  545. curlx_str_nudge(&out, 1);
  546. if(curlx_str_casecompare(&out, "all"))
  547. trc_apply_level_by_category(TRC_CT_NONE, lvl);
  548. else if(curlx_str_casecompare(&out, "protocol"))
  549. trc_apply_level_by_category(TRC_CT_PROTOCOL, lvl);
  550. else if(curlx_str_casecompare(&out, "network"))
  551. trc_apply_level_by_category(TRC_CT_NETWORK, lvl);
  552. else if(curlx_str_casecompare(&out, "proxy"))
  553. trc_apply_level_by_category(TRC_CT_PROXY, lvl);
  554. else if(curlx_str_casecompare(&out, "doh")) {
  555. struct Curl_str dns = { "dns", 3 };
  556. trc_apply_level_by_name(&dns, lvl);
  557. }
  558. else
  559. trc_apply_level_by_name(&out, lvl);
  560. if(curlx_str_single(&config, ','))
  561. break;
  562. }
  563. return CURLE_OK;
  564. }
  565. CURLcode Curl_trc_opt(const char *config)
  566. {
  567. CURLcode result = config ? trc_opt(config) : CURLE_OK;
  568. #ifdef DEBUGBUILD
  569. /* CURL_DEBUG can override anything */
  570. if(!result) {
  571. const char *dbg_config = getenv("CURL_DEBUG");
  572. if(dbg_config)
  573. result = trc_opt(dbg_config);
  574. }
  575. #endif /* DEBUGBUILD */
  576. return result;
  577. }
  578. CURLcode Curl_trc_init(void)
  579. {
  580. #ifdef DEBUGBUILD
  581. return Curl_trc_opt(NULL);
  582. #else
  583. return CURLE_OK;
  584. #endif
  585. }
  586. #else /* CURL_DISABLE_VERBOSE_STRINGS */
  587. CURLcode Curl_trc_init(void)
  588. {
  589. return CURLE_OK;
  590. }
  591. void Curl_infof(struct Curl_easy *data, const char *fmt, ...)
  592. {
  593. (void)data; (void)fmt;
  594. }
  595. void Curl_trc_cf_infof(struct Curl_easy *data, const struct Curl_cfilter *cf,
  596. const char *fmt, ...)
  597. {
  598. (void)data; (void)cf; (void)fmt;
  599. }
  600. void Curl_trc_multi(struct Curl_easy *data, const char *fmt, ...)
  601. {
  602. (void)data; (void)fmt;
  603. }
  604. void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...)
  605. {
  606. (void)data; (void)fmt;
  607. }
  608. void Curl_trc_dns(struct Curl_easy *data, const char *fmt, ...)
  609. {
  610. (void)data; (void)fmt;
  611. }
  612. void Curl_trc_timer(struct Curl_easy *data, int tid, const char *fmt, ...)
  613. {
  614. (void)data; (void)tid; (void)fmt;
  615. }
  616. void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...)
  617. {
  618. (void)data; (void)fmt;
  619. }
  620. #ifndef CURL_DISABLE_FTP
  621. void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
  622. {
  623. (void)data; (void)fmt;
  624. }
  625. #endif
  626. #ifndef CURL_DISABLE_SMTP
  627. void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...)
  628. {
  629. (void)data; (void)fmt;
  630. }
  631. #endif
  632. #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
  633. void Curl_trc_ws(struct Curl_easy *data, const char *fmt, ...)
  634. {
  635. (void)data; (void)fmt;
  636. }
  637. #endif
  638. #ifdef USE_SSL
  639. void Curl_trc_ssls(struct Curl_easy *data, const char *fmt, ...)
  640. {
  641. (void)data;
  642. (void)fmt;
  643. }
  644. #endif
  645. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */