]> 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)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 12 Jul 2021 20:51:16 +0000 (22:51 +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/main.c
lib/log.c
lib/log.h

index e482977b3d14e517aad6edeefbe00b9f9741ffac..53e6a9edcb1d91a15937ecd6d6dfb54859f74e3e 100644 (file)
@@ -674,14 +674,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) {
@@ -693,28 +693,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 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 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