MCPcopy Create free account
hub / github.com/apache/brpc / PrintLogPrefixAsJSON

Function PrintLogPrefixAsJSON

src/butil/logging.cc:896–949  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

894}
895
896static void PrintLogPrefixAsJSON(std::ostream& os, int severity,
897 butil::StringPiece file,
898 butil::StringPiece func,
899 int line, TimeVal tv) {
900 // severity
901 os << "\"L\":\"";
902 if (severity < 0) {
903 os << 'V' << -severity;
904 } else if (severity < LOG_NUM_SEVERITIES) {
905 os << log_severity_names[severity][0];
906 } else {
907 os << 'U';
908 }
909 // time
910 os << "\",\"T\":\"";
911 time_t t = tv.tv_sec;
912 struct tm local_tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
913#if _MSC_VER >= 1400
914 localtime_s(&local_tm, &t);
915#else
916 localtime_r(&t, &local_tm);
917#endif
918 const char prev_fill = os.fill('0');
919 if (FLAGS_log_year) {
920 os << std::setw(4) << local_tm.tm_year + 1900;
921 }
922 os << std::setw(2) << local_tm.tm_mon + 1
923 << std::setw(2) << local_tm.tm_mday << ' '
924 << std::setw(2) << local_tm.tm_hour << ':'
925 << std::setw(2) << local_tm.tm_min << ':'
926 << std::setw(2) << local_tm.tm_sec;
927#if defined(OS_LINUX) || defined(OS_MACOSX)
928 os << '.' << std::setw(6) << tv.tv_usec;
929#endif
930 os << "\",";
931 os.fill(prev_fill);
932
933 if (FLAGS_log_pid) {
934 os << "\"pid\":\"" << CurrentProcessId() << "\",";
935 }
936 os << "\"tid\":\"" << butil::PlatformThread::CurrentId() << "\",";
937 if (FLAGS_log_hostname) {
938 butil::StringPiece hostname(butil::my_hostname());
939 if (hostname.ends_with(".baidu.com")) { // make it shorter
940 hostname.remove_suffix(10);
941 }
942 os << "\"host\":\"" << hostname << "\",";
943 }
944 os << "\"C\":\"" << file << ':' << line;
945 if (!func.empty()) {
946 os << " " << func;
947 }
948 os << "\"";
949}
950
951void EscapeJson(std::ostream& os, const butil::StringPiece& s) {
952 for (auto it = s.begin(); it != s.end(); it++) {

Callers 1

PrintLogFunction · 0.85

Calls 5

CurrentProcessIdFunction · 0.85
my_hostnameFunction · 0.85
ends_withMethod · 0.80
remove_suffixMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected