Add timestamp when logging to stdout
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "telnet.h"
|
#include "telnet.h"
|
||||||
@@ -83,18 +84,40 @@ typedef struct {
|
|||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
#define gettid() (pid_t)syscall(__NR_gettid)
|
#define gettid() (pid_t)syscall(__NR_gettid)
|
||||||
|
|
||||||
|
static const char *_hms_txt(char *buf, unsigned int len)
|
||||||
|
{
|
||||||
|
struct timeval tv;
|
||||||
|
unsigned int sod, hh, mm, ss;
|
||||||
|
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
|
||||||
|
sod = tv.tv_sec % (24 * 3600);
|
||||||
|
hh = sod / 3600;
|
||||||
|
mm = (sod - hh * 3600) / 60;
|
||||||
|
ss = sod - hh * 3600 - mm * 60;
|
||||||
|
snprintf(buf, len, "%02d:%02d:%02d.%06d",
|
||||||
|
hh, mm, ss, (unsigned int)tv.tv_usec);
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
_PRF2_ static void _log(int prio, const char *fmt, ...)
|
_PRF2_ static void _log(int prio, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list arg;
|
va_list arg;
|
||||||
char buf[256];
|
char buf[256], buft[64];
|
||||||
|
|
||||||
va_start(arg, fmt);
|
va_start(arg, fmt);
|
||||||
vsnprintf(buf, sizeof(buf), fmt, arg);
|
vsnprintf(buf, sizeof(buf), fmt, arg);
|
||||||
|
|
||||||
if (ttynvt_param.logstd)
|
if (ttynvt_param.logstd)
|
||||||
printf("[%d] %d: %s", gettid(), prio, buf);
|
{
|
||||||
|
printf("%s: [%d] %d: %s",
|
||||||
|
_hms_txt(buft, sizeof(buft)), gettid(), prio, buf);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
syslog(prio, "[%d] %s", gettid(), buf);
|
syslog(prio, "[%d] %s", gettid(), buf);
|
||||||
|
}
|
||||||
|
|
||||||
va_end(arg);
|
va_end(arg);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user