-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathdlog.c
More file actions
1409 lines (1256 loc) · 35 KB
/
Copy pathdlog.c
File metadata and controls
1409 lines (1256 loc) · 35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* (C) Copyright 2016-2024 Intel Corporation.
* (C) Copyright 2025-2026 Hewlett Packard Enterprise Development LP
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
/**
* This file is part of CaRT. It implements message logging system.
*/
#include <fcntl.h>
#include <errno.h>
#include <inttypes.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <gurt/dlog.h>
#include <gurt/common.h>
#include <gurt/list.h>
/* extra tag bytes to alloc for a pid */
#define DLOG_TAGPAD 16
enum {
/** minimum log file size is 1MB */
LOG_SIZE_MIN = (1ULL << 20),
/** default log file size is 2GB */
LOG_SIZE_DEF = (1ULL << 31),
};
/**
* internal global state
*/
struct d_log_state {
/** logfile name [malloced] */
char *log_file;
/** backup file name */
char *log_old;
/** buffer to cache log messages */
char *log_buf;
/** Number Of Bytes in the buffer */
off_t log_buf_nob;
/** fd of the open logfile */
int log_fd;
/** fd of the backup logfile, only for log_sync on assertion failure */
int log_old_fd;
/** current size of log file */
uint64_t log_size;
/** log size of last time check */
uint64_t log_last_check_size;
/** max size of log file */
uint64_t log_size_max;
/** Callback to get thread id and ULT id */
d_log_id_cb_t log_id_cb;
/* note: tag, dlog_facs, and fac_cnt are in xstate now */
int def_mask; /* default facility mask value */
int stderr_mask; /* mask above which we send to stderr */
int oflags; /* open flags */
int fac_alloc; /* # of slots in facs[] (>=fac_cnt) */
struct utsname uts; /* for hostname, from uname(3) */
int stdout_isatty; /* non-zero if stdout is a tty */
int stderr_isatty; /* non-zero if stderr is a tty */
int flush_pri; /* flush priority */
bool append_rank; /* append rank to the log filename */
bool rank_appended; /* flag to indicate if rank is already appended */
};
static pthread_mutex_t clogmux = PTHREAD_MUTEX_INITIALIZER; /* protect clog in threaded env */
struct cache_entry {
int *ce_cache;
d_list_t ce_link;
int ce_nr;
};
/*
* global data. Zero initialization means the log is not open.
* this is global so clog_filter() in dlog.h can get at it.
*/
struct d_log_xstate d_log_xst;
static struct d_log_state mst;
static d_list_t d_log_caches;
/* default name for facility 0 */
static const char *default_fac0name = "CLOG";
/* whether we should merge log and stderr */
static bool merge_stderr;
#define clog_lock() (void)pthread_mutex_lock(&clogmux)
#define clog_unlock() (void)pthread_mutex_unlock(&clogmux)
static int d_log_write(char *buf, int len, bool flush);
static const char *clog_pristr(int);
static int clog_setnfac(int);
/* static arrays for converting between pri's and strings */
static const char *const norm[] = { "DEBUG", "INFO ", "NOTE ", "WARN ", "ERR ",
"CRIT ", "ALRT ", "EMRG ", "EMIT "};
/**
* clog_pristr: convert priority to 4 byte symbolic name.
*
* \param[in] pri the priority to convert to a string
*
* \return the string (symbolic name) of the priority
*/
static const char *clog_pristr(int pri)
{
int s;
s = DLOG_PRI(pri);
if (s >= (sizeof(norm) / sizeof(char *))) {
/* prevent checksum warning */
s = 0;
}
return norm[s];
}
/**
* clog_setnfac: set the number of facilities allocated (including default
* to a given value). clog must be open for this to do anything.
* we set the default name for facility 0 here.
* caller must hold clogmux.
*
* \param[in] n the number of facilities to allocate space for now.
*
* \return zero on success, -1 on error.
*/
static int clog_setnfac(int n)
{
int try, lcv;
struct dlog_fac *nfacs;
/*
* no need to check d_log_xst.tag to see if clog is open or not,
* since caller holds clogmux already it must be ok.
*/
/* hmm, already done */
if (n <= d_log_xst.fac_cnt)
return 0;
/* can we expand in place? */
if (n <= mst.fac_alloc) {
d_log_xst.fac_cnt = n;
return 0;
}
/* must grow the array */
try = (n < 1024) ? (n + 32) : n; /* pad a bit for small values of n */
nfacs = calloc(1, try * sizeof(*nfacs));
if (!nfacs)
return -1;
/* over the hump, setup the new array */
lcv = 0;
if (d_log_xst.dlog_facs && d_log_xst.fac_cnt) { /* copy old? */
for (/*null */ ; lcv < d_log_xst.fac_cnt; lcv++)
nfacs[lcv] = d_log_xst.dlog_facs[lcv];
}
for (/*null */ ; lcv < try; lcv++) { /* init the new */
nfacs[lcv].fac_mask = mst.def_mask;
nfacs[lcv].fac_aname =
(lcv == 0) ? (char *)default_fac0name : NULL;
nfacs[lcv].fac_lname = NULL;
nfacs[lcv].is_enabled = true; /* enable all facs by default */
}
/* install */
if (d_log_xst.dlog_facs)
free(d_log_xst.dlog_facs);
d_log_xst.dlog_facs = nfacs;
d_log_xst.fac_cnt = n;
mst.fac_alloc = try;
return 0;
}
/**
* clog_bput: copy a string to a buffer, counting the bytes
*
* @param bpp pointer to output pointer (we advance it)
* @param skippy pointer to bytes to skip
* @param residp pointer to length of buffer remaining
* @param totcp pointer to total bytes moved counter
* @param str the string to copy in (null to just add a \0)
*/
static void clog_bput(char **bpp, int *skippy, int *residp, int *totcp,
const char *str)
{
static const char *nullsrc = "X\0\0"; /* 'X' is a non-null dummy char */
const char *sp;
if (str == NULL) /* trick to allow a null insert */
str = nullsrc;
for (sp = str; *sp; sp++) {
if (sp == nullsrc)
sp++; /* skip over 'X' to null */
if (totcp)
(*totcp)++; /* update the total */
if (skippy && *skippy > 0) {
(*skippy)--; /* honor skip */
continue;
}
if (*residp > 0 && *bpp != NULL) {
/* copyout if buffer w/space */
**bpp = *sp;
(*bpp)++;
(*residp)--;
}
}
}
static void
reset_caches(bool lock_held)
{
struct cache_entry *ce;
int i;
if (!lock_held)
clog_lock();
d_list_for_each_entry(ce, &d_log_caches, ce_link) {
for (i = 0; i < ce->ce_nr; i++)
ce->ce_cache[i] = DLOG_UNINIT;
}
if (!lock_held)
clog_unlock();
}
void
d_log_add_cache(int *cache, int nr)
{
struct cache_entry *ce;
/* Can't use D_ALLOC yet */
ce = malloc(sizeof(*ce));
if (ce == NULL)
return;
clog_lock();
ce->ce_cache = cache;
ce->ce_nr = nr;
d_list_add(&ce->ce_link, &d_log_caches);
clog_unlock();
}
/**
* dlog_cleanout: release previously allocated resources (e.g. from a
* close or during a failed open).
*
* the caller handles cleanout of d_log_xst.tag (not us).
*/
static void dlog_cleanout(void)
{
struct cache_entry *ce;
int lcv;
clog_lock();
if (mst.log_file) {
if (mst.log_fd >= 0) {
d_log_write(NULL, 0, true);
close(mst.log_fd);
}
mst.log_fd = -1;
free(mst.log_file);
mst.log_file = NULL;
}
if (mst.log_old) {
if (mst.log_old_fd >= 0)
close(mst.log_old_fd);
mst.log_old_fd = -1;
free(mst.log_old);
mst.log_old = NULL;
}
if (mst.log_buf) {
free(mst.log_buf);
mst.log_buf = NULL;
mst.log_buf_nob = 0;
}
if (d_log_xst.dlog_facs) {
/*
* free malloced facility names, being careful not to free
* the static default_fac0name....
*/
for (lcv = 0; lcv < mst.fac_alloc; lcv++) {
if (d_log_xst.dlog_facs[lcv].fac_aname &&
d_log_xst.dlog_facs[lcv].fac_aname !=
default_fac0name)
free(d_log_xst.dlog_facs[lcv].fac_aname);
if (d_log_xst.dlog_facs[lcv].fac_lname)
free(d_log_xst.dlog_facs[lcv].fac_lname);
}
free(d_log_xst.dlog_facs);
d_log_xst.dlog_facs = NULL;
d_log_xst.fac_cnt = 0;
mst.fac_alloc = 0;
}
reset_caches(true); /* Log is going away, reset cached masks */
while ((ce = d_list_pop_entry(&d_log_caches,
struct cache_entry, ce_link)))
free(ce);
clog_unlock();
}
static __thread int pre_err;
static __thread int pre_err_line;
static __thread uint64_t pre_err_time;
#define dlog_print_err(err, fmt, ...) \
do { \
struct timeval _tv; \
gettimeofday(&_tv, NULL); \
if (pre_err_line == __LINE__ && pre_err == (err) && _tv.tv_sec <= pre_err_time) \
break; \
pre_err_time = _tv.tv_sec; \
pre_err_line = __LINE__; \
pre_err = err; \
fprintf(stderr, "%s: %d: err=%d (%s) " fmt, __func__, __LINE__, err, \
strerror(err), ##__VA_ARGS__); \
} while (0)
#define LOG_BUF_SIZE (16 << 10)
static bool
log_exceed_threshold(void)
{
struct stat st;
int rc;
if (!merge_stderr)
goto out;
/**
* if we merge stderr to log file which is not
* calculated by log_size, to avoid exceeding threshold
* too much, log_size will be updated with fstat if log
* size increased by 2% of max size every time.
*/
if ((mst.log_size - mst.log_last_check_size) < (mst.log_size_max / 50))
goto out;
rc = fstat(mst.log_fd, &st);
if (!rc)
mst.log_size = st.st_size;
mst.log_last_check_size = mst.log_size;
out:
return mst.log_size + mst.log_buf_nob >= mst.log_size_max;
}
/* exceeds the size threshold, rename the current log file
* as backup, create a new log file.
*/
static int
log_rotate(void)
{
int rc = 0;
if (!mst.log_old) {
rc = asprintf(&mst.log_old, "%s.first", mst.log_file);
if (rc < 0) {
dlog_print_err(errno, "failed to alloc name\n");
return -1;
}
} else {
free(mst.log_old);
rc = asprintf(&mst.log_old, "%s.old", mst.log_file);
if (rc < 0) {
dlog_print_err(errno, "failed to alloc name\n");
return -1;
}
}
if (mst.log_old_fd >= 0) {
close(mst.log_old_fd);
mst.log_old_fd = -1;
}
/* rename the current log file as a backup */
rc = rename(mst.log_file, mst.log_old);
if (rc) {
dlog_print_err(errno, "failed to rename log file\n");
return -1;
}
mst.log_old_fd = mst.log_fd;
/* create a new log file */
if (merge_stderr) {
if (freopen(mst.log_file, "w", stderr) == NULL) {
fprintf(stderr, "d_log_write(): cannot open new %s: %s\n",
mst.log_file, strerror(errno));
return -1;
}
mst.log_fd = fileno(stderr);
} else {
mst.log_fd = open(mst.log_file, O_RDWR | O_CREAT, 0644);
if (mst.log_fd < 0) {
fprintf(stderr, "d_log_write(): failed to recreate log file %s: %s\n",
mst.log_file, strerror(errno));
return -1;
}
rc = fcntl(mst.log_fd, F_DUPFD, 128);
if (rc < 0) {
fprintf(stderr,
"d_log_write(): failed to recreate log file %s: %s\n",
mst.log_file, strerror(errno));
close(mst.log_fd);
return -1;
}
close(mst.log_fd);
mst.log_fd = rc;
}
mst.log_size = 0;
mst.log_last_check_size = 0;
return rc;
}
/**
* This function can do a few things:
* - copy log message @msg to log buffer
* - if the log buffer is full, write it to log file
* - if the log file is too large (exceeds to threshold), rename it to
* original_name.old, and create a new log file.
*
* If @flush is true, it writes log buffer to log file immediately.
*/
static int
d_log_write(char *msg, int len, bool flush)
{
int rc;
if (mst.log_fd < 0)
return 0;
if (len >= LOG_BUF_SIZE) {
dlog_print_err(0, "message='%.64s' is too long, len=%d\n",
msg, len);
return 0;
}
if (!mst.log_buf) {
mst.log_buf = malloc(LOG_BUF_SIZE);
if (!mst.log_buf) {
dlog_print_err(ENOMEM, "failed to alloc log buffer\n");
return -1;
}
}
D_ASSERT(!msg || len);
again:
if (msg && (len <= LOG_BUF_SIZE - mst.log_buf_nob)) {
/* the current buffer is not full */
strncpy(&mst.log_buf[mst.log_buf_nob], msg, len);
mst.log_buf_nob += len;
if (!flush)
return 0; /* short path done */
msg = NULL; /* already copied into log buffer */
len = 0;
}
/* write log buffer to log file */
if (mst.log_buf_nob == 0)
return 0; /* nothing to write */
/* rotate the log if it exceeds the threshold */
if (log_exceed_threshold()) {
rc = log_rotate();
if (rc != 0)
return rc;
}
/* flush the cached log messages */
rc = write(mst.log_fd, mst.log_buf, mst.log_buf_nob);
if (rc < 0) {
int err = errno;
dlog_print_err(err, "failed to write log %d\n", mst.log_fd);
if (err == EBADF)
mst.log_fd = -1;
return -1;
}
mst.log_size += mst.log_buf_nob;
mst.log_buf_nob = 0;
if (msg) /* the current message is not processed yet */
goto again;
return 0;
}
void
d_log_sync(void)
{
int rc = 0;
clog_lock();
if (mst.log_buf_nob > 0) /* write back the in-flight buffer */
rc = d_log_write(NULL, 0, true);
/* Skip flush if there was a problem on write */
if (mst.log_fd >= 0 && rc == 0) {
rc = fsync(mst.log_fd);
if (rc < 0) {
int err = errno;
dlog_print_err(err, "failed to sync log file %d\n", mst.log_fd);
if (err == EBADF)
mst.log_fd = -1;
}
}
if (mst.log_old_fd >= 0) {
rc = fsync(mst.log_old_fd);
if (rc < 0)
dlog_print_err(errno, "failed to sync log backup %d\n", mst.log_old_fd);
close(mst.log_old_fd);
mst.log_old_fd = -1; /* nobody is going to write it again */
}
clog_unlock();
}
void
d_log_disable_logging(void)
{
mst.log_fd = -1;
mst.log_old_fd = -1;
}
static __thread uint32_t dlog_tid = -1;
static __thread uint32_t dlog_pid = -1;
/**
* d_vlog: core log function, front-ended by d_log
* we vsnprintf the message into a holding buffer to format it. then we
* send it to all target output logs. the holding buffer is set to
* DLOG_TBSIZ, if the message is too long it will be silently truncated.
* caller should not hold clogmux, d_vlog will grab it as needed.
*
* @param flags returned by d_log_check
* @param fmt the printf(3) format to use
* @param ap the stdargs va_list to use for the printf format
*
* @note Any changes to this function must be reflected in the CaRT log parser.
* @see src/tests/ftest/cart/util/cart_logparser.py
*/
void d_vlog(int flags, const char *fmt, va_list ap)
{
#define DLOG_TBSIZ 1024 /* bigger than any line should be */
static __thread char buf[DLOG_TBSIZ];
static uint64_t last_flush;
uint64_t uid = 0;
int fac, lvl, pri;
bool flush;
char *buf_nopt1hdr, facstore[16], *facstr;
struct timeval tv;
struct tm *tm;
unsigned int hlen_pt1, hlen, mlen, tlen;
/*
* since we ignore any potential errors in CLOG let's always re-set
* errno to its original value
*/
int save_errno = errno;
int rc;
if (flags == 0)
return;
fac = flags & DLOG_FACMASK;
lvl = flags & DLOG_PRIMASK;
pri = flags & DLOG_PRINDMASK;
/* Check the facility so we don't crash. We will just log the message
* in this case but it really is indicative of a usage error as user
* didn't pass sanitized flags to this routine
*/
if (fac >= d_log_xst.fac_cnt)
fac = 0;
/* Assumes stderr mask isn't used for debug messages */
if (mst.stderr_mask != 0 && lvl >= mst.stderr_mask)
flags |= DLOG_STDERR;
if ((mst.oflags & DLOG_FLV_TAG) && (mst.oflags & DLOG_FLV_LOGPID)) {
/* Init static members in ahead of lock */
if (unlikely(dlog_pid == (uint32_t)(-1)))
dlog_pid = (uint32_t)getpid();
if (unlikely(dlog_tid == (uint32_t)(-1))) {
if (mst.log_id_cb)
mst.log_id_cb(&dlog_tid, NULL);
else
dlog_tid = (uint32_t)syscall(SYS_gettid);
}
if (mst.log_id_cb)
mst.log_id_cb(NULL, &uid);
}
/*
* we must log it, start computing the parts of the log we'll need.
*/
clog_lock(); /* lock out other threads */
if (d_log_xst.dlog_facs[fac].fac_aname) {
facstr = d_log_xst.dlog_facs[fac].fac_aname;
} else {
snprintf(facstore, sizeof(facstore), "%d", fac);
facstr = facstore;
}
(void)gettimeofday(&tv, 0);
tm = localtime(&tv.tv_sec);
if (tm == NULL) {
dlog_print_err(errno, "localtime returned NULL\n");
clog_unlock();
return;
}
/*
* ok, first, put the header into b[]
*/
hlen = 0;
hlen += snprintf(buf, sizeof(buf), "%s ", clog_pristr(lvl));
if (mst.oflags & DLOG_FLV_YEAR)
hlen += snprintf(buf + hlen, sizeof(buf) - hlen, "%04d/", tm->tm_year + 1900);
hlen += snprintf(buf + hlen, sizeof(buf) - hlen, "%02d/%02d %02d:%02d:%02d.%06ld %s ",
tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
(long int)tv.tv_usec, mst.uts.nodename);
if (mst.oflags & DLOG_FLV_TAG) {
if (mst.oflags & DLOG_FLV_LOGPID) {
hlen += snprintf(buf + hlen, sizeof(buf) - hlen, "%s%d/%d/" DF_U64 "] ",
d_log_xst.tag, dlog_pid, dlog_tid, uid);
} else {
hlen += snprintf(buf + hlen, sizeof(buf) - hlen, "%s ", d_log_xst.tag);
}
}
hlen_pt1 = hlen; /* save part 1 length */
if (hlen < sizeof(buf) && mst.oflags & DLOG_FLV_FAC)
hlen += snprintf(buf + hlen, sizeof(buf) - hlen, "%-4s ", facstr);
/*
* we expect there is still room (i.e. at least one byte) for a
* message, so this overflow check should never happen, but let's
* check for it anyway.
*/
if (hlen + 1 >= sizeof(buf)) {
clog_unlock(); /* drop lock, this is the only early exit */
dlog_print_err(E2BIG, "header overflowed %zd byte buffer (%d)\n", sizeof(buf),
hlen + 1);
errno = save_errno;
return;
}
/*
* now slap in the user's data at the end of the buffer
*/
mlen = vsnprintf(buf + hlen, sizeof(buf) - hlen, fmt, ap);
/*
* compute total length, check for overflows... make sure the string
* ends in a newline.
*/
tlen = hlen + mlen;
/* after condition, tlen will point at index of null byte */
if (unlikely(tlen >= (sizeof(buf) - 1))) {
/* Either the string was truncated or the buffer is full. */
tlen = sizeof(buf) - 1;
} else {
/* it fits with a byte to spare, make sure it ends in newline */
if (unlikely(buf[tlen - 1] != '\n'))
tlen++;
}
/* Ensure it ends with '\n' and '\0' */
buf[tlen - 1] = '\n';
buf[tlen] = '\0';
buf_nopt1hdr = buf + hlen_pt1;
if (mst.oflags & DLOG_FLV_STDOUT)
flags |= DLOG_STDOUT;
if (mst.oflags & DLOG_FLV_STDERR)
flags |= DLOG_STDERR;
/* log message is ready to be dispatched, write to log file.
* NB: flush to logfile if the message is important (warning/error...)
* or the last flush was 1+ second ago.
*/
if (mst.flush_pri == DLOG_DBG)
flush = true;
else
flush = (lvl >= mst.flush_pri) || (tv.tv_sec > last_flush);
if (flush)
last_flush = tv.tv_sec;
rc = d_log_write(buf, tlen, flush);
if (rc < 0)
errno = save_errno;
clog_unlock(); /* drop lock here */
/*
* log it to stderr and/or stdout. skip part one of the header
* if the output channel is a tty
*/
if ((flags & DLOG_STDERR) && (pri != DLOG_EMIT)) {
if (mst.stderr_isatty)
fprintf(stderr, "%s", buf_nopt1hdr);
else
fprintf(stderr, "%s", buf);
}
if ((flags & DLOG_STDOUT) && (pri != DLOG_EMIT)) {
if (mst.stderr_isatty)
printf("%s", buf_nopt1hdr);
else
printf("%s", buf);
fflush(stdout);
}
/* done! */
errno = save_errno;
}
/*
* d_log_str2pri: convert a priority string to an int pri value to allow
* for more user-friendly programs. returns -1 (an invalid pri) on error.
* does not access clog global state.
*
* len should be the number of characters to evaluate, not including any
* termination character.
*/
static int d_log_str2pri(const char *pstr, size_t len)
{
int lcv;
int size;
/* make sure we have a valid input */
if (len == 0 || len > 7) {
/* prevent checksum warning */
return -1;
}
/*
* handle some quirks
*/
if (strncasecmp(pstr, "ERR", len) == 0 || strncasecmp(pstr, "ERROR", len) == 0)
/* has trailing space in the array */
return DLOG_ERR;
if (((strncasecmp(pstr, "DEBUG", len) == 0) ||
(strncasecmp(pstr, "DBUG", len) == 0))) {
/* check to see is debug mask bits are set */
return d_dbglog_data.dd_mask != 0 ?
d_dbglog_data.dd_mask : DLOG_DBG;
}
/*
* handle non-debug case
*/
size = sizeof(norm) / sizeof(char *);
for (lcv = 1; lcv < size; lcv++)
if (strncasecmp(pstr, norm[lcv], len) == 0)
return lcv << DLOG_PRISHIFT;
/* bogus! */
return -1;
}
/*
* d_getenv_size: interpret a string as a size which can
* contain a unit symbol.
*/
static uint64_t
d_getenv_size(char *env)
{
char *end;
uint64_t size;
size = strtoull(env, &end, 0);
switch (*end) {
default:
break;
case 'k':
size *= 1000;
break;
case 'm':
size *= 1000 * 1000;
break;
case 'g':
size *= 1000 * 1000 * 1000;
break;
case 'K':
size *= 1024;
break;
case 'M':
size *= 1024 * 1024;
break;
case 'G':
size *= 1024 * 1024 * 1024;
break;
}
return size;
}
/*
* d_log_open: open a clog (uses malloc, inits global state). you
* can only have one clog open at a time, but you can use multiple
* facilities.
*
* if an clog is already open, then this call will fail. if you use
* the message buffer, you prob want it to be 1K or larger.
*
* return 0 on success, -1 on error.
*/
int
d_log_open(char *tag, int maxfac_hint, int default_mask, int stderr_mask,
char *logfile, int flags, d_log_id_cb_t log_id_cb)
{
int tagblen;
char *newtag = NULL, *cp;
int truncate = 0, rc;
char *env;
char *buffer = NULL;
uint64_t log_size = LOG_SIZE_DEF;
int pri;
memset(&mst, 0, sizeof(mst));
mst.flush_pri = DLOG_WARN;
mst.log_id_cb = log_id_cb;
d_agetenv_str(&env, D_LOG_FLUSH_ENV);
if (env) {
pri = d_log_str2pri(env, strlen(env) + 1);
if (pri != -1)
mst.flush_pri = pri;
d_freeenv_str(&env);
}
d_agetenv_str(&env, D_LOG_TRUNCATE_ENV);
if (env != NULL && atoi(env) > 0)
truncate = 1;
d_freeenv_str(&env);
d_agetenv_str(&env, D_LOG_SIZE_ENV);
if (env != NULL) {
log_size = d_getenv_size(env);
if (log_size < LOG_SIZE_MIN)
log_size = LOG_SIZE_MIN;
d_freeenv_str(&env);
}
d_agetenv_str(&env, D_LOG_FILE_APPEND_PID_ENV);
if (logfile != NULL && env != NULL) {
if (strcmp(env, "0") != 0) {
rc = asprintf(&buffer, "%s.%d", logfile, getpid());
if (buffer != NULL && rc != -1)
logfile = buffer;
else
D_PRINT_ERR("Failed to append pid to "
"DAOS debug log name, "
"continuing.\n");
}
}
d_freeenv_str(&env);
d_agetenv_str(&env, D_LOG_FILE_APPEND_RANK_ENV);
if (env && strcmp(env, "0") != 0)
mst.append_rank = true;
d_freeenv_str(&env);
/* quick sanity check (mst.tag is non-null if already open) */
if (d_log_xst.tag || !tag ||
(maxfac_hint < 0) || (default_mask & ~DLOG_PRIMASK) ||
(stderr_mask & ~DLOG_PRIMASK)) {
fprintf(stderr, "d_log_open invalid parameter.\n");
goto early_error;
}
/* init working area so we can use dlog_cleanout to bail out */
mst.log_fd = -1;
mst.log_old_fd = -1;
/* start filling it in */
tagblen = strlen(tag) + DLOG_TAGPAD; /* add a bit for pid */
newtag = calloc(1, tagblen);
if (!newtag) {
fprintf(stderr, "d_log_open calloc failed.\n");
goto early_error;
}
/* it is now safe to use dlog_cleanout() for error handling */
clog_lock(); /* now locked */
D_INIT_LIST_HEAD(&d_log_caches);
if (flags & DLOG_FLV_LOGPID)
snprintf(newtag, tagblen, "%s[", tag);
else
snprintf(newtag, tagblen, "%s", tag);
mst.def_mask = default_mask;
mst.stderr_mask = stderr_mask;
if (logfile) {
int log_flags = O_RDWR | O_CREAT;
struct stat st;
d_agetenv_str(&env, D_LOG_STDERR_IN_LOG_ENV);
if (env != NULL && atoi(env) > 0)
merge_stderr = true;
d_freeenv_str(&env);
if (!truncate)
log_flags |= O_APPEND;
mst.log_file = strdup(logfile);
if (!mst.log_file) {
fprintf(stderr, "strdup failed.\n");
goto error;
}
/* merge stderr into log file, to aggregate and order with
* messages from Mercury/libfabric
*/
if (merge_stderr) {
if (freopen(mst.log_file, truncate ? "w" : "a", stderr) == NULL) {
fprintf(stderr, "d_log_open: cannot open %s: %s\n", mst.log_file,
strerror(errno));
goto error;
}
/* set per-line buffering to limit jumbling */
setlinebuf(stderr);
mst.log_fd = fileno(stderr);
} else {
mst.log_fd = open(mst.log_file, log_flags, 0644);
}
if (mst.log_fd < 0) {
fprintf(stderr, "d_log_open: cannot open %s: %s\n",
mst.log_file, strerror(errno));
goto error;
}
/* Use a higher, consistent, number for the daos log file. This isn't strictly
* necessary however it does avoid problems with the file descriptor being closed
* incorrectly when using the interception library, see DAOS-11119
*/
rc = fcntl(mst.log_fd, F_DUPFD, 128);
if (rc < 0) {
fprintf(stderr, "d_log_write(): failed to recreate log file %s: %s\n",
mst.log_file, strerror(errno));
close(mst.log_fd);
goto error;
}
close(mst.log_fd);
mst.log_fd = rc;
if (!truncate) {
rc = fstat(mst.log_fd, &st);
if (rc)
goto error;
mst.log_size = st.st_size;
}
mst.log_size_max = log_size;
}
mst.oflags = flags;
/* maxfac_hint should include default fac. */
if (clog_setnfac((maxfac_hint < 1) ? 1 : maxfac_hint) < 0) {
fprintf(stderr, "clog_setnfac failed.\n");
goto error;
}
(void)uname(&mst.uts);
d_log_xst.nodename = mst.uts.nodename; /* expose this */
/* chop off the domainname */
if ((flags & DLOG_FLV_FQDN) == 0) {
for (cp = mst.uts.nodename; *cp && *cp != '.'; cp++)
;
*cp = 0;
}
/* cache value of isatty() to avoid extra system calls */
mst.stdout_isatty = isatty(fileno(stdout));
mst.stderr_isatty = isatty(fileno(stderr));