#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_UTIME_H
#include <sys/utime.h>
#endif
#include <time.h>
#include "libssh/priv.h"
#include "libssh/misc.h"
#include "libssh/session.h"
#ifndef LOG_SIZE
#define LOG_SIZE 1024
#endif
static LIBSSH_THREAD int ssh_log_level;
static LIBSSH_THREAD ssh_logging_callback ssh_log_cb;
static LIBSSH_THREAD void *ssh_log_userdata;
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, (long)tv.tv_usec);
} else {
strftime(tbuf, sizeof(tbuf) - 1, "%Y/%m/%d %H:%M:%S", tm);
snprintf(buf, len, "%s", tbuf);
}
return 0;
}
static void ssh_log_stderr(int verbosity,
const char *function,
const char *buffer)
{
char date[128] = {0};
int rc;
rc = current_timestring(1, date, sizeof(date));
if (rc == 0) {
fprintf(stderr, "[%s, %d] %s:", date, verbosity, function);
} else {
fprintf(stderr, "[%d] %s", verbosity, function);
}
fprintf(stderr, " %s\n", buffer);
}
static void ssh_log_custom(ssh_logging_callback log_fn,
int verbosity,
const char *function,
const char *buffer)
{
char buf[LOG_SIZE + 64];
snprintf(buf, sizeof(buf), "%s: %s", function, buffer);
log_fn(verbosity, function, buf, ssh_get_log_userdata());
}
void ssh_log_function(int verbosity,
const char *function,
const char *buffer)
{
ssh_logging_callback log_fn = ssh_get_log_callback();
if (log_fn) {
ssh_log_custom(log_fn, verbosity, function, buffer);
return;
}
ssh_log_stderr(verbosity, function, buffer);
}
void ssh_vlog(int verbosity,
const char *function,
const char *format,
va_list *va)
{
char buffer[LOG_SIZE];
vsnprintf(buffer, sizeof(buffer), format, *va);
ssh_log_function(verbosity, function, buffer);
}
void _ssh_log(int verbosity,
const char *function,
const char *format, ...)
{
va_list va;
if (verbosity <= ssh_get_log_level()) {
va_start(va, format);
ssh_vlog(verbosity, function, format, &va);
va_end(va);
}
}
void ssh_log(ssh_session session,
int verbosity,
const char *format, ...)
{
va_list va;
if (verbosity <= session->common.log_verbosity) {
va_start(va, format);
ssh_vlog(verbosity, "", format, &va);
va_end(va);
}
}
void ssh_log_common(struct ssh_common_struct *common,
int verbosity,
const char *function,
const char *format, ...)
{
va_list va;
if (verbosity <= common->log_verbosity) {
va_start(va, format);
ssh_vlog(verbosity, function, format, &va);
va_end(va);
}
}
int ssh_set_log_level(int level) {
if (level < 0) {
return SSH_ERROR;
}
ssh_log_level = level;
return SSH_OK;
}
int ssh_get_log_level(void) {
return ssh_log_level;
}
int ssh_set_log_callback(ssh_logging_callback cb) {
if (cb == NULL) {
return SSH_ERROR;
}
ssh_log_cb = cb;
return SSH_OK;
}
ssh_logging_callback ssh_get_log_callback(void) {
return ssh_log_cb;
}
void *ssh_get_log_userdata(void)
{
if (ssh_log_userdata == NULL) {
return NULL;
}
return ssh_log_userdata;
}
int ssh_set_log_userdata(void *data)
{
ssh_log_userdata = data;
return 0;
}