diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp index 2f9e7a788..da1e9d82f 100644 --- a/bootstat/bootstat.cpp +++ b/bootstat/bootstat.cpp @@ -353,10 +353,18 @@ bool addKernelPanicSubReason(const std::string& console, std::string& ret) { char tounderline(char c) { return ::isblank(c) ? '_' : c; } + char toprintable(char c) { return ::isprint(c) ? c : '?'; } +// Cleanup boot_reason regarding acceptable character set +void transformReason(std::string& reason) { + std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); + std::transform(reason.begin(), reason.end(), reason.begin(), tounderline); + std::transform(reason.begin(), reason.end(), reason.begin(), toprintable); +} + const char system_reboot_reason_property[] = "sys.boot.reason"; const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY; const char bootloader_reboot_reason_property[] = "ro.boot.bootreason"; @@ -370,10 +378,7 @@ std::string BootReasonStrToReason(const std::string& boot_reason) { // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate if (reason == ret) ret = ""; - // Cleanup boot_reason regarding acceptable character set - std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); - std::transform(reason.begin(), reason.end(), reason.begin(), tounderline); - std::transform(reason.begin(), reason.end(), reason.begin(), toprintable); + transformReason(reason); // Is the current system boot reason sys.boot.reason valid? if (!isKnownRebootReason(ret)) ret = ""; @@ -462,13 +467,13 @@ std::string BootReasonStrToReason(const std::string& boot_reason) { pos += strlen(cmd); std::string subReason(content.substr(pos, max_reason_length)); for (pos = 0; pos < subReason.length(); ++pos) { - char c = tounderline(subReason[pos]); + char c = subReason[pos]; if (!::isprint(c) || (c == '\'')) { subReason.erase(pos); break; } - subReason[pos] = ::tolower(c); } + transformReason(subReason); if (subReason != "") { // Will not land "reboot" as that is too blunt. if (isKernelRebootReason(subReason)) { ret = "reboot," + subReason; // User space can't talk kernel reasons. @@ -565,10 +570,7 @@ std::string BootReasonStrToReason(const std::string& boot_reason) { // Content buffer no longer will have console data. Beware if more // checks added below, that depend on parsing console content. content = GetProperty(last_reboot_reason_property); - // Cleanup last_boot_reason regarding acceptable character set - std::transform(content.begin(), content.end(), content.begin(), ::tolower); - std::transform(content.begin(), content.end(), content.begin(), tounderline); - std::transform(content.begin(), content.end(), content.begin(), toprintable); + transformReason(content); // Anything in last is better than 'super-blunt' reboot or shutdown. if ((ret == "") || (ret == "reboot") || (ret == "shutdown") || !isBluntRebootReason(content)) {