aboutsummaryrefslogtreecommitdiff
path: root/src/log.c
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-09-10 11:49:53 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-09-10 12:38:42 +0200
commitc8f48a247808fe9ad8a59774545f527dbea71cfc (patch)
treed53ffa85dda96b08bd9c091057046cbefe98d284 /src/log.c
parent08129002de26ff010d55e4ad4bc93c675a3412be (diff)
downloadlibssh-c8f48a247808fe9ad8a59774545f527dbea71cfc.tar.gz
libssh-c8f48a247808fe9ad8a59774545f527dbea71cfc.tar.xz
libssh-c8f48a247808fe9ad8a59774545f527dbea71cfc.zip
log: Improve the logging function.
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c100
1 files changed, 72 insertions, 28 deletions
diff --git a/src/log.c b/src/log.c
index 509b8abc..dbbe8484 100644
--- a/src/log.c
+++ b/src/log.c
@@ -24,8 +24,10 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
+#include <sys/time.h>
#include "libssh/priv.h"
+#include "libssh/misc.h"
#include "libssh/session.h"
/**
@@ -37,43 +39,85 @@
* @{
*/
+static int current_timestring(int hires, char *buf, size_t len)
+{
+ char tbuf[64];
+ struct timeval tv;
+ struct tm *tm;
+ time_t t;
+
+ gettimeofday(&tv, NULL);
+ t = (time_t) tv.tv_sec;
+
+ tm = localtime(&t);
+ if (tm == NULL) {
+ return -1;
+ }
+
+ if (hires) {
+ strftime(tbuf, sizeof(tbuf) - 1, "%Y/%m/%d %H:%M:%S", tm);
+ snprintf(buf, len, "%s.%06ld", tbuf, tv.tv_usec);
+ } else {
+ strftime(tbuf, sizeof(tbuf) - 1, "%Y/%m/%d %H:%M:%S", tm);
+ snprintf(buf, len, "%s", tbuf);
+ }
+
+ return 0;
+}
+
/** @internal
* @brief do the actual work of logging an event
*/
-static void do_ssh_log(struct ssh_common_struct *common, int verbosity,
- const char *buffer){
- char indent[256];
- int min;
- if (common->callbacks && common->callbacks->log_function) {
- common->callbacks->log_function((ssh_session)common, verbosity, buffer,
- common->callbacks->userdata);
- } else if (verbosity == SSH_LOG_FUNCTIONS) {
- if (common->log_indent > 255) {
- min = 255;
+static void do_ssh_log(struct ssh_common_struct *common,
+ int verbosity,
+ const char *function,
+ const char *buffer) {
+ char date[64] = {0};
+ int rc;
+
+ if (common->callbacks && common->callbacks->log_function) {
+ char buf[1024];
+
+ snprintf(buf, sizeof(buf), "%s: %s", function, buf);
+
+ common->callbacks->log_function((ssh_session)common,
+ verbosity,
+ buf,
+ common->callbacks->userdata);
+ return;
+ }
+
+ rc = current_timestring(1, date, sizeof(date));
+ if (rc == 0) {
+ fprintf(stderr, "[%s, %d] %s\n", date, verbosity, function);
} else {
- min = common->log_indent;
+ fprintf(stderr, "[%d] %s\n", verbosity, function);
}
+ fprintf(stderr, " %s\n", buffer);
+}
- memset(indent, ' ', min);
- indent[min] = '\0';
+/* legacy function */
+void ssh_log(ssh_session session,
+ int verbosity,
+ const char *format, ...)
+{
+ char buffer[1024];
+ va_list va;
- fprintf(stderr, "[func] %s%s\n", indent, buffer);
- } else {
- fprintf(stderr, "[%d] %s\n", verbosity, buffer);
+ if (verbosity <= session->common.log_verbosity) {
+ va_start(va, format);
+ vsnprintf(buffer, sizeof(buffer), format, va);
+ va_end(va);
+ do_ssh_log(&session->common, verbosity, "", buffer);
}
}
-/**
- * @brief Log a SSH event.
- *
- * @param session The SSH session.
- *
- * @param verbosity The verbosity of the event.
- *
- * @param format The format string of the log entry.
- */
-void ssh_log(ssh_session session, int verbosity, const char *format, ...) {
+void ssh_log_function(ssh_session session,
+ int verbosity,
+ const char *function,
+ const char *format, ...)
+{
char buffer[1024];
va_list va;
@@ -81,7 +125,7 @@ void ssh_log(ssh_session session, int verbosity, const char *format, ...) {
va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va);
va_end(va);
- do_ssh_log(&session->common, verbosity, buffer);
+ do_ssh_log(&session->common, verbosity, function, buffer);
}
}
@@ -99,7 +143,7 @@ void ssh_log_common(struct ssh_common_struct *common, int verbosity, const char
va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va);
va_end(va);
- do_ssh_log(common, verbosity, buffer);
+ do_ssh_log(common, verbosity, "common", buffer);
}
}