logging: Add a timestamp to the log messages we emit
This commit is contained in:
@@ -94,7 +94,10 @@ error to set both --verbose and --quiet. The last one wins.
|
|||||||
|
|
||||||
The log line format is:
|
The log line format is:
|
||||||
|
|
||||||
<LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
|
<TIMESTAMP> <LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
|
||||||
|
|
||||||
|
*TIMESTAMP*:
|
||||||
|
Time the log entry was made. This is expressed as milliseconds since the epoc.
|
||||||
|
|
||||||
*LEVEL*:
|
*LEVEL*:
|
||||||
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
|
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
|
||||||
|
@@ -278,7 +278,10 @@ error to set both --verbose and --quiet. The last one wins.
|
|||||||
|
|
||||||
The log line format is:
|
The log line format is:
|
||||||
|
|
||||||
<LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
|
<TIMESTAMP> <LEVEL>:<PID> <THREAD> <SOURCEFILE>:<SOURCELINE>: <MSG>
|
||||||
|
|
||||||
|
*TIMESTAMP*:
|
||||||
|
Time the log entry was made. This is expressed as milliseconds since the epoc.
|
||||||
|
|
||||||
*LEVEL*:
|
*LEVEL*:
|
||||||
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
|
This will be one of 'D', 'I', 'W', 'E', 'F' in increasing order of
|
||||||
|
2
Rakefile
2
Rakefile
@@ -22,7 +22,7 @@ TEST_SOURCES = FileList['tests/unit/*.c']
|
|||||||
TEST_OBJECTS = TEST_SOURCES.pathmap( "%{^tests/unit,build/tests}X.o" )
|
TEST_OBJECTS = TEST_SOURCES.pathmap( "%{^tests/unit,build/tests}X.o" )
|
||||||
|
|
||||||
LIBS = %w( pthread )
|
LIBS = %w( pthread )
|
||||||
LDFLAGS = []
|
LDFLAGS = ["-lrt"]
|
||||||
CCFLAGS = %w(
|
CCFLAGS = %w(
|
||||||
-D_GNU_SOURCE=1
|
-D_GNU_SOURCE=1
|
||||||
-Wall
|
-Wall
|
||||||
|
20
src/util.c
20
src/util.c
@@ -6,6 +6,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
@@ -50,6 +51,25 @@ void mylog(int line_level, const char* format, ...)
|
|||||||
va_end(argptr);
|
va_end(argptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t monotonic_time_ms()
|
||||||
|
{
|
||||||
|
struct timespec ts;
|
||||||
|
uint64_t seconds_ms, nanoseconds_ms;
|
||||||
|
|
||||||
|
FATAL_IF_NEGATIVE(
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts),
|
||||||
|
SHOW_ERRNO( "clock_gettime failed" )
|
||||||
|
);
|
||||||
|
|
||||||
|
seconds_ms = ts.tv_sec;
|
||||||
|
seconds_ms = seconds_ms * 1000;
|
||||||
|
|
||||||
|
nanoseconds_ms = ts.tv_nsec;
|
||||||
|
nanoseconds_ms = nanoseconds_ms / 1000000;
|
||||||
|
|
||||||
|
return seconds_ms + nanoseconds_ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void* xrealloc(void* ptr, size_t size)
|
void* xrealloc(void* ptr, size_t size)
|
||||||
{
|
{
|
||||||
|
@@ -8,6 +8,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
void* xrealloc(void* ptr, size_t size);
|
void* xrealloc(void* ptr, size_t size);
|
||||||
void* xmalloc(size_t size);
|
void* xmalloc(size_t size);
|
||||||
@@ -85,9 +86,13 @@ void error_handler(int fatal);
|
|||||||
/* mylog a line at the given level (0 being most verbose) */
|
/* mylog a line at the given level (0 being most verbose) */
|
||||||
void mylog(int line_level, const char* format, ...);
|
void mylog(int line_level, const char* format, ...);
|
||||||
|
|
||||||
|
/* Returns the current time, in milliseconds, from CLOCK_MONOTONIC */
|
||||||
|
uint64_t monotonic_time_ms(void);
|
||||||
|
|
||||||
|
|
||||||
#define levstr(i) (i==0?'D':(i==1?'I':(i==2?'W':(i==3?'E':'F'))))
|
#define levstr(i) (i==0?'D':(i==1?'I':(i==2?'W':(i==3?'E':'F'))))
|
||||||
|
|
||||||
#define myloglev(level, msg, ...) mylog( level, "%c:%d %p %s:%d: "msg"\n", levstr(level), getpid(),pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__ )
|
#define myloglev(level, msg, ...) mylog( level, "%"PRIu64":%c:%d %p %s:%d: "msg"\n", monotonic_time_ms(), levstr(level), getpid(),pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__ )
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
# define debug(msg, ...) myloglev(0, msg, ##__VA_ARGS__)
|
# define debug(msg, ...) myloglev(0, msg, ##__VA_ARGS__)
|
||||||
|
Reference in New Issue
Block a user