5
5
#include <stdarg.h>
6
6
#include <stdio.h>
7
7
#include <string.h>
8
+ #include <time.h>
9
+ #include <sys/time.h>
8
10
9
11
static const char * STR [] = {FOREACH_LOG_LEVEL (GENERATE_STRING )};
10
12
11
13
void
12
14
jrtc_default_va_logging (const char * domain , jrtc_logging_level level , const char * s , va_list arg )
13
15
{
14
- char output [LOGGING_BUFFER_LEN ];
15
- snprintf (output , LOGGING_BUFFER_LEN , "%s %s%s" , domain , STR [level ], s );
16
-
17
16
if (level >= jrtc_logger_level ) {
17
+ char output [LOGGING_BUFFER_LEN ];
18
+
19
+ // Get current time in milliseconds
20
+ struct timeval tv ;
21
+ gettimeofday (& tv , NULL );
22
+
23
+ // Format the timestamp
24
+ char timestamp [64 ];
25
+ struct tm * tm_info = gmtime (& tv .tv_sec );
26
+ strftime (timestamp , sizeof (timestamp ), "%Y-%m-%dT%H:%M:%S" , tm_info );
27
+ snprintf (timestamp + strlen (timestamp ), sizeof (timestamp ) - strlen (timestamp ), ".%06ldZ" , tv .tv_usec );
28
+
29
+ // Add timestamp and log level
30
+ snprintf (output , LOGGING_BUFFER_LEN , "%s %s%s%s" , timestamp , domain , STR [level ], s );
31
+
18
32
FILE * where = level >= INFO ? stderr : stdout ;
19
33
vfprintf (where , output , arg );
20
34
fflush (where );
@@ -26,7 +40,17 @@ jrtc_default_logging(const char* domain, jrtc_logging_level level, const char* s
26
40
{
27
41
if (level >= jrtc_logger_level ) {
28
42
char output [LOGGING_BUFFER_LEN ];
29
- snprintf (output , LOGGING_BUFFER_LEN , "%s %s%s" , domain , STR [level ], s );
43
+ // Get current time in milliseconds
44
+ struct timeval tv ;
45
+ gettimeofday (& tv , NULL );
46
+
47
+ // Format the timestamp
48
+ char timestamp [64 ];
49
+ struct tm * tm_info = gmtime (& tv .tv_sec );
50
+ strftime (timestamp , sizeof (timestamp ), "%Y-%m-%dT%H:%M:%S" , tm_info );
51
+ snprintf (timestamp + strlen (timestamp ), sizeof (timestamp ) - strlen (timestamp ), ".%06ldZ" , tv .tv_usec );
52
+
53
+ snprintf (output , LOGGING_BUFFER_LEN , "%s %s %s" , timestamp , domain , s );
30
54
va_list ap ;
31
55
va_start (ap , s );
32
56
FILE * where = level >= INFO ? stderr : stdout ;
0 commit comments