diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/include/chord/logger/clog.h b/include/chord/logger/clog.h index 3980a41..cd81f8a 100644 --- a/include/chord/logger/clog.h +++ b/include/chord/logger/clog.h @@ -1,5 +1,5 @@ -#ifndef LOGGER_H -#define LOGGER_H +#ifndef CLOG_H +#define CLOG_H #include @@ -53,9 +53,17 @@ struct logger_ctx_t int log_partial; }; +struct timespec; + void clog_init(); int clog_get_default_log_level(); void clog_set_default_log_level(int level); + int clog_time_offset(struct timespec *diff_time); + + int logger_ctx_init(logger_ctx_t *l, const char *name, int min_level, void *data, start_msg_func start_msg, printf_func printf, write_func write, end_msg_func end_msg); +logger_ctx_t *logger_ctx_new(const char *name, int min_level, void *data, start_msg_func start_msg, printf_func printf, write_func write, end_msg_func end_msg); + void logger_ctx_close(logger_ctx_t *l); + void logger_ctx_free(logger_ctx_t *l); int clog_add_logger(logger_ctx_t *l_new); logger_ctx_t *clog_get_logger(const char *name); @@ -74,35 +82,35 @@ void clog_end_log_as(const char *name); #define clog_file_logger() clog_get_logger_for_file(__FILE__) -#define Log(level, fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) -#define LogTrace(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) -#define LogDebug(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) -#define LogInfo(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) -#define LogWarn(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) -#define LogError(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) -#define LogFatal(fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) +#define Log(level, fmt, ...) clog_log(NULL, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) +#define LogTrace(fmt, ...) Log(CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) +#define LogDebug(fmt, ...) Log(CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) +#define LogInfo(fmt, ...) Log(CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) +#define LogWarn(fmt, ...) Log(CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) +#define LogError(fmt, ...) Log(CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) +#define LogFatal(fmt, ...) Log(CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) #define StartLog(level) { clog_start_log(NULL, __FILE__, __LINE__, __func__, level) #define PartialLog(fmt, ...) clog_partial_log(NULL, __FILE__, fmt, ##__VA_ARGS__) #define EndLog() } clog_end_log(NULL, __FILE__) -#define LogTo(l_ctx, level, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) -#define LogTraceTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) -#define LogDebugTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) -#define LogInfoTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) -#define LogWarnTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) -#define LogErrorTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) -#define LogFatalTo(l_ctx, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) +#define LogTo(l_ctx, level, fmt, ...) clog_log(l_ctx, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) +#define LogTraceTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) +#define LogDebugTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) +#define LogInfoTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) +#define LogWarnTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) +#define LogErrorTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) +#define LogFatalTo(l_ctx, fmt, ...) LogTo(l_ctx, CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) #define StartLogTo(l_ctx, level) { clog_start_log(l_ctx, __FILE__, __LINE__, __func__, level) #define PartialLogTo(l_ctx, fmt, ...) clog_partial_log(l_ctx, __FILE__, fmt, ##__VA_ARGS__) #define EndLogTo(l_ctx) } clog_end_log(l_ctx, __FILE__) -#define LogAs(name, level, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) -#define LogTraceAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) -#define LogDebugAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) -#define LogInfoAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) -#define LogWarnAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) -#define LogErrorAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) -#define LogFatalAs(name, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) +#define LogAs(name, level, fmt, ...) clog_log_as(name, __FILE__, __LINE__, __func__, level, fmt, ##__VA_ARGS__) +#define LogTraceAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__) +#define LogDebugAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__) +#define LogInfoAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) +#define LogWarnAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) +#define LogErrorAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) +#define LogFatalAs(name, fmt, ...) LogAs(name, CLOG_LOG_LEVEL_FATAL, fmt, ##__VA_ARGS__) #define StartLogAs(name, level) { clog_start_log_as(name, __FILE__, __LINE__, __func__, level) #define PartialLogAs(name, fmt, ...) clog_partial_log_as(name, fmt, ##__VA_ARGS__) #define EndLogAs(name) } clog_end_log_as(name) @@ -138,4 +146,4 @@ void clog_end_log_as(const char *name); } #endif -#endif \ No newline at end of file +#endif diff --git a/include/chord/logger/file.h b/include/chord/logger/file.h new file mode 100644 index 0000000..a62e635 --- /dev/null +++ b/include/chord/logger/file.h @@ -0,0 +1,13 @@ +#ifndef CLOG_FILE_H +#define CLOG_FILE_H + +#include +#include "chord/logger/clog.h" + +logger_ctx_t *logger_ctx_new_file(const char *name, int min_level, FILE *file); + int start_file_msg(logger_ctx_t *l, const char *file, int line, const char *func, int level); + ssize_t write_file(FILE *file, const char *buf, size_t size); + int end_file_msg(logger_ctx_t *l); + ssize_t logger_call_write(logger_ctx_t *l, const char *buf, size_t size); + +#endif \ No newline at end of file diff --git a/src/join.c b/src/join.c index 07847ef..a7b29b2 100644 --- a/src/join.c +++ b/src/join.c @@ -1,12 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "chord/chord.h" #include "chord/sendpkt.h" diff --git a/src/logger/clog.c b/src/logger/clog.c index 49411e6..2357177 100644 --- a/src/logger/clog.c +++ b/src/logger/clog.c @@ -9,17 +9,13 @@ #include "chord/logger/clog.h" #include "chord/logger/color.h" #include "chord/logger/hashmap.h" - -#define BASENAME(file) strrchr(file, '/') ? strrchr(file, '/') + 1 : file +#include "chord/logger/file.h" +#include "clog_internal.h" static map_t loggers; static int clog_default_log_level = 0; -static int default_level_colors[] = { - FG_PURPLE|MOD_INTENSE_FG, FG_CYAN|MOD_INTENSE_FG, FG_GREEN|MOD_INTENSE_FG, FG_YELLOW|MOD_INTENSE_FG, FG_RED|MOD_INTENSE_FG, FG_WHITE|BG_RED -}; - struct timespec start_time; void clog_init() @@ -67,67 +63,27 @@ void clog_set_default_log_level(int level) clog_default_log_level = level; } -int start_file_msg(logger_ctx_t *l, const char *file, int line, const char *func, int level) -{ - FILE *fp = (FILE *)l->data; - - int color = level >= CLOG_LOG_LEVEL_TRACE && level <= CLOG_LOG_LEVEL_FATAL ? default_level_colors[level] : 0; - start_color(fp, color|ATTR_BOLD); - -#ifdef LOG_BRACKET_LEADER - char leader[level+1]; - memset(leader, '>', level); - leader[level] = '\0'; - fprintf(fp, "%s> ", leader); -#endif - +int clog_time_offset(struct timespec *diff_time) +{ struct timespec current_time; if (start_time.tv_sec == 0 && start_time.tv_nsec == 0) { - clock_gettime(CLOCK_MONOTONIC, &start_time); + if (clock_gettime(CLOCK_MONOTONIC, &start_time)) return -1; current_time.tv_sec = start_time.tv_sec; current_time.tv_nsec = start_time.tv_nsec; } else - clock_gettime(CLOCK_MONOTONIC, ¤t_time); + if (clock_gettime(CLOCK_MONOTONIC, ¤t_time)) return -1; - struct timespec diff_time; - diff_time.tv_sec = current_time.tv_sec - start_time.tv_sec; - diff_time.tv_nsec = current_time.tv_nsec - start_time.tv_nsec; - if (diff_time.tv_nsec < 0) { - diff_time.tv_sec--; - diff_time.tv_nsec += 1000000000L; + diff_time->tv_sec = current_time.tv_sec - start_time.tv_sec; + diff_time->tv_nsec = current_time.tv_nsec - start_time.tv_nsec; + if (diff_time->tv_nsec < 0) { + diff_time->tv_sec--; + diff_time->tv_nsec += 1000000000L; } - diff_time.tv_nsec = (diff_time.tv_nsec - (diff_time.tv_nsec % 1000000L)) / 1000000L; - - int ret = fprintf(fp, "[%3lu.%.3lu] {%s} (%s) %s@%d: ", diff_time.tv_sec, diff_time.tv_nsec, l->name, func, BASENAME(file), line); + diff_time->tv_nsec = (diff_time->tv_nsec - (diff_time->tv_nsec % 1000000L)) / 1000000L; - default_color(fp); - start_color(fp, color); - - return ret; -} - -ssize_t write_file(FILE *file, const char *buf, size_t size) -{ - return fwrite(buf, 1, size, file); -} - -int end_file_msg(logger_ctx_t *l) -{ - FILE *fp = (FILE *)l->data; - default_color(fp); - int ret = fwrite("\n", 1, 1, fp) != 1; - ret |= fflush(fp); - return ret; -} - -ssize_t logger_call_write(logger_ctx_t *l, const char *buf, size_t size) -{ - if (l->log_partial) - return l->write(l->data, buf, size); - else - return size; + return 0; } int logger_ctx_init(logger_ctx_t *l, const char *name, int min_level, void *data, start_msg_func start_msg, printf_func printf, write_func write, end_msg_func end_msg) @@ -169,17 +125,10 @@ logger_ctx_t *logger_ctx_new(const char *name, int min_level, void *data, start_ return l; } -logger_ctx_t *logger_ctx_new_file(const char *name, int min_level, FILE *file) -{ - logger_ctx_t *l = malloc(sizeof(logger_ctx_t)); - if (logger_ctx_init(l, name, min_level, file, (start_msg_func)start_file_msg, (printf_func)vfprintf, (write_func)write_file, (end_msg_func)end_file_msg)) - return NULL; - - return l; -} - void logger_ctx_close(logger_ctx_t *l) { + if (l->fp != NULL) + fclose(l->fp); } void logger_ctx_free(logger_ctx_t *l) diff --git a/src/logger/clog_internal.h b/src/logger/clog_internal.h new file mode 100644 index 0000000..333683d --- /dev/null +++ b/src/logger/clog_internal.h @@ -0,0 +1,8 @@ +#ifndef CLOG_INTERNAL_H +#define CLOG_INTERNAL_H + +#include + +#define BASENAME(file) strrchr(file, '/') ? strrchr(file, '/') + 1 : file + +#endif diff --git a/src/logger/file.c b/src/logger/file.c new file mode 100644 index 0000000..556683a --- /dev/null +++ b/src/logger/file.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "chord/logger/clog.h" +#include "chord/logger/color.h" +#include "chord/logger/file.h" +#include "clog_internal.h" + +static int default_level_colors[] = { + FG_PURPLE|MOD_INTENSE_FG, FG_CYAN|MOD_INTENSE_FG, FG_GREEN|MOD_INTENSE_FG, FG_YELLOW|MOD_INTENSE_FG, FG_RED|MOD_INTENSE_FG, FG_WHITE|BG_RED +}; + +logger_ctx_t *logger_ctx_new_file(const char *name, int min_level, FILE *file) +{ + logger_ctx_t *l = malloc(sizeof(logger_ctx_t)); + if (logger_ctx_init(l, name, min_level, file, (start_msg_func)start_file_msg, (printf_func)vfprintf, (write_func)write_file, (end_msg_func)end_file_msg)) + return NULL; + + return l; +} + +int start_file_msg(logger_ctx_t *l, const char *file, int line, const char *func, int level) +{ + FILE *fp = (FILE *)l->data; + + int color = level >= CLOG_LOG_LEVEL_TRACE && level <= CLOG_LOG_LEVEL_FATAL ? default_level_colors[level] : 0; + start_color(fp, color|ATTR_BOLD); + +#ifdef LOG_BRACKET_LEADER + char leader[level+1]; + memset(leader, '>', level); + leader[level] = '\0'; + fprintf(fp, "%s> ", leader); +#endif + + int ret; + struct timespec diff_time; + if (clog_time_offset(&diff_time)) + ret = fprintf(fp, "[] {%s} (%s) %s@%d: ", l->name, func, BASENAME(file), line); + else + ret = fprintf(fp, "[%3lu.%.3lu] {%s} (%s) %s@%d: ", diff_time.tv_sec, diff_time.tv_nsec, l->name, func, BASENAME(file), line); + + default_color(fp); + start_color(fp, color); + + return ret; +} + +ssize_t write_file(FILE *file, const char *buf, size_t size) +{ + return fwrite(buf, 1, size, file); +} + +int end_file_msg(logger_ctx_t *l) +{ + FILE *fp = (FILE *)l->data; + default_color(fp); + int ret = fwrite("\n", 1, 1, fp) != 1; + ret |= fflush(fp); + return ret; +} + +ssize_t logger_call_write(logger_ctx_t *l, const char *buf, size_t size) +{ + if (l->log_partial) + return l->write(l->data, buf, size); + else + return size; +} \ No newline at end of file