diff --git a/frameworks/libhilog/utils/log_print.cpp b/frameworks/libhilog/utils/log_print.cpp index 6d47a9ae3c225ceea271f73b9d091f5cd0f76d5e..db77571f3e6ee33ed6cb4733c3fd0ba5bcb01e7c 100644 --- a/frameworks/libhilog/utils/log_print.cpp +++ b/frameworks/libhilog/utils/log_print.cpp @@ -121,18 +121,22 @@ static void PrintLogPrefix(const LogContent& content, const LogFormat& format, s out << "Invalid time accuracy format" << endl; return; } - out << setfill(' '); - // 2. print pid/tid - out << " " << setw(PID_WIDTH) << content.pid << " " << setw(PID_WIDTH) << content.tid; - // 3. print level - out << " " << LogLevel2ShortStr(content.level) << " "; - // 4. print log type - out << GetLogTypePrefix(content.type); - // 5. print domain - out << setfill('0'); - out << hex << setw(DOMAIN_WIDTH) << ShortDomain(content.domain) << dec; - // 5. print tag & log - out << "/" << content.tag << ": "; + if (content.type != LOG_KMSG) { + out << setfill(' '); + // 2. print pid/tid + out << " " << setw(PID_WIDTH) << content.pid << " " << setw(PID_WIDTH) << content.tid; + // 3. print level + out << " " << LogLevel2ShortStr(content.level) << " "; + // 4. print log type + out << GetLogTypePrefix(content.type); + // 5. print domain + out << setfill('0'); + out << hex << setw(DOMAIN_WIDTH) << ShortDomain(content.domain) << dec; + // 5. print tag & log + out << "/" << content.tag << ": "; + } else { + out << " " << content.tag << " "; + } } void LogPrintWithFormat(const LogContent& content, const LogFormat& format, std::ostream& out) diff --git a/interfaces/rust/src/macros.rs b/interfaces/rust/src/macros.rs index 2a557a76eca7c8be31864671063dc590fe79bc3b..544c4c98d5391cb7e6f8566c05c020d196382739 100644 --- a/interfaces/rust/src/macros.rs +++ b/interfaces/rust/src/macros.rs @@ -21,7 +21,7 @@ macro_rules! hilog { (@call $log_label:ident, $level:expr, $fmt:literal, $(,)? $($processed_args:expr),* ) => ( let log_str = format!($fmt, $($processed_args),*); - let res = unsafe { + let res = unsafe { $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32, CString::new($log_label.tag).expect("default tag").as_ptr() as *const c_char, CString::new(log_str).expect("default log").as_ptr() as *const c_char) @@ -81,12 +81,12 @@ macro_rules! hilog { } /// printf log at the debug level. -/// +/// /// #Examples -/// +/// /// ``` /// use hilog_rust::{debug, hilog, HiLogLabel, LogType}; -/// +/// /// # fn main() { /// let log_label: HiLogLabel = HiLogLabel { /// log_type: LogType::LogCore, @@ -104,12 +104,12 @@ macro_rules! debug{ } /// printf log at the info level. -/// +/// /// #Examples -/// +/// /// ``` /// use hilog_rust::{info, hilog, HiLogLabel, LogType}; -/// +/// /// # fn main() { /// let log_label: HiLogLabel = HiLogLabel { /// log_type: LogType::LogCore, @@ -127,12 +127,12 @@ macro_rules! info{ } /// printf log at the warn level. -/// +/// /// #Examples -/// +/// /// ``` /// use hilog_rust::{warn, hilog, HiLogLabel, LogType}; -/// +/// /// # fn main() { /// let log_label: HiLogLabel = HiLogLabel { /// log_type: LogType::LogCore, @@ -150,12 +150,12 @@ macro_rules! warn{ } /// printf log at the error level. -/// +/// /// #Examples -/// +/// /// ``` /// use hilog_rust::{error, hilog, HiLogLabel, LogType}; -/// +/// /// # fn main() { /// let log_label: HiLogLabel = HiLogLabel { /// log_type: LogType::LogCore, @@ -173,12 +173,12 @@ macro_rules! error{ } /// printf log at the fatal level. -/// +/// /// #Examples -/// +/// /// ``` /// use hilog_rust::{fatal, hilog, HiLogLabel, LogType}; -/// +/// /// # fn main() { /// let log_label: HiLogLabel = HiLogLabel { /// log_type: LogType::LogCore, diff --git a/services/hilogd/kmsg_parser.cpp b/services/hilogd/kmsg_parser.cpp index eefe7c5fa0fb9bda654889bc631b9c2baba3f92a..c7e96c486dfb9e15317200abb2358f83e21e4dc0 100644 --- a/services/hilogd/kmsg_parser.cpp +++ b/services/hilogd/kmsg_parser.cpp @@ -34,7 +34,7 @@ namespace OHOS { namespace HiviewDFX { using namespace std::chrono; using namespace std::literals; - +constexpr double TRANS_USEC_NUM = 1000000.0; constexpr int DEC = 10; // Avoid name collision between sys/syslog.h and our log_c.h @@ -54,45 +54,6 @@ using Priority = enum { PV6 }; -static void ParseHeader(std::string& str, uint16_t* level, uint64_t* timestamp) -{ - static const std::string pattern = "(\\d+),(\\d+),(\\d+),(\\S);"; - static const std::regex express(pattern); - std::match_results res; - if (std::regex_search(str.begin(), str.end(), res, express)) { - *level = strtoul(res[1].str().c_str(), nullptr, DEC); - *timestamp = strtoumax(res[3].str().c_str(), nullptr, DEC); - str.erase(res.position(), res.length()); - } -} - -// Parse pid if exists -static uint32_t ParsePid(std::string& str) -{ - static const std::string pattern = "\\[pid=(\\d+)\\]"; - static const std::regex express(pattern); - std::match_results res; - if (std::regex_search(str.begin(), str.end(), res, express)) { - uint32_t ret = strtoumax(res[1].str().c_str(), nullptr, DEC); - str.erase(res.position(), res.length()); - return ret; - } - return 0; -} - -static std::string ParseTag(std::string& str) -{ - static const std::string pattern = "\\[.*?\\]"; - static const std::regex express(pattern); - std::match_results res; - if (std::regex_search(str.begin(), str.end(), res, express)) { - std::string ret = res[0].str(); - str.erase(res.position(), res.length()); - return ret; - } - return {}; -} - // Log levels are different in syslog.h and hilog log_c.h static uint16_t KmsgLevelMap(uint16_t prio) { @@ -120,48 +81,32 @@ static uint16_t KmsgLevelMap(uint16_t prio) return level; } -static constexpr timespec TimepointToTimespec(time_point tp) +static void ParseHeader(std::string& str, uint8_t& level, std::string& tag) { - auto secs = time_point_cast(tp); - auto nsecs = time_point_cast(tp) - time_point_cast(secs); - return timespec{secs.time_since_epoch().count(), nsecs.count()}; + static const std::string pattern = "(\\d+),(\\d+),(\\d+),(\\S);"; + static const std::regex express(pattern); + std::match_results res; + std::string levelStr; + uint64_t usec; + if (std::regex_search(str.begin(), str.end(), res, express)) { + level = strtoul(res[1].str().c_str(), nullptr, DEC); + levelStr = res[1].str(); + usec = strtoul(res[3].str().c_str(), nullptr, DEC); + str.erase(res.position(), res.length()); + } else { + return; + } + double sec = static_cast(usec) / TRANS_USEC_NUM; + tag = "<" + levelStr + ">" + " [" + std::to_string(sec) + "]"; } -// Kmsg has microseconds from system boot. Now get the time of system boot. -KmsgParser::BootTp KmsgParser::BootTime() -{ - struct timespec t_uptime; - clock_gettime(CLOCK_BOOTTIME, &t_uptime); - auto uptime = seconds{t_uptime.tv_sec} + nanoseconds{t_uptime.tv_nsec}; - auto current = system_clock::now(); - auto boottime = current - uptime; - return boottime; -} std::optional KmsgParser::ParseKmsg(const std::vector& kmsgBuffer) { std::string kmsgStr(kmsgBuffer.data()); - std::vector mtag(MAX_TAG_LEN, '\0'); - uint16_t mLevel = 0; - uint64_t timestamp = 0; - ParseHeader(kmsgStr, &mLevel, ×tamp); - // Parses pid if exists. Pid in kmsg content is like: [pid=xxx,...] - uint32_t mpid = ParsePid(kmsgStr); - // If there are some other content wrapped in square brackets "[]", parse it as tag - // Otherwise, use default tag "kmsg" - size_t tagLen = 0; - std::string tagStr = ParseTag(kmsgStr); - if (!tagStr.empty()) { - tagLen = tagStr.size(); - if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, tagStr.c_str(), tagStr.size()) != 0) { - return {}; - } - } else { - constexpr auto defaultTag = "kmsg"sv; - tagLen = defaultTag.size(); - if (strncpy_s(mtag.data(), MAX_TAG_LEN - 1, defaultTag.data(), defaultTag.size()) != 0) { - return {}; - } - } + std::string tagStr = ""; + uint8_t mLevel = 0; + ParseHeader(kmsgStr, mLevel, tagStr); + size_t tagLen = tagStr.size(); // Now build HilogMsg and insert it into buffer auto len = kmsgStr.size() + 1; auto msgLen = sizeof(HilogMsg) + tagLen + len + 1; @@ -170,15 +115,12 @@ std::optional KmsgParser::ParseKmsg(const std::vector& km msg.len = msgLen; msg.tag_len = tagLen + 1; msg.type = LOG_KMSG; - msg.domain = 0xD002600; msg.level = KmsgLevelMap(mLevel); - time_point logtime = BootTime() + microseconds{timestamp}; - struct timespec logts = TimepointToTimespec(logtime); - msg.tv_sec = static_cast(logts.tv_sec); - msg.tv_nsec = static_cast(logts.tv_nsec); - msg.pid = mpid; - msg.tid = mpid; - if (strncpy_s(msg.tag, tagLen + 1, mtag.data(), tagLen) != 0) { + struct timespec ts = {0}; + (void)clock_gettime(CLOCK_REALTIME, &ts); + msg.tv_sec = static_cast(ts.tv_sec); + msg.tv_nsec = static_cast(ts.tv_nsec); + if (strncpy_s(msg.tag, tagLen + 1, tagStr.c_str(), tagLen) != 0) { return {}; } if (strncpy_s(CONTENT_PTR((&msg)), MAX_LOG_LEN, kmsgStr.c_str(), len) != 0) {