Commit 6093d286 authored by mbionchi's avatar mbionchi

make log use vararg, replace old fprintfs

parent 7e4497c7
......@@ -21,6 +21,7 @@
#include "data.h"
#include "module.h"
#include "log.h"
#include <dlfcn.h>
#include <stdlib.h>
......@@ -82,7 +83,7 @@ int read_content_from_section(struct content *content, struct section *section)
content->lines->n_raw = read_nlines(fp, &content->lines->raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), section->filename);
log_(LOG_WARN, "%s:%s:%u: %s: %s", binary_name, __FILE__, __LINE__, strerror(errno), section->filename);
free(content->lines);
content->lines = NULL;
return 1;
......@@ -92,14 +93,14 @@ int read_content_from_section(struct content *content, struct section *section)
content->dlib = malloc(sizeof(struct dynamic_content));
content->dlib->so_handle = dlopen(section->filename, RTLD_LAZY);
if (content->dlib->so_handle == NULL) {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, dlerror());
log_(LOG_WARN, "%s:%s:%u: %s: %s", binary_name, __FILE__, __LINE__, dlerror(), section->filename);
free(content->dlib);
content->dlib = NULL;
return 1;
} else {
content->dlib->init_fun = dlsym(content->dlib->so_handle, INIT_FUNC_NAME_S);
if (content->dlib->init_fun == NULL) {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, dlerror());
log_(LOG_WARN, "%s:%s:%u: %s: %s", binary_name, __FILE__, __LINE__, dlerror());
dlclose(content->dlib->so_handle);
free(content->dlib);
content->dlib = NULL;
......
......@@ -29,7 +29,7 @@ char *lvl_str[] = {
"NIL"
};
void log_(enum log_lvl lvl, char *s) {
void log_(enum log_lvl lvl, char *fmt, ...) {
static char *ip = NULL;
if (!ip) {
ip = getenv("TCPREMOTEIP");
......@@ -42,6 +42,18 @@ void log_(enum log_lvl lvl, char *s) {
lvl = 3;
}
char *str = NULL;
size_t str_len = 0;
va_list args;
va_start(args, fmt);
str_len = vsnprintf(str, str_len, fmt, args);
va_end(args);
str_len++;
str = malloc(sizeof(char)*str_len);
va_start(args, fmt);
str_len = vsnprintf(str, str_len, fmt, args);
va_end(args);
time_t now;
time(&now);
struct tm *tm = gmtime(&now);
......@@ -51,6 +63,7 @@ void log_(enum log_lvl lvl, char *s) {
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);
fprintf(stderr, "%-19s %-15s %-5s %s\n", date, ip, lvl_str[lvl], str);
free(str);
}
......@@ -27,6 +27,6 @@ enum log_lvl {
extern char *lvl_str[];
void log_(enum log_lvl lvl, char *s);
void log_(enum log_lvl lvl, char *fmt, ...);
#endif
......@@ -50,15 +50,9 @@ 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);
log_(LOG_INFO, "started session, TERM is %s", getenv("TERM"));
if (!site_path) {
log_(LOG_ERR, "usage: telnetsite --site <path-to-dir> [--splash <path-to-file>]");
log_(LOG_ERR, "usage: %s --site <path-to-dir> [--splash <path-to-file>]", binary_name);
exit(1);
}
......@@ -70,18 +64,20 @@ int main(int argc, char **argv) {
cbreak();
halfdelay(1);
noecho();
curs_set(0);
int curs_save = curs_set(0);
nonl();
keypad(main_window, 1);
int rv = 0;
if (splash_path) {
splash(splash_path);
rv |= splash(splash_path);
}
if (site_path) {
site(site_path);
rv |= site(site_path);
}
curs_set(curs_save);
endwin();
log_(LOG_INFO, "ended session");
return 0;
log_(rv?LOG_ERR:LOG_INFO, "ended session with status %d", rv);
return rv;
}
......@@ -22,6 +22,7 @@
#include "scroll.h"
#include "data.h"
#include "winch.h"
#include "log.h"
#include <errno.h>
#include <ncurses.h>
......@@ -60,7 +61,7 @@ int reload_content = 0;
* - error handling everywhere please
* - refactor the event loop, it's horrifying atm
*/
void site(char *path) {
int site(char *path) {
struct window index_window;
struct window separator_window;
struct window content_window;
......@@ -72,8 +73,8 @@ void site(char *path) {
size_t n_sections;
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "[E] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), path);
exit(1);
log_(LOG_ERR, "%s:%s:%u: %s: %s", binary_name, __FILE__, __LINE__, strerror(errno), path);
return 1;
}
struct section **sections = read_sections(dir, path, &n_sections);
closedir(dir);
......@@ -388,4 +389,5 @@ void site(char *path) {
delwin(index_window.window);
delwin(separator_window.window);
delwin(content_window.window);
return 0;
}
......@@ -18,6 +18,6 @@
#ifndef _TELNET_SITE_SITE_H
#define _TELNET_SITE_SITE_H
void site(char *site_path);
int site(char *site_path);
#endif
......@@ -21,6 +21,7 @@
#include "render.h"
#include "anim.h"
#include "winch.h"
#include "log.h"
#include <stdio.h>
#include <stddef.h>
......@@ -30,7 +31,7 @@
#include <string.h>
#include <signal.h>
void splash(char *path) {
int splash(char *path) {
struct window splash_window;
FILE *fp = fopen(path, "r");
......@@ -40,7 +41,7 @@ void splash(char *path) {
splash_window.content.lines->n_raw = read_nlines(fp, &splash_window.content.lines->raw);
fclose(fp);
} else {
fprintf(stderr, "[W] %s:%s:%u: %s: %s\n", binary_name, __FILE__, __LINE__, strerror(errno), path);
log_(LOG_WARN, "%s:%s:%u: %s: %s", binary_name, __FILE__, __LINE__, strerror(errno), path);
gen_err_opening(&splash_window.content);
}
......@@ -90,5 +91,5 @@ void splash(char *path) {
clear();
wrefresh(splash_window.window);
delwin(splash_window.window);
return;
return 0;
}
......@@ -18,6 +18,6 @@
#ifndef _TELNET_SPLASH_SITE_H
#define _TELNET_SPLASH_SITE_H
void splash(char *path);
int splash(char *path);
#endif
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