From f4db2aad573d55a7a05782bfdfbb15b529e8ff11 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Fri, 14 Jun 2019 14:54:02 -0700 Subject: [PATCH] init: use Errorf() now that we have it Init had some pretty horrid Error() << StringPrintf(...) calls that are all much better replaced by Errorf(...) now. Test: build, check that keyword_map errors look correct Change-Id: I572588c7541b928c72ae1bf140b814acdef1cd60 --- init/keychords_test.cpp | 1 - init/keyword_map.h | 23 ++++++++--------------- init/reboot.cpp | 2 -- init/result.h | 2 ++ init/service.cpp | 9 ++++----- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp index a3baeb116..33373d473 100644 --- a/init/keychords_test.cpp +++ b/init/keychords_test.cpp @@ -29,7 +29,6 @@ #include #include -#include #include #include diff --git a/init/keyword_map.h b/init/keyword_map.h index c95fc7318..7837bb363 100644 --- a/init/keyword_map.h +++ b/init/keyword_map.h @@ -14,14 +14,11 @@ * limitations under the License. */ -#ifndef _INIT_KEYWORD_MAP_H_ -#define _INIT_KEYWORD_MAP_H_ +#pragma once #include #include -#include - #include "result.h" namespace android { @@ -37,8 +34,6 @@ class KeywordMap { } const Result FindFunction(const std::vector& args) const { - using android::base::StringPrintf; - if (args.empty()) return Error() << "Keyword needed, but not provided"; auto& keyword = args[0]; @@ -46,7 +41,7 @@ class KeywordMap { auto function_info_it = map().find(keyword); if (function_info_it == map().end()) { - return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str()); + return Errorf("Invalid keyword '{}'", keyword); } auto function_info = function_info_it->second; @@ -54,17 +49,17 @@ class KeywordMap { auto min_args = std::get<0>(function_info); auto max_args = std::get<1>(function_info); if (min_args == max_args && num_args != min_args) { - return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args, - (min_args > 1 || min_args == 0) ? "s" : ""); + return Errorf("{} requires {} argument{}", keyword, min_args, + (min_args > 1 || min_args == 0) ? "s" : ""); } if (num_args < min_args || num_args > max_args) { if (max_args == std::numeric_limits::max()) { - return Error() << StringPrintf("%s requires at least %zu argument%s", - keyword.c_str(), min_args, min_args > 1 ? "s" : ""); + return Errorf("{} requires at least {} argument{}", keyword, min_args, + min_args > 1 ? "s" : ""); } else { - return Error() << StringPrintf("%s requires between %zu and %zu arguments", - keyword.c_str(), min_args, max_args); + return Errorf("{} requires between {} and {} arguments", keyword, min_args, + max_args); } } @@ -79,5 +74,3 @@ class KeywordMap { } // namespace init } // namespace android - -#endif diff --git a/init/reboot.cpp b/init/reboot.cpp index fbc03c276..eaba3ccff 100644 --- a/init/reboot.cpp +++ b/init/reboot.cpp @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -62,7 +61,6 @@ using android::base::GetBoolProperty; using android::base::Split; -using android::base::StringPrintf; using android::base::Timer; using android::base::unique_fd; using android::base::WriteStringToFile; diff --git a/init/result.h b/init/result.h index 8c1f91e03..b70dd1b44 100644 --- a/init/result.h +++ b/init/result.h @@ -22,6 +22,8 @@ #include using android::base::ErrnoError; +using android::base::ErrnoErrorf; using android::base::Error; +using android::base::Errorf; using android::base::Result; using android::base::ResultError; diff --git a/init/service.cpp b/init/service.cpp index b6a7c33f7..4fe374c45 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -332,12 +332,11 @@ Result Service::ParseCapabilities(std::vector&& args) { const std::string& arg = args[i]; int res = LookupCap(arg); if (res < 0) { - return Error() << StringPrintf("invalid capability '%s'", arg.c_str()); + return Errorf("invalid capability '{}'", arg); } unsigned int cap = static_cast(res); // |res| is >= 0. if (cap > last_valid_cap) { - return Error() << StringPrintf("capability '%s' not supported by the kernel", - arg.c_str()); + return Errorf("capability '{}' not supported by the kernel", arg); } (*capabilities_)[cap] = true; } @@ -402,8 +401,8 @@ Result Service::ParsePriority(std::vector&& args) { if (!ParseInt(args[1], &proc_attr_.priority, static_cast(ANDROID_PRIORITY_HIGHEST), // highest is negative static_cast(ANDROID_PRIORITY_LOWEST))) { - return Error() << StringPrintf("process priority value must be range %d - %d", - ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST); + return Errorf("process priority value must be range {} - {}", ANDROID_PRIORITY_HIGHEST, + ANDROID_PRIORITY_LOWEST); } return {}; }