logs: avoid multiple syscall(__NR_write) in logs
authorRobert Swiecki <robert@swiecki.net>
Sun, 16 Dec 2018 10:55:33 +0000 (11:55 +0100)
committerRobert Swiecki <robert@swiecki.net>
Sun, 16 Dec 2018 10:55:33 +0000 (11:55 +0100)
logs.cc

diff --git a/logs.cc b/logs.cc
index 13bfb77028a225b30db91f42c1483abfa6e41c2a..1912d39c5c25927d3347699d68bb79fdc94347b7 100644 (file)
--- a/logs.cc
+++ b/logs.cc
@@ -108,30 +108,47 @@ void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt
        };
 
        /* Start printing logs */
+       std::string msg;
        if (_log_fd_isatty) {
-               dprintf(_log_fd, "%s", logLevels[ll].prefix);
+               msg.append(logLevels[ll].prefix);
        }
+       msg.append("[").append(logLevels[ll].descr).append("]");
        if (logLevels[ll].print_time) {
-               const auto timestr = util::timeToStr(time(NULL));
-               dprintf(_log_fd, "[%s] ", timestr.c_str());
+               msg.append("[").append(util::timeToStr(time(NULL))).append("]");
        }
        if (logLevels[ll].print_funcline) {
-               dprintf(_log_fd, "[%s][%d] %s():%d ", logLevels[ll].descr, (int)getpid(), fn, ln);
+               msg.append("[")
+                   .append(std::to_string(getpid()))
+                   .append("] ")
+                   .append(fn)
+                   .append("():")
+                   .append(std::to_string(ln))
+                   .append(" ");
        }
 
        va_list args;
        va_start(args, fmt);
-       vdprintf(_log_fd, fmt, args);
+       char* strp;
        va_end(args);
+       if (vasprintf(&strp, fmt, args) == -1) {
+               msg.append("[logs internal]: MEMORY ALLOCATION ERROR");
+       } else {
+               msg.append(strp);
+               free(strp);
+       }
        if (perr) {
-               dprintf(_log_fd, ": %s", strerr);
+               msg.append(": ").append(strerr);
        }
        if (_log_fd_isatty) {
-               dprintf(_log_fd, "\033[0m");
+               msg.append("\033[0m");
        }
-       dprintf(_log_fd, "\n");
+       msg.append("\n");
        /* End printing logs */
 
+       if (write(_log_fd, msg.c_str(), msg.size()) == -1) {
+               dprintf(_log_fd, "%s", msg.c_str());
+       }
+
        if (ll == FATAL) {
                exit(0xff);
        }