backend_socket_log.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <assert.h>
  2. #include <string.h>
  3. #include "putty.h"
  4. #include "network.h"
  5. void backend_socket_log(Seat *seat, LogContext *logctx,
  6. PlugLogType type, SockAddr *addr, int port,
  7. const char *error_msg, int error_code, Conf *conf,
  8. bool session_started)
  9. {
  10. char addrbuf[256], *msg;
  11. switch (type) {
  12. case PLUGLOG_CONNECT_TRYING:
  13. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  14. if (sk_addr_needs_port(addr)) {
  15. msg = dupprintf("Connecting to %s port %d", addrbuf, port);
  16. } else {
  17. msg = dupprintf("Connecting to %s", addrbuf);
  18. }
  19. break;
  20. case PLUGLOG_CONNECT_FAILED:
  21. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  22. msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
  23. break;
  24. case PLUGLOG_CONNECT_SUCCESS:
  25. if (addr)
  26. sk_getaddr(addr, addrbuf, lenof(addrbuf));
  27. else /* fallback if address unavailable */
  28. sprintf(addrbuf, "remote host");
  29. msg = dupprintf("Connected to %s", addrbuf);
  30. break;
  31. case PLUGLOG_PROXY_MSG: {
  32. /* Proxy-related log messages have their own identifying
  33. * prefix already, put on by our caller. */
  34. int len, log_to_term;
  35. /* Suffix \r\n temporarily, so we can log to the terminal. */
  36. msg = dupprintf("%s\r\n", error_msg);
  37. len = strlen(msg);
  38. assert(len >= 2);
  39. log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
  40. if (log_to_term == AUTO)
  41. log_to_term = session_started ? FORCE_OFF : FORCE_ON;
  42. if (log_to_term == FORCE_ON)
  43. seat_stderr(seat, msg, len);
  44. msg[len-2] = '\0'; /* remove the \r\n again */
  45. break;
  46. }
  47. default:
  48. msg = NULL; /* shouldn't happen, but placate optimiser */
  49. break;
  50. }
  51. if (msg) {
  52. logevent(logctx, msg);
  53. sfree(msg);
  54. }
  55. }