From b5db8c3a396b12970f4f173553495e25cabcf94b Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Wed, 12 May 2021 23:30:47 +0800 Subject: [PATCH] perf: log: use git submodule Signed-off-by: Jianhui Zhao --- .github/workflows/release.yml | 2 +- .gitmodules | 7 +- src/CMakeLists.txt | 1 + src/command.c | 2 +- src/file.c | 6 +- src/log | 1 + src/log.c | 143 ---------------------------------- src/log.h | 46 ----------- src/main.c | 6 +- src/net.c | 2 +- src/rtty.c | 2 +- src/web.c | 2 +- 12 files changed, 18 insertions(+), 202 deletions(-) create mode 160000 src/log delete mode 100644 src/log.c delete mode 100644 src/log.h diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 10eddcc..26041b5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,7 +30,7 @@ jobs: version=${{ steps.get-version.outputs.version-without-v }} cd .. cp -r rtty rtty-$version - rm -rf rtty-$version/.git* rtty-$version/src/buffer/.git* rtty-$version/src/ssl/.git* + rm -rf rtty-$version/.git* rtty-$version/src/buffer/.git* rtty-$version/src/ssl/.git* rtty-$version/src/log/.git* tar zcfv rtty-$version.tar.gz rtty-$version echo "::set-output name=asset-path::../rtty-$version.tar.gz" echo "::set-output name=asset-name::rtty-$version.tar.gz" diff --git a/.gitmodules b/.gitmodules index 50acaa1..6be3123 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "src/buffer"] path = src/buffer - url = https://github.com/zhaojh329/buffer.git + url = https://gitee.com/zhaojh329/buffer.git [submodule "src/ssl"] path = src/ssl - url = https://github.com/zhaojh329/ssl.git + url = https://gitee.com/zhaojh329/ssl.git +[submodule "src/log"] + path = src/log + url = https://github.com/zhaojh329/log.git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f3375a4..b2e45ee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ set(RTTY_VERSION_PATCH 0) find_package(Libev REQUIRED) aux_source_directory(. SRCS) +aux_source_directory(log SRCS) aux_source_directory(buffer SRCS) add_executable(rtty ${SRCS}) diff --git a/src/command.c b/src/command.c index 6ef6bbd..49c92d3 100644 --- a/src/command.c +++ b/src/command.c @@ -34,7 +34,7 @@ #include #include -#include "log.h" +#include "log/log.h" #include "list.h" #include "utils.h" #include "command.h" diff --git a/src/file.c b/src/file.c index bbcaa63..e010aa0 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include #include -#include "log.h" +#include "log/log.h" #include "file.h" #include "list.h" #include "rtty.h" @@ -143,7 +143,7 @@ static int start_upload_file(struct file_context *ctx, const char *path) fd = open(path, O_RDONLY); if (fd < 0) { - log_err("open '%s' fail\n", path, strerror(errno)); + log_err("open '%s' fail: %s\n", path, strerror(errno)); return -1; } @@ -295,7 +295,7 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i log_info("download file: %s, size: %u\n", savepath, ctx->total_size); if (fchown(fd, ctx->uid, ctx->gid) < 0) - log_err("fchown %s fail: %s\n", strerror(errno)); + log_err("fchown %s fail: %s\n", savepath, strerror(errno)); if (ctx->total_size == 0) close(fd); diff --git a/src/log b/src/log new file mode 160000 index 0000000..f4ce2d9 --- /dev/null +++ b/src/log @@ -0,0 +1 @@ +Subproject commit f4ce2d92785fcc7dd5b3bd25905824e676169277 diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 010dcd3..0000000 --- a/src/log.c +++ /dev/null @@ -1,143 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 Jianhui Zhao - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" - -static int log_threshold = LOG_DEBUG; -static bool log_initialized; -static const char *ident; - -void (*log_write)(int priority, const char *fmt, va_list ap); - -static const char *prioritynames[] = { - [LOG_EMERG] = "emerg", - [LOG_ALERT] = "alert", - [LOG_CRIT] = "crit", - [LOG_ERR] = "err", - [LOG_WARNING] = "warn", - [LOG_NOTICE] = "notice", - [LOG_INFO] = "info", - [LOG_DEBUG] = "debug" -}; - -static const char *log_ident() -{ - FILE *self; - static char line[64]; - char *p = NULL; - char *sbuf; - - if ((self = fopen("/proc/self/status", "r")) != NULL) { - while (fgets(line, sizeof(line), self)) { - if (!strncmp(line, "Name:", 5)) { - strtok_r(line, "\t\n", &sbuf); - p = strtok_r(NULL, "\t\n", &sbuf); - break; - } - } - fclose(self); - } - - return p; -} - -static inline void log_write_stdout(int priority, const char *fmt, va_list ap) -{ - time_t now; - struct tm tm; - char buf[32]; - - now = time(NULL); - localtime_r(&now, &tm); - strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", &tm); - - fprintf(stderr, "%s %s ", buf, prioritynames[priority]); - vfprintf(stderr, fmt, ap); -} - -static inline void log_write_syslog(int priority, const char *fmt, va_list ap) -{ - vsyslog(priority, fmt, ap); -} - -static inline void log_init() -{ - if (log_initialized) - return; - - ident = log_ident(); - - if (isatty(STDOUT_FILENO)) { - log_write = log_write_stdout; - } else { - log_write = log_write_syslog; - - openlog(ident, 0, LOG_DAEMON); - } - - log_initialized = true; -} - - -void set_log_threshold(int threshold) -{ - log_threshold = threshold; -} - -void log_close() -{ - if (!log_initialized) - return; - - closelog(); - - log_initialized = 0; -} - -void __ilog(const char *filename, int line, int priority, const char *fmt, ...) -{ - static char new_fmt[256]; - va_list ap; - - priority = LOG_PRI(priority); - - if (priority > log_threshold) - return; - - log_init(); - - snprintf(new_fmt, sizeof(new_fmt), "(%s:%d) %s", filename, line, fmt); - - va_start(ap, fmt); - log_write(priority, new_fmt, ap); - va_end(ap); -} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 2f5134d..0000000 --- a/src/log.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2019 Jianhui Zhao - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#ifndef _LOG_H -#define _LOG_H - -#include -#include - -void set_log_threshold(int threshold); - -void log_close(); - -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) - -#define log(priority, fmt...) __ilog(__FILENAME__, __LINE__, priority, fmt) - -#define log_debug(fmt...) log(LOG_DEBUG, fmt) -#define log_info(fmt...) log(LOG_INFO, fmt) -#define log_warn(fmt...) log(LOG_WARNING, fmt) -#define log_err(fmt...) log(LOG_ERR, fmt) - -void __ilog(const char *filename, int line, int priority, const char *fmt, ...); - -#endif diff --git a/src/main.c b/src/main.c index 53382a3..ac8328f 100644 --- a/src/main.c +++ b/src/main.c @@ -27,7 +27,7 @@ #include #include -#include "log.h" +#include "log/log.h" #include "rtty.h" enum { @@ -197,8 +197,8 @@ int main(int argc, char **argv) if (background && daemon(0, 0)) log_err("Can't run in the background: %s\n", strerror(errno)); - if (!verbose) - set_log_threshold(LOG_ERR); + if (verbose) + log_level(LOG_DEBUG); log_info("rtty version %s\n", RTTY_VERSION_STRING); diff --git a/src/net.c b/src/net.c index 69283ac..f78ab66 100644 --- a/src/net.c +++ b/src/net.c @@ -32,7 +32,7 @@ #include "list.h" #include "net.h" -#include "log.h" +#include "log/log.h" struct net_context { struct ev_timer tmr; diff --git a/src/rtty.c b/src/rtty.c index ed9014d..5ee2065 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -29,13 +29,13 @@ #include #include "net.h" -#include "log.h" #include "web.h" #include "file.h" #include "rtty.h" #include "list.h" #include "utils.h" #include "command.h" +#include "log/log.h" static char login_path[128]; /* /bin/login */ diff --git a/src/web.c b/src/web.c index b3a19ce..6944130 100644 --- a/src/web.c +++ b/src/web.c @@ -33,7 +33,7 @@ #include "web.h" #include "net.h" -#include "log.h" +#include "log/log.h" void web_request_free(struct web_request_ctx *ctx) {