util: implement utilTimeToStr
authorRobert Swiecki <robert@swiecki.net>
Wed, 21 Jun 2017 16:46:19 +0000 (18:46 +0200)
committerRobert Swiecki <robert@swiecki.net>
Wed, 21 Jun 2017 16:46:19 +0000 (18:46 +0200)
subproc.c
util.c
util.h

index b9bb9ce..060432c 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -190,8 +190,8 @@ static void subprocRemove(struct nsjconf_t *nsjconf, pid_t pid)
        struct pids_t *p;
        TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
                if (p->pid == pid) {
-                       LOG_D("Removing pid '%d' from the queue (IP:'%s', start time:'%u')", p->pid,
-                             p->remote_txt, (unsigned int)p->start);
+                       LOG_D("Removing pid '%d' from the queue (IP:'%s', start time:'%s')", p->pid,
+                             p->remote_txt, utilTimeToStr(p->start));
                        close(p->pid_syscall_fd);
                        TAILQ_REMOVE(&nsjconf->pids, p, pointers);
                        free(p);
diff --git a/util.c b/util.c
index 1aba2fd..58c7c93 100644 (file)
--- a/util.c
+++ b/util.c
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <time.h>
 
 #include "log.h"
 
@@ -244,19 +245,28 @@ uint64_t utilRnd64(void)
 }
 
 extern const char *sys_sigabbrev[];
-static __thread char arch_signame[32];
-
+static __thread char sigstr[32];
 const char *utilSigName(int signo)
 {
        if (signo < 0 || signo > _NSIG) {
-               snprintf(arch_signame, sizeof(arch_signame), "UNKNOWN-%d", signo);
-               return arch_signame;
+               snprintf(sigstr, sizeof(sigstr), "UNKNOWN-%d", signo);
+               return sigstr;
        }
        if (signo > __SIGRTMIN) {
-               snprintf(arch_signame, sizeof(arch_signame), "SIG%d-RTMIN+%d", signo,
-                        signo - __SIGRTMIN);
-               return arch_signame;
+               snprintf(sigstr, sizeof(sigstr), "SIG%d-RTMIN+%d", signo, signo - __SIGRTMIN);
+               return sigstr;
+       }
+       snprintf(sigstr, sizeof(sigstr), "SIG%s", sys_sigabbrev[signo]);
+       return sigstr;
+}
+
+static __thread char timestr[64];
+const char *utilTimeToStr(time_t t)
+{
+       struct tm utctime;
+       localtime_r(&t, &utctime);
+       if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
+               return "[Time conv error]";
        }
-       snprintf(arch_signame, sizeof(arch_signame), "SIG%s", sys_sigabbrev[signo]);
-       return arch_signame;
+       return timestr;
 }
diff --git a/util.h b/util.h
index 221f507..e21df5a 100644 (file)
--- a/util.h
+++ b/util.h
@@ -41,5 +41,6 @@ int utilSSnPrintf(char *str, size_t size, const char *format, ...);
 bool utilIsANumber(const char *s);
 uint64_t utilRnd64(void);
 const char *utilSigName(int signo);
+const char *utilTimeToStr(time_t t);
 
 #endif                         /* NS_UTIL_H */