From 9d2b67ede55e02c539d3a7787ac52cf6ecc7a338 Mon Sep 17 00:00:00 2001 From: Lipeng Zhu Date: Tue, 18 Jun 2024 05:45:28 -0400 Subject: [PATCH 1/2] Introduce log.h to decouple serverLog to server.h. Signed-off-by: Lipeng Zhu --- src/Makefile | 2 +- src/log.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 88 +++++++++++++++++++++++++++++++++++ src/server.c | 106 ------------------------------------------ src/server.h | 30 +----------- 5 files changed, 218 insertions(+), 136 deletions(-) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/Makefile b/src/Makefile index 302ad06b84..30e0047e75 100644 --- a/src/Makefile +++ b/src/Makefile @@ -401,7 +401,7 @@ endif ENGINE_NAME=valkey SERVER_NAME=$(ENGINE_NAME)-server$(PROG_SUFFIX) ENGINE_SENTINEL_NAME=$(ENGINE_NAME)-sentinel$(PROG_SUFFIX) -ENGINE_SERVER_OBJ=threads_mngr.o adlist.o quicklist.o ae.o anet.o dict.o kvstore.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o cluster_legacy.o crc16.o endianconv.o slowlog.o eval.o bio.o rio.o rand.o memtest.o syscheck.o crcspeed.o crccombine.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o valkey-check-rdb.o valkey-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o socket.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script_lua.o script.o functions.o function_lua.o commands.o strl.o connection.o unix.o logreqres.o +ENGINE_SERVER_OBJ=threads_mngr.o adlist.o quicklist.o ae.o anet.o dict.o kvstore.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o cluster_legacy.o crc16.o endianconv.o slowlog.o eval.o bio.o rio.o rand.o memtest.o syscheck.o crcspeed.o crccombine.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o valkey-check-rdb.o valkey-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o socket.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script_lua.o script.o functions.o function_lua.o commands.o strl.o connection.o unix.o logreqres.o log.o ENGINE_CLI_NAME=$(ENGINE_NAME)-cli$(PROG_SUFFIX) ENGINE_CLI_OBJ=anet.o adlist.o dict.o valkey-cli.o zmalloc.o release.o ae.o serverassert.o crcspeed.o crccombine.o crc64.o siphash.o crc16.o monotonic.o cli_common.o mt19937-64.o strl.o cli_commands.o ENGINE_BENCHMARK_NAME=$(ENGINE_NAME)-benchmark$(PROG_SUFFIX) diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000000..9dfb68c358 --- /dev/null +++ b/src/log.c @@ -0,0 +1,128 @@ +#include "log.h" + +#include "util.h" + +#include +#include +#include + +/* We use a private localtime implementation which is fork-safe. The logging + * function of the server may be called from other threads. */ +void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst); + +/* Low level logging from signal handler. Should be used with pre-formatted strings. + See serverLogFromHandler. */ +void valkeyLogRawFromHandler(int level, int daemonize, const char *logfile, const char *msg) { + int fd; + int log_to_stdout = logfile[0] == '\0'; + char buf[64]; + + if (log_to_stdout && daemonize) return; + fd = log_to_stdout ? STDOUT_FILENO : open(logfile, O_APPEND | O_CREAT | O_WRONLY, 0644); + if (fd == -1) return; + if (level & LL_RAW) { + if (write(fd, msg, strlen(msg)) == -1) goto err; + } else { + ll2string(buf, sizeof(buf), getpid()); + if (write(fd, buf, strlen(buf)) == -1) goto err; + if (write(fd, ":signal-handler (", 17) == -1) goto err; + ll2string(buf, sizeof(buf), time(NULL)); + if (write(fd, buf, strlen(buf)) == -1) goto err; + if (write(fd, ") ", 2) == -1) goto err; + if (write(fd, msg, strlen(msg)) == -1) goto err; + if (write(fd, "\n", 1) == -1) goto err; + } +err: + if (!log_to_stdout) close(fd); +} + +/* An async-signal-safe version of serverLog. if LL_RAW is not included in level flags, + * The message format is: :signal-handler (