Commit 7e4497c7 authored by mbionchi's avatar mbionchi

add rudimentary logging but with ip and terminal tracking

parent 9c84b470
......@@ -20,20 +20,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#define LOGPATH "/tmp/telnetsite"
char *lvl_str[] = {
"INFO",
"WARN",
"ERROR",
"NIL"
};
void errlog(char *fmt, ...) {
static FILE *fp = NULL;
if (fp == NULL) {
fp = fopen(LOGPATH, "a");
void log_(enum log_lvl lvl, char *s) {
static char *ip = NULL;
if (!ip) {
ip = getenv("TCPREMOTEIP");
if (!ip) {
ip = "<unknown>";
}
}
if (fp != NULL) {
va_list args;
va_start(args, fmt);
vfprintf(fp, fmt, args);
fflush(fp);
va_end(args);
if (lvl < 0 || lvl > 3) {
lvl = 3;
}
time_t now;
time(&now);
struct tm *tm = gmtime(&now);
char date[20] = "";
if (tm) {
snprintf(date, 20, "%04d-%02d-%02dt%02d-%02d-%02d",
tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
fprintf(stderr, "%-19s %-15s %-5s %s\n", date, ip, lvl_str[lvl], s);
}
......@@ -18,8 +18,15 @@
#ifndef _TELNETSITE_LOG_H
#define _TELNETSITE_LOG_H
#include <stdarg.h>
enum log_lvl {
LOG_INFO = 0,
LOG_WARN = 1,
LOG_ERR = 2,
LOG_NIL = 3
};
void errlog(char *fmt, ...);
extern char *lvl_str[];
void log_(enum log_lvl lvl, char *s);
#endif
......@@ -19,6 +19,7 @@
#include "splash.h"
#include "data.h"
#include "winch.h"
#include "log.h"
#include <getopt.h>
#include <stdlib.h>
......@@ -49,8 +50,15 @@ int main(int argc, char **argv) {
opt = getopt_long_only(argc, argv, "", options, NULL);
}
char *term = getenv("TERM");
if (!term) {
term = "unknown";
}
char log_str[256] = "";
snprintf(log_str, 256, "started session, TERM is %s", term);
log_(LOG_INFO, log_str);
if (!site_path) {
fprintf(stderr, "Usage: %s --site <path-to-dir> [--splash <path-to-file>]\n", binary_name);
log_(LOG_ERR, "usage: telnetsite --site <path-to-dir> [--splash <path-to-file>]");
exit(1);
}
......@@ -74,6 +82,6 @@ int main(int argc, char **argv) {
}
endwin();
log_(LOG_INFO, "ended session");
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment