@@ -27,6 +27,7 @@ static const char *bunyan_name = NULL;
27
27
static char * bunyan_buf = NULL ;
28
28
static size_t bunyan_buf_sz = 0 ;
29
29
static enum bunyan_log_level bunyan_min_level = WARN ;
30
+ static boolean_t bunyan_omit_timestamp = B_FALSE ;
30
31
31
32
struct bunyan_var {
32
33
struct bunyan_var * bv_next ;
@@ -260,9 +261,59 @@ bny_timer_print(struct bunyan_timers *tms)
260
261
return (0 );
261
262
}
262
263
264
+ #if defined(__linux__ )
265
+ static boolean_t
266
+ bunyan_detect_journald (void )
267
+ {
268
+ char * envp ;
269
+
270
+ /*
271
+ * If journald is providing logging for this process, it will set
272
+ * JOURNAL_STREAM to <dev>:<inode>, corresponding to the device and
273
+ * inode of the socket attached to stderr and stdout.
274
+ */
275
+ if ((envp = getenv ("JOURNAL_STREAM" )) != NULL ) {
276
+ char * devp , * inop ;
277
+ unsigned long device = ULONG_MAX , inode = ULONG_MAX ;
278
+
279
+ if ((devp = strdup (envp )) != NULL ) {
280
+ inop = strstr (devp , ":" );
281
+ if (inop != NULL ) {
282
+ * inop = '\0' ;
283
+ inop ++ ;
284
+ device = strtoul (devp , NULL , 10 );
285
+ inode = strtoul (inop , NULL , 10 );
286
+ }
287
+ free (devp );
288
+ }
289
+
290
+ if (device != ULONG_MAX && inode != ULONG_MAX ) {
291
+ struct stat info ;
292
+
293
+ if (fstat (STDERR_FILENO , & info ) == 0 ) {
294
+ if (info .st_dev == device &&
295
+ info .st_ino == inode ) {
296
+ return (B_TRUE );
297
+ }
298
+ }
299
+ }
300
+ }
301
+ return (B_FALSE );
302
+ }
303
+ #endif /* defined (__linux__) */
304
+
263
305
void
264
306
bunyan_init (void )
265
307
{
308
+ #if defined(__linux__ )
309
+ /*
310
+ * When logging to journald, generating our own timestamps is
311
+ * unecessary as the journal has its own native ones.
312
+ */
313
+ if (bunyan_detect_journald ()) {
314
+ bunyan_omit_timestamp = B_TRUE ;
315
+ }
316
+ #endif
266
317
}
267
318
268
319
void
@@ -469,7 +520,6 @@ print_frame(struct bunyan_frame *frame, uint *pn, struct bunyan_var **evars)
469
520
void
470
521
bunyan_log (enum bunyan_log_level level , const char * msg , ...)
471
522
{
472
- char time [128 ];
473
523
va_list ap ;
474
524
const char * propname ;
475
525
errf_t * err = NULL ;
@@ -480,8 +530,12 @@ bunyan_log(enum bunyan_log_level level, const char *msg, ...)
480
530
481
531
reset_buf ();
482
532
483
- bunyan_timestamp (time , sizeof (time ));
484
- printf_buf ("[%s] " , time );
533
+ if (!bunyan_omit_timestamp ) {
534
+ char time [128 ];
535
+
536
+ bunyan_timestamp (time , sizeof (time ));
537
+ printf_buf ("[%s] " , time );
538
+ }
485
539
486
540
switch (level ) {
487
541
case TRACE :
0 commit comments