1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-10 06:23:01 +03:00

log: Improve the logging function.

This commit is contained in:
Andreas Schneider
2011-09-10 11:49:53 +02:00
parent 08129002de
commit c8f48a2478
3 changed files with 89 additions and 32 deletions

106
src/log.c
View File

@@ -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,69 @@
* @{
*/
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;
} else {
min = common->log_indent;
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;
}
memset(indent, ' ', min);
indent[min] = '\0';
fprintf(stderr, "[func] %s%s\n", indent, buffer);
} else {
fprintf(stderr, "[%d] %s\n", verbosity, buffer);
}
rc = current_timestring(1, date, sizeof(date));
if (rc == 0) {
fprintf(stderr, "[%s, %d] %s\n", date, verbosity, function);
} else {
fprintf(stderr, "[%d] %s\n", verbosity, function);
}
fprintf(stderr, " %s\n", 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, ...) {
/* legacy function */
void ssh_log(ssh_session session,
int verbosity,
const char *format, ...)
{
char buffer[1024];
va_list va;
@@ -81,7 +109,23 @@ 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, "", buffer);
}
}
void ssh_log_function(ssh_session session,
int verbosity,
const char *function,
const char *format, ...)
{
char buffer[1024];
va_list va;
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, 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);
}
}