log.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* log.c -- Guile-SSH logging procedures
  2. *
  3. * Copyright (C) 2014, 2015, 2016, 2017 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  4. *
  5. * This file is part of Guile-SSH
  6. *
  7. * Guile-SSH is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * Guile-SSH is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdarg.h>
  22. #include <libguile.h>
  23. #include <libssh/libssh.h>
  24. #include <libssh/callbacks.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #include <stdio.h> /* DEBUG */
  28. #include <unistd.h> /* DEBUG */
  29. #include "error.h"
  30. #include "common.h"
  31. /* Log verbosity levels used by libssh sessions and servers. */
  32. struct symbol_mapping log_verbosity[] = {
  33. /* 0, No logging at all */
  34. { "nolog", SSH_LOG_NOLOG },
  35. /* 1, Only rare and noteworthy events */
  36. { "rare", SSH_LOG_RARE },
  37. /* 2, High level protocol information */
  38. { "protocol", SSH_LOG_PROTOCOL },
  39. /* 3, Lower level protocol infomations, packet level */
  40. { "packet", SSH_LOG_PACKET },
  41. /* 4, Every function path */
  42. { "functions", SSH_LOG_FUNCTIONS },
  43. { NULL, -1 }
  44. };
  45. /* Whether the default calback was set or not. */
  46. static int is_logging_callback_set = 0;
  47. /* A Scheme log printer. */
  48. static SCM logging_callback = SCM_BOOL_F;
  49. /* The libssh logging callback which calls Scheme callback procedure. */
  50. void
  51. libssh_logging_callback (int c_priority,
  52. const char *c_function_name,
  53. const char *c_message,
  54. void *c_userdata)
  55. {
  56. SCM priority = scm_from_int (c_priority);
  57. SCM function = scm_from_locale_string (c_function_name);
  58. SCM message = scm_from_locale_string (c_message);
  59. SCM userdata = (SCM) c_userdata;
  60. scm_call_4 (logging_callback, priority, function, message, userdata);
  61. }
  62. #define TBUF_SZ 64
  63. static int
  64. _get_current_timestring (char *buf, size_t len)
  65. {
  66. char tbuf[TBUF_SZ];
  67. struct timeval tv;
  68. struct tm *tm;
  69. time_t t;
  70. gettimeofday (&tv, NULL);
  71. t = (time_t) tv.tv_sec;
  72. tm = localtime (&t);
  73. if (tm == NULL)
  74. return -1;
  75. strftime (tbuf, sizeof (tbuf) - 1, "%Y/%m/%d %H:%M:%S", tm);
  76. snprintf (buf, len, "%s.%06ld", tbuf, (long) tv.tv_usec);
  77. return 0;
  78. }
  79. SCM_DEFINE (guile_ssh_default_libssh_log_printer,
  80. "%default-libssh-log-printer", 4, 0, 0,
  81. (SCM priority, SCM function_name, SCM message, SCM user_data),
  82. "")
  83. {
  84. char date[TBUF_SZ] = {0};
  85. int rc = _get_current_timestring (date, sizeof(date));
  86. scm_puts ("[", scm_current_error_port ());
  87. if (rc == 0)
  88. {
  89. scm_puts (date, scm_current_error_port ());
  90. scm_puts (", ", scm_current_error_port ());
  91. }
  92. scm_display (priority, scm_current_error_port ());
  93. scm_puts ("] ", scm_current_error_port ());
  94. scm_display (message, scm_current_error_port ());
  95. scm_newline (scm_current_error_port ());
  96. return SCM_UNDEFINED;
  97. }
  98. SCM_DEFINE (guile_ssh_set_logging_callback_x,
  99. "set-logging-callback!", 1, 0, 0,
  100. (SCM procedure),
  101. "")
  102. #define FUNC_NAME s_guile_ssh_set_logging_callback_x
  103. {
  104. SCM_ASSERT (scm_procedure_p (procedure), procedure, SCM_ARG1, FUNC_NAME);
  105. if (! is_logging_callback_set)
  106. {
  107. int res = ssh_set_log_userdata (SCM_BOOL_F);
  108. if (res != SSH_OK)
  109. guile_ssh_error1 (FUNC_NAME, "Could not set userdata", procedure);
  110. res = ssh_set_log_callback (&libssh_logging_callback);
  111. if (res != SSH_OK)
  112. guile_ssh_error1 (FUNC_NAME, "Could not setup logging", procedure);
  113. is_logging_callback_set = 1;
  114. }
  115. logging_callback = procedure;
  116. return SCM_UNDEFINED;
  117. }
  118. #undef FUNC_NAME
  119. SCM_DEFINE (guile_ssh_get_logging_callback,
  120. "current-logging-callback", 0, 0, 0,
  121. (void),
  122. "")
  123. {
  124. return logging_callback;
  125. }
  126. SCM_DEFINE (guile_ssh_set_log_useradata_x,
  127. "set-log-userdata!", 1, 0, 0,
  128. (SCM data),
  129. "")
  130. #define FUNC_NAME s_guile_ssh_set_log_useradata_x
  131. {
  132. int res = ssh_set_log_userdata (data);
  133. if (res != SSH_OK)
  134. guile_ssh_error1 (FUNC_NAME, "Could not set userdata", data);
  135. return SCM_UNDEFINED;
  136. }
  137. #undef FUNC_NAME
  138. SCM_DEFINE (guile_ssh_get_log_userdata,
  139. "get-log-userdata", 0, 0, 0,
  140. (void),
  141. "")
  142. {
  143. void *data = (void *) ssh_get_log_userdata ();
  144. return data ? (SCM) data : SCM_BOOL_F;
  145. }
  146. SCM_DEFINE (guile_ssh_write_log,
  147. "%write-log", 3, 0, 0,
  148. (SCM priority, SCM function_name, SCM message),
  149. "\
  150. Write a MESSAGE to the libssh log with the given PRIORITY. Return value is \n\
  151. undefined. \
  152. ")
  153. #define FUNC_NAME s_guile_ssh_write_log
  154. {
  155. const struct symbol_mapping *c_priority;
  156. char *c_function_name;
  157. char *c_message;
  158. SCM_ASSERT (scm_symbol_p (priority), priority, SCM_ARG1, FUNC_NAME);
  159. SCM_ASSERT (scm_string_p (function_name), function_name, SCM_ARG2, FUNC_NAME);
  160. SCM_ASSERT (scm_string_p (message), message, SCM_ARG3, FUNC_NAME);
  161. c_priority = _scm_to_ssh_const (log_verbosity, priority);
  162. if (! c_priority)
  163. guile_ssh_error1 (FUNC_NAME, "Wrong priority level", priority);
  164. c_function_name = scm_to_locale_string (function_name);
  165. c_message = scm_to_locale_string (message);
  166. _ssh_log (c_priority->value, c_function_name, "%s", c_message);
  167. return SCM_UNDEFINED;
  168. }
  169. #undef FUNC_NAME
  170. SCM_DEFINE (guile_ssh_set_log_verbosity_x,
  171. "set-log-verbosity!", 1, 0, 0,
  172. (SCM verbosity),
  173. "\
  174. Set the global log verbosity to a VERBOSITY. Throw `guile-ssh-error' on \
  175. error. Return value is undefined.\
  176. ")
  177. #define FUNC_NAME s_guile_ssh_set_log_verbosity_x
  178. {
  179. const struct symbol_mapping *opt = _scm_to_ssh_const (log_verbosity, verbosity);
  180. int res;
  181. if (! opt)
  182. guile_ssh_error1 (FUNC_NAME, "Wrong verbosity level", verbosity);
  183. res = ssh_set_log_level (opt->value);
  184. if (res == SSH_ERROR)
  185. guile_ssh_error1 (FUNC_NAME, "Could not set log verbosity", verbosity);
  186. return SCM_UNDEFINED;
  187. }
  188. #undef FUNC_NAME
  189. SCM_DEFINE (guile_ssh_get_log_verbosity,
  190. "get-log-verbosity", 0, 0, 0,
  191. (void),
  192. "\
  193. Get global log verbosity value.\
  194. ")
  195. {
  196. return _ssh_const_to_scm (log_verbosity, ssh_get_log_level ());
  197. }
  198. /* Write an error MESSAGE along with ARGS to the libssh log. */
  199. void
  200. _gssh_log_error (const char* function_name, const char* msg, SCM args)
  201. {
  202. char *c_str;
  203. scm_dynwind_begin (0);
  204. c_str = scm_to_locale_string (scm_object_to_string (args, SCM_UNDEFINED));
  205. scm_dynwind_free (c_str);
  206. _ssh_log (SSH_LOG_NOLOG, function_name, "[GSSH ERROR] %s: %s",
  207. msg, c_str);
  208. scm_dynwind_end ();
  209. }
  210. void
  211. _gssh_log_warning (const char* function_name, const char* msg, SCM args)
  212. {
  213. char *c_str;
  214. scm_dynwind_begin (0);
  215. c_str = scm_to_locale_string (scm_object_to_string (args, SCM_UNDEFINED));
  216. scm_dynwind_free (c_str);
  217. _ssh_log (SSH_LOG_WARNING, function_name, "[GSSH WARNING] %s: %s",
  218. msg, c_str);
  219. scm_dynwind_end ();
  220. }
  221. void
  222. _gssh_log_debug (const char* function_name, const char* msg, SCM args)
  223. {
  224. char *c_str;
  225. scm_dynwind_begin (0);
  226. c_str = scm_to_locale_string (scm_object_to_string (args, SCM_UNDEFINED));
  227. scm_dynwind_free (c_str);
  228. _ssh_log (SSH_LOG_FUNCTIONS, function_name, "[GSSH DEBUG] %s: %s",
  229. msg, c_str);
  230. scm_dynwind_end ();
  231. }
  232. void
  233. _gssh_log_debug1 (const char* function_name, const char* msg)
  234. {
  235. _ssh_log (SSH_LOG_FUNCTIONS, function_name, "[GSSH DEBUG] %s", msg);
  236. }
  237. #ifdef DEBUG
  238. void
  239. _gssh_log_debug_format(const char* function_name, SCM args, const char* fmt, ...)
  240. {
  241. va_list arg;
  242. enum { MSG_SZ = 100 };
  243. char msg[MSG_SZ];
  244. va_start (arg, fmt);
  245. vsnprintf (msg, MSG_SZ, fmt, arg);
  246. va_end (arg);
  247. _gssh_log_debug(function_name, msg, args);
  248. }
  249. #else
  250. #define _gssh_log_debug_format(function_name, args, fmt, ...)
  251. #endif /* ifdef DEBUG */
  252. /* Initialization */
  253. void
  254. init_log_func (void)
  255. {
  256. #include "log.x"
  257. }
  258. /* log.c ends here */