From 499fcb776d691da06ec2da9a0fbadc3cbcf8758f Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 18 Nov 2018 17:53:58 -0500 Subject: make logger thread-safe and add tty color --- include/salticidae/conn.h | 1 - include/salticidae/netaddr.h | 1 + include/salticidae/util.h | 51 +++++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 25 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 265a02a..79b1a89 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -27,7 +27,6 @@ #include #include -#include #include #include diff --git a/include/salticidae/netaddr.h b/include/salticidae/netaddr.h index 5639c1b..224d1d4 100644 --- a/include/salticidae/netaddr.h +++ b/include/salticidae/netaddr.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "salticidae/util.h" #include "salticidae/stream.h" diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 7552f8e..ed7d980 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -32,7 +32,8 @@ #include #include #include -#include +#include +#include #include "salticidae/config.h" #include "salticidae/ref.h" @@ -40,13 +41,14 @@ namespace salticidae { void sec2tv(double t, struct timeval &tv); -void event_add_with_timeout(struct event *ev, double timeout); double gen_rand_timeout(double base_timeout, double alpha = 0.5); std::string trim(const std::string &s, const std::string &space = "\t\r\n "); std::vector split(const std::string &s, const std::string &delim); std::vector trim_all(const std::vector &ss); +std::string vstringprintf(const char *fmt, va_list ap); +std::string stringprintf(const char *fmt, ...); class SalticidaeError: public std::exception { std::string msg; @@ -55,47 +57,48 @@ class SalticidaeError: public std::exception { template SalticidaeError(const std::string &fmt, Args... args) { - int guessed_size = 128; - std::string buff; - for (;;) - { - buff.resize(guessed_size); - int nwrote = snprintf((char *)buff.data(), guessed_size, fmt.c_str(), args...); - if (nwrote < 0 || nwrote == guessed_size) - { - guessed_size <<= 1; - continue; - } - buff.resize(nwrote); - msg = std::move(buff); - break; - } + msg = stringprintf(fmt.c_str(), args...); } operator std::string() const { return msg; } const char *what() const throw() override { return msg.c_str(); } }; +extern const char *TTY_COLOR_RED; +extern const char *TTY_COLOR_GREEN; +extern const char *TTY_COLOR_YELLOW; +extern const char *TTY_COLOR_BLUE; +extern const char *TTY_COLOR_MAGENT; +extern const char *TTY_COLOR_CYAN; +extern const char *TTY_COLOR_RESET; + class Logger { + const char *color_info; + const char *color_debug; + const char *color_warning; + const char *color_error; protected: - FILE *output; + int output; bool opened; const char *prefix; - void write(const char *tag, const char *fmt, va_list ap); + void write(const char *tag, const char *color, + const char *fmt, va_list ap); + void set_color(); public: - Logger(const char *prefix, FILE *file = stderr): - output(file), opened(false), prefix(prefix) {} + Logger(const char *prefix, int fd = 2): + output(fd), opened(false), prefix(prefix) { set_color(); } Logger(const char *prefix, const char *filename): opened(true), prefix(prefix) { - if ((output = fopen(filename, "w")) == nullptr) + if ((output = open(filename, O_CREAT | O_WRONLY)) == -1) throw SalticidaeError("logger cannot open file %s", filename); + set_color(); } - ~Logger() { if (opened) fclose(output); } + ~Logger() { if (opened) close(output); } - void debug(const char *fmt, ...); void info(const char *fmt, ...); + void debug(const char *fmt, ...); void warning(const char *fmt, ...); void error(const char *fmt, ...); }; -- cgit v1.2.3