Browse code

core: new flag p for json logging

- prefix is printed as it is in the root json, expected to be a list of
json field starting with comma

Daniel-Constantin Mierla authored on 16/11/2021 07:38:43
Showing 1 changed files
... ...
@@ -530,6 +530,7 @@ static int _ksr_slog_json_flags = 0;
530 530
 #define KSR_SLOGJSON_FL_NOAPPPREFIXMSG (1<<3)
531 531
 #define KSR_SLOGJSON_FL_CALLID (1<<4)
532 532
 #define KSR_SLOGJSON_FL_MSGJSON (1<<5)
533
+#define KSR_SLOGJSON_FL_PRFJSONFLD (1<<6)
533 534
 
534 535
 
535 536
 #define LOGV_CALLID_STR (((_ksr_slog_json_flags & KSR_SLOGJSON_FL_CALLID) \
... ...
@@ -615,33 +616,33 @@ static void ksr_slog_json_str_escape(str *s_in, str *s_out, int *emode)
615 616
 }
616 617
 
617 618
 #define KSR_SLOG_SYSLOG_JSON_FMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
618
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
619
+	" \"line\": %d, \"function\": \"%s\"%.*s%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
619 620
 
620 621
 #define KSR_SLOG_SYSLOG_JSON_CFMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
621
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
622
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\"%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
622 623
 
623 624
 #define KSR_SLOG_SYSLOG_JSON_PFMT "{ \"" NAME ".level\": \"%s\", \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
624
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
625
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\"%.*s%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
625 626
 
626 627
 #define KSR_SLOG_SYSLOG_JSON_CPFMT "{ \"" NAME ".level\": \"%s\", \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
627
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": %s%.*s%s," \
628
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\"%s%s%.*s%s," \
628 629
 	" \"%smessage\": %s%.*s%s }%s"
629 630
 
630 631
 #define KSR_SLOG_STDERR_JSON_FMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
631 632
 	" \"module\": \"%s\", \"file\": \"%s\"," \
632
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
633
+	" \"line\": %d, \"function\": \"%s\"%.*s%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
633 634
 
634 635
 #define KSR_SLOG_STDERR_JSON_CFMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
635 636
 	" \"module\": \"%s\", \"file\": \"%s\"," \
636
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
637
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\"%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
637 638
 
638 639
 #define KSR_SLOG_STDERR_JSON_PFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
639 640
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
640
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
641
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\"%.*s\"%s%s%.*s%s, \"%smessage\": %s%.*s%s }%s"
641 642
 
642 643
 #define KSR_SLOG_STDERR_JSON_CPFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
643 644
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
644
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": %s%.*s%s," \
645
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\"%s%s%.*s%s," \
645 646
 	" \"%smessage\": %s%.*s%s }%s"
646 647
 
647 648
 #ifdef HAVE_PTHREAD
... ...
@@ -677,6 +678,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
677 678
 	char *sme = "\"";
678 679
 	char *pmb = "\"";
679 680
 	char *pme = "\"";
681
+	char *prname = ", \"" NAME ".logprefix\": ";
680 682
 
681 683
 	va_start(arglist, format);
682 684
 	n = vsnprintf(obuf + s_in.len, KSR_SLOG_MAX_SIZE - s_in.len, format, arglist);
... ...
@@ -727,6 +729,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
727 729
 		} else {
728 730
 			prefmsg = NAME ".";
729 731
 		}
732
+		prname = ", \"" NAME ".logprefix\": ";
730 733
 		if(_ksr_slog_json_flags & KSR_SLOGJSON_FL_CALLID) {
731 734
 			efmt = KSR_SLOG_STDERR_JSON_CPFMT;
732 735
 			sfmt = KSR_SLOG_SYSLOG_JSON_CPFMT;
... ...
@@ -736,6 +739,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
736 739
 		}
737 740
 	} else {
738 741
 		prefmsg = "";
742
+		prname = ", \"logprefix\": ";
739 743
 		if(_ksr_slog_json_flags & KSR_SLOGJSON_FL_CALLID) {
740 744
 			efmt = KSR_SLOG_STDERR_JSON_CFMT;
741 745
 			sfmt = KSR_SLOG_SYSLOG_JSON_CFMT;
... ...
@@ -744,6 +748,14 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
744 748
 			sfmt = KSR_SLOG_SYSLOG_JSON_FMT;
745 749
 		}
746 750
 	}
751
+	if ((!log_cee) && (_ksr_slog_json_flags & KSR_SLOGJSON_FL_PRFJSONFLD)) {
752
+		if( (log_prefix_val==NULL) || (log_prefix_val->len<=0)
753
+				|| ((log_prefix_val->len>1) && (log_prefix_val->s[0] == ','))) {
754
+			prname = "";
755
+			pmb = "";
756
+			pme = "";
757
+		}
758
+	}
747 759
 	ksr_clock_gettime (&_tp);
748 760
 	gmtime_r (&_tp.tv_sec, &_tm);
749 761
 	strftime (iso8601buf, ISO8601_BUF_SIZE, "%FT%T", &_tm);
... ...
@@ -764,7 +776,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
764 776
 				efmt, process_no, my_pid(),
765 777
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
766 778
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
767
-				pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
779
+				prname, pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
768 780
 				prefmsg, smb, s_out.len, s_out.s, sme,
769 781
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
770 782
 			if (unlikely(log_color)) dprint_color_reset();
... ...
@@ -785,7 +797,7 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
785 797
 				sfmt,
786 798
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
787 799
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
788
-				pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
800
+				prname, pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
789 801
 				prefmsg, smb, s_out.len, s_out.s, sme,
790 802
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
791 803
 		}
... ...
@@ -821,12 +833,6 @@ void ksr_slog_init(char *ename)
821 833
 			_km_log_engine_data = p + 1;
822 834
 			while (*p) {
823 835
 				switch (*p) {
824
-					case 'M':
825
-						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_STRIPMSGNL;
826
-					break;
827
-					case 'N':
828
-						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_NOLOGNL;
829
-					break;
830 836
 					case 'a':
831 837
 						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_APPPREFIX;
832 838
 					break;
... ...
@@ -839,6 +845,15 @@ void ksr_slog_init(char *ename)
839 845
 					case 'j':
840 846
 						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_MSGJSON;
841 847
 					break;
848
+					case 'M':
849
+						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_STRIPMSGNL;
850
+					break;
851
+					case 'N':
852
+						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_NOLOGNL;
853
+					break;
854
+					case 'p':
855
+						_ksr_slog_json_flags |= KSR_SLOGJSON_FL_PRFJSONFLD;
856
+					break;
842 857
 					case 'U':
843 858
 						log_cee = 1;
844 859
 					break;