aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-11-18 17:53:58 -0500
committerDeterminant <[email protected]>2018-11-18 17:53:58 -0500
commit499fcb776d691da06ec2da9a0fbadc3cbcf8758f (patch)
tree94b6666b8ca6f2879b466c1fe2be254b2cd96fe2 /include
parent161d969e0eabfecccd69a6b9ed2d03919cf89cb5 (diff)
make logger thread-safe and add tty color
Diffstat (limited to 'include')
-rw-r--r--include/salticidae/conn.h1
-rw-r--r--include/salticidae/netaddr.h1
-rw-r--r--include/salticidae/util.h51
3 files changed, 28 insertions, 25 deletions
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 <cassert>
#include <cstdint>
-#include <event2/event.h>
#include <arpa/inet.h>
#include <unistd.h>
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 <cstring>
#include <cstdint>
#include <arpa/inet.h>
+#include <netdb.h>
#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 <unordered_map>
#include <functional>
#include <getopt.h>
-#include <event2/event.h>
+#include <unistd.h>
+#include <fcntl.h>
#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<std::string> split(const std::string &s, const std::string &delim);
std::vector<std::string> trim_all(const std::vector<std::string> &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<typename... Args>
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, ...);
};