]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
rework logging from control sockets (+simplify)
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 12 Jul 2021 18:02:12 +0000 (20:02 +0200)
committerTomas Krizek <tomas.krizek@nic.cz>
Thu, 29 Jul 2021 09:42:34 +0000 (11:42 +0200)
- 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

daemon/io.c
daemon/lua/kres-gen-29.lua
daemon/main.c
daemon/scripting.rst
lib/log.c
lib/log.h

index 84910a425356a339adb4bc2c9bd4f577980b569e..b105519b25a747cc47cab4d040bda6acdf4cf596 100644 (file)
@@ -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 */
index a136d6cc2e1617947481ccdab88b9f3d6006d5bc..2b540a95edee966a3ed2798e82c19b7934acab9a 100644 (file)
@@ -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;
index b88a95c0383d9f7480bba319920a11b6b8eeb276..736213801116c613bc659e94aec85a56434fa15d 100644 (file)
@@ -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);
index 629855c0aa509e3a241bf23e448ce243cd0b8d11..3a464605934680e0558e4a4846bac8f1c59355eb 100644 (file)
@@ -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
index 2dfad264f7a210b1b7d0171eae0f42a94f0e123e..6e27e583f3d681451b47f4abeb78ccb0ceb038e0 100644 (file)
--- 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 },
 };
index 51ccfd359e5d0602f9452fafdde41f0e58b60b11..993ad72e6c51edd870e9bc84c3b0aab4c4f0a0d4 100644 (file)
--- 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