Browse code

core: json logging - apply j flag for prefix field

Daniel-Constantin Mierla authored on 12/11/2021 13:17:18
Showing 1 changed files
... ...
@@ -615,33 +615,33 @@ static void ksr_slog_json_str_escape(str *s_in, str *s_out, int *emode)
615 615
 }
616 616
 
617 617
 #define KSR_SLOG_SYSLOG_JSON_FMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
618
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
618
+	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
619 619
 
620 620
 #define KSR_SLOG_SYSLOG_JSON_CFMT "{ \"level\": \"%s\", \"module\": \"%s\", \"file\": \"%s\"," \
621
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
621
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
622 622
 
623 623
 #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\", \"%smessage\": %s%.*s%s }%s"
624
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
625 625
 
626 626
 #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\"," \
627
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": %s%.*s%s," \
628 628
 	" \"%smessage\": %s%.*s%s }%s"
629 629
 
630 630
 #define KSR_SLOG_STDERR_JSON_FMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
631 631
 	" \"module\": \"%s\", \"file\": \"%s\"," \
632
-	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
632
+	" \"line\": %d, \"function\": \"%s\", %.*s\"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
633 633
 
634 634
 #define KSR_SLOG_STDERR_JSON_CFMT "{ \"idx\": %d, \"pid\": %d, \"level\": \"%s\"," \
635 635
 	" \"module\": \"%s\", \"file\": \"%s\"," \
636
-	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
636
+	" \"line\": %d, \"function\": \"%s\", \"callid\": \"%.*s\", \"logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
637 637
 
638 638
 #define KSR_SLOG_STDERR_JSON_PFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
639 639
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
640
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": \"%.*s\", \"%smessage\": %s%.*s%s }%s"
640
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", %.*s\"" NAME ".logprefix\": %s%.*s%s, \"%smessage\": %s%.*s%s }%s"
641 641
 
642 642
 #define KSR_SLOG_STDERR_JSON_CPFMT "{ \"" NAME ".idx\": %d, \"" NAME ".pid\": %d, \"" NAME ".level\": \"%s\"," \
643 643
 	" \"" NAME ".module\": \"%s\", \"" NAME ".file\": \"%s\"," \
644
-	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": \"%.*s\"," \
644
+	" \"" NAME ".line\": %d, \"" NAME ".function\": \"%s\", \"" NAME ".callid\": \"%.*s\", \"" NAME ".logprefix\": %s%.*s%s," \
645 645
 	" \"%smessage\": %s%.*s%s }%s"
646 646
 
647 647
 #ifdef HAVE_PTHREAD
... ...
@@ -675,6 +675,8 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
675 675
 	struct tm _tm;
676 676
 	char *smb = "\"";
677 677
 	char *sme = "\"";
678
+	char *pmb = "\"";
679
+	char *pme = "\"";
678 680
 
679 681
 	va_start(arglist, format);
680 682
 	n = vsnprintf(obuf + s_in.len, KSR_SLOG_MAX_SIZE - s_in.len, format, arglist);
... ...
@@ -701,6 +703,15 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
701 703
 			smb = "{ \"text\": \"";
702 704
 			sme = "\" }";
703 705
 		}
706
+		if((log_prefix_val!=NULL) && (log_prefix_val->len>1)
707
+				&& (log_prefix_val->s[0] == '{')
708
+				&& (log_prefix_val->s[log_prefix_val->len - 1] == '}')) {
709
+			pmb = "";
710
+			pme = "";
711
+		} else {
712
+			pmb = "{ \"text\": \"";
713
+			pme = "\" }";
714
+		}
704 715
 	}
705 716
 
706 717
 	if(s_out.s == NULL) {
... ...
@@ -753,7 +764,8 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
753 764
 				efmt, process_no, my_pid(),
754 765
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
755 766
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
756
-				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, smb, s_out.len, s_out.s, sme,
767
+				pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
768
+				prefmsg, smb, s_out.len, s_out.s, sme,
757 769
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
758 770
 			if (unlikely(log_color)) dprint_color_reset();
759 771
 		}
... ...
@@ -773,7 +785,8 @@ void ksr_slog_json(ksr_logdata_t *kld, const char *format, ...)
773 785
 				sfmt,
774 786
 				kld->v_lname, kld->v_mname, kld->v_fname, kld->v_fline,
775 787
 				kld->v_func, LOGV_CALLID_LEN, LOGV_CALLID_STR,
776
-				LOGV_PREFIX_LEN, LOGV_PREFIX_STR, prefmsg, smb, s_out.len, s_out.s, sme,
788
+				pmb, LOGV_PREFIX_LEN, LOGV_PREFIX_STR, pme,
789
+				prefmsg, smb, s_out.len, s_out.s, sme,
777 790
 				(_ksr_slog_json_flags & KSR_SLOGJSON_FL_NOLOGNL)?"":"\n");
778 791
 		}
779 792
 	}