Commit 6093d286 authored by mbionchi's avatar mbionchi

make log use vararg, replace old fprintfs

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