Fix a little regression in the JSON format 30/243830/2
authorMateusz Majewski <m.majewski2@samsung.com>
Thu, 10 Sep 2020 10:00:28 +0000 (12:00 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Thu, 10 Sep 2020 11:23:53 +0000 (13:23 +0200)
We've forgotten the case in which the tag/message are empty in JSON
format. This commit fixes it.

Change-Id: I3e2deaf0595e46bd92ab763ae9b95e62aa9dc9f7

src/shared/logprint.c
src/tests/logprint.c

index c78df4a..d4f9fef 100644 (file)
@@ -838,21 +838,6 @@ static int resolve_write(int fd, struct log_write_buffer *wrbuf, const char *rea
        }
 }
 
-static int log_print_line_format_json(int fd, const dlogutil_entry_s *entry, struct log_write_buffer *wrbuf)
-{
-       size_t buflen;
-       // TODO: This could really use an improvement
-       char *const buf = log_format_json(wrbuf->data + wrbuf->position, wrbuf->size - wrbuf->position, entry, &buflen, entry->msg, entry->msg + entry->tag_len + 1);
-       if (!buf)
-               return -1;
-       bool to_free = buf != (wrbuf->data + wrbuf->position);
-
-       int res = resolve_write(fd, wrbuf, buf, buflen);
-       if (to_free)
-               free(buf);
-       return res;
-}
-
 #define METADATA_MAX_LEN 128
 #define COLORED_METADATA_MAX_LEN (METADATA_MAX_LEN  + sizeof COLOR_PURPLE + sizeof COLOR_RESET)
 
@@ -1190,6 +1175,21 @@ static size_t scan_lines(const char *message, bool split_on_eol)
        return result;
 }
 
+static int log_print_line_format_json(int fd, const dlogutil_entry_s *entry, struct log_write_buffer *wrbuf)
+{
+       size_t buflen;
+       // TODO: This could really use an improvement
+       char *const buf = log_format_json(wrbuf->data + wrbuf->position, wrbuf->size - wrbuf->position, entry, &buflen, entry_get_tag_string(entry), entry_get_message_string(entry));
+       if (!buf)
+               return -1;
+       bool to_free = buf != (wrbuf->data + wrbuf->position);
+
+       int res = resolve_write(fd, wrbuf, buf, buflen);
+       if (to_free)
+               free(buf);
+       return res;
+}
+
 /**
  * @brief Print log line
  * @details Prints the given log entry to the supplied file descriptor
index 2a5b912..8d484c2 100644 (file)
@@ -288,6 +288,17 @@ void check_log_print_log_line()
                {FORMAT_RECV_REALTIME, DLOG_INFO, false, "02-01 00:05:17.734 I/DLOG_ERROR_NOTAG(P 4228, T 4567): Tagless\n"},
                {FORMAT_BRIEF        , DLOG_INFO, false, "I/DLOG_ERROR_NOTAG( 4228): Tagless\n"                            },
                {FORMAT_PROCESS      , DLOG_INFO, false, "I( 4228) Tagless  (DLOG_ERROR_NOTAG)\n"                          },
+               {FORMAT_JSON         , DLOG_INFO, false, "{"
+                       "\"priority\":\"info\","
+                       "\"pid\":4228,"
+                       "\"tid\":4567,"
+                       "\"recv_real\":\"1970-02-01T00:05:17+0000\","
+                       "\"recv_mono\":365467.346253662,"
+                       "\"sent_real\":\"1970-03-14T23:22:51+0000\","
+                       "\"sent_mono\":3205048.235123213,"
+                       "\"tag\":\"DLOG_ERROR_NOTAG\","
+                       "\"msg\":\"Tagless\""
+               "}\n"},
        };
        CHECK_LOGPRINT_TESTCASES("\0Tagless", tests_tagless);
 
@@ -357,6 +368,17 @@ void check_log_print_log_line()
                {FORMAT_PROCESS, DLOG_INFO, false, "I( 4228) DLOG_ERROR_NOMSG  (SOME_TAG)\n"                                  },
                {FORMAT_LONG   , DLOG_INFO, false, "[ 03-14 23:22:51.451 I/SOME_TAG P 4228, T 4567]\n"
                                                   "DLOG_ERROR_NOMSG\n\n"                                                     },
+               {FORMAT_JSON   , DLOG_INFO, false, "{"
+                       "\"priority\":\"info\","
+                       "\"pid\":4228,"
+                       "\"tid\":4567,"
+                       "\"recv_real\":\"1970-02-01T00:05:17+0000\","
+                       "\"recv_mono\":365467.346253662,"
+                       "\"sent_real\":\"1970-03-14T23:22:51+0000\","
+                       "\"sent_mono\":3205048.235123213,"
+                       "\"tag\":\"SOME_TAG\","
+                       "\"msg\":\"DLOG_ERROR_NOMSG\""
+               "}\n"},
 
                {FORMAT_BRIEF  , DLOG_WARN,  true, "\033[33;1mW/SOME_TAG( 4228): \033[0mDLOG_ERROR_NOMSG\n"                   },
                {FORMAT_RAW    , DLOG_WARN,  true, "DLOG_ERROR_NOMSG\n"                                                       },