From: Vladimír Čunát Date: Mon, 12 Jul 2021 18:02:12 +0000 (+0200) Subject: rework logging from control sockets (+simplify) X-Git-Tag: v5.4.0~2^2~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=072348acf807e5a2eefa3281aa0774fd63ef1abd;p=thirdparty%2Fknot-resolver.git rework logging from control sockets (+simplify) - unify interactive mode to stdout - use its own logging group - elevated log level when the command throws an exception - don't try detecting that the logs go back into the same console (yes, in that case you can see some lines twice) - don't make the binary mode turn off logging --- diff --git a/daemon/io.c b/daemon/io.c index 84910a425..b105519b2 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -675,14 +675,14 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu goto next_iter; } - int ret = engine_cmd(L, cmd, false); + const bool cmd_failed = engine_cmd(L, cmd, false); const char *message = NULL; size_t len_s; if (lua_gettop(L) > 0) { message = lua_tolstring(L, -1, &len_s); } - /* Simpler output in binary mode */ + /* Send back the output, either in "binary" or normal mode. */ if (data->mode == io_mode_binary) { /* Leader expects length field in all cases */ if (!message || len_s > UINT32_MAX) { @@ -694,28 +694,24 @@ void io_tty_process_input(uv_stream_t *stream, ssize_t nread, const uv_buf_t *bu fwrite(&len_n, sizeof(len_n), 1, out); if (len_s > 0) fwrite(message, len_s, 1, out); - goto next_iter; - } - - /* Log to remote socket if connected */ - const char *delim = args->quiet ? "" : "> "; - if (stream_fd != STDIN_FILENO) { - if (VERBOSE_STATUS) - fprintf(stdout, "%s\n", cmd); /* Duplicate command to logs */ + } else { if (message) - fprintf(out, "%s", message); /* Duplicate output to sender */ + fprintf(out, "%s", message); if (message || !args->quiet) fprintf(out, "\n"); - fprintf(out, "%s", delim); + if (!args->quiet) + fprintf(out, "> "); } - if (stream_fd == STDIN_FILENO || VERBOSE_STATUS) { - /* Log to standard streams */ - FILE *fp_out = ret ? stderr : stdout; + + /* Duplicate command and output to logs */ + if (cmd_failed) { + kr_log_warning(CONTROL, "> %s\n", cmd); + if (message) + kr_log_warning(CONTROL, "%s\n", message); + } else { + kr_log_debug(CONTROL, "> %s\n", cmd); if (message) - fprintf(fp_out, "%s", message); - if (message && !args->quiet) - fprintf(fp_out, "\n"); - fprintf(fp_out, "%s", delim); + kr_log_debug(CONTROL, "%s\n", message); } next_iter: lua_settop(L, 0); /* not required in some cases but harmless */ diff --git a/daemon/lua/kres-gen-29.lua b/daemon/lua/kres-gen-29.lua index a136d6cc2..2b540a95e 100644 --- a/daemon/lua/kres-gen-29.lua +++ b/daemon/lua/kres-gen-29.lua @@ -307,7 +307,7 @@ struct kr_server_selection { struct local_state *local_state; }; typedef int log_level_t; -enum kr_log_group {LOG_GRP_SYSTEM = 1, LOG_GRP_CACHE, LOG_GRP_IO, LOG_GRP_NETWORK, LOG_GRP_TA, LOG_GRP_TLS, LOG_GRP_GNUTLS, LOG_GRP_TLSCLIENT, LOG_GRP_XDP, LOG_GRP_ZIMPORT, LOG_GRP_ZSCANNER, LOG_GRP_DOH, LOG_GRP_DNSSEC, LOG_GRP_HINT, LOG_GRP_PLAN, LOG_GRP_ITERATOR, LOG_GRP_VALIDATOR, LOG_GRP_RESOLVER, LOG_GRP_SELECTION, LOG_GRP_ZCUT, LOG_GRP_COOKIES, LOG_GRP_STATISTICS, LOG_GRP_REBIND, LOG_GRP_WORKER, LOG_GRP_POLICY, LOG_GRP_TASENTINEL, LOG_GRP_TASIGNALING, LOG_GRP_TAUPDATE, LOG_GRP_DAF, LOG_GRP_DETECTTIMEJUMP, LOG_GRP_DETECTTIMESKEW, LOG_GRP_GRAPHITE, LOG_GRP_PREFILL, LOG_GRP_PRIMING, LOG_GRP_SRVSTALE, LOG_GRP_WATCHDOG, LOG_GRP_NSID, LOG_GRP_DNSTAP, LOG_GRP_TESTS, LOG_GRP_DOTAUTH, LOG_GRP_HTTP, LOG_GRP_DEVEL}; +enum kr_log_group {LOG_GRP_SYSTEM = 1, LOG_GRP_CACHE, LOG_GRP_IO, LOG_GRP_NETWORK, LOG_GRP_TA, LOG_GRP_TLS, LOG_GRP_GNUTLS, LOG_GRP_TLSCLIENT, LOG_GRP_XDP, LOG_GRP_ZIMPORT, LOG_GRP_ZSCANNER, LOG_GRP_DOH, LOG_GRP_DNSSEC, LOG_GRP_HINT, LOG_GRP_PLAN, LOG_GRP_ITERATOR, LOG_GRP_VALIDATOR, LOG_GRP_RESOLVER, LOG_GRP_SELECTION, LOG_GRP_ZCUT, LOG_GRP_COOKIES, LOG_GRP_STATISTICS, LOG_GRP_REBIND, LOG_GRP_WORKER, LOG_GRP_POLICY, LOG_GRP_TASENTINEL, LOG_GRP_TASIGNALING, LOG_GRP_TAUPDATE, LOG_GRP_DAF, LOG_GRP_DETECTTIMEJUMP, LOG_GRP_DETECTTIMESKEW, LOG_GRP_GRAPHITE, LOG_GRP_PREFILL, LOG_GRP_PRIMING, LOG_GRP_SRVSTALE, LOG_GRP_WATCHDOG, LOG_GRP_NSID, LOG_GRP_DNSTAP, LOG_GRP_TESTS, LOG_GRP_DOTAUTH, LOG_GRP_HTTP, LOG_GRP_CONTROL, LOG_GRP_DEVEL}; kr_layer_t kr_layer_t_static; _Bool kr_dbg_assertion_abort; diff --git a/daemon/main.c b/daemon/main.c index b88a95c03..736213801 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -161,10 +161,8 @@ static int run_worker(uv_loop_t *loop, struct engine *engine, bool leader, struc uv_pipe_t *pipe = malloc(sizeof(*pipe)); uv_pipe_init(loop, pipe, 0); if (args->interactive) { - if (!args->quiet) { - kr_log_notice(SYSTEM, "interactive mode\n"); - printf("> "); - } + if (!args->quiet) + printf("Interactive mode:\n" "> "); pipe->data = io_tty_alloc_data(); uv_pipe_open(pipe, 0); uv_read_start((uv_stream_t*)pipe, io_tty_alloc, io_tty_process_input); diff --git a/daemon/scripting.rst b/daemon/scripting.rst index 629855c0a..3a4646059 100644 --- a/daemon/scripting.rst +++ b/daemon/scripting.rst @@ -50,10 +50,10 @@ set a new one. There are some basic commands to start with. The *direct output* of commands sent over socket is captured and sent back, -while also printed to the daemon standard outputs (in :func:`verbose` mode). -This gives you an immediate response on the outcome of your command. Error or -debug logs aren't captured, but you can find them in the daemon standard -outputs. +which gives you an immediate response on the outcome of your command. +The commands and their output are also logged in ``contrl`` group, +on ``debug`` level if successful or ``warning`` level if failed +(see around :func:`set_log_level`). Control sockets are also a way to enumerate and test running instances, the list of sockets corresponds to the list of processes, and you can test the diff --git a/lib/log.c b/lib/log.c index 2dfad264f..6e27e583f 100644 --- a/lib/log.c +++ b/lib/log.c @@ -70,6 +70,7 @@ log_group_names_t log_group_names[] = { GRP_NAME_ITEM(LOG_GRP_TESTS), GRP_NAME_ITEM(LOG_GRP_DOTAUTH), GRP_NAME_ITEM(LOG_GRP_HTTP), + GRP_NAME_ITEM(LOG_GRP_CONTROL), GRP_NAME_ITEM(LOG_GRP_DEVEL), { NULL, -1 }, }; diff --git a/lib/log.h b/lib/log.h index 51ccfd359..993ad72e6 100644 --- a/lib/log.h +++ b/lib/log.h @@ -66,6 +66,7 @@ enum kr_log_group { LOG_GRP_TESTS, LOG_GRP_DOTAUTH, LOG_GRP_HTTP, + LOG_GRP_CONTROL, /* ^^ Add new log groups above ^^. */ LOG_GRP_DEVEL, /* Must be last entry in enum! */ }; @@ -116,6 +117,7 @@ typedef struct { #define LOG_GRP_TESTS_TAG "tests" #define LOG_GRP_DOTAUTH_TAG "dotaut" #define LOG_GRP_HTTP_TAG "http" +#define LOG_GRP_CONTROL_TAG "contrl" #define LOG_GRP_DEVEL_TAG "devel" KR_EXPORT