From c33a17f103c4e3b15a68bd7164495d98fc894960 Mon Sep 17 00:00:00 2001 From: Dennis Tsiang Date: Tue, 7 Feb 2023 14:23:49 +0000 Subject: [PATCH] Use strtol instead of std::atoi in logging strtol does not throw exceptions. Change-Id: I6d6d3c32c2b61ed0307370bbfc3ce333adb5c504 Signed-off-by: Dennis Tsiang --- util/log.cpp | 23 +++++++---------------- util/log.hpp | 12 ++++++++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/util/log.cpp b/util/log.cpp index 482d2c7..eac9e35 100644 --- a/util/log.cpp +++ b/util/log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Arm Limited. + * Copyright (c) 2021-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -38,31 +38,22 @@ namespace util /** * @brief check if a log level is enabled, and print it */ -static int check_and_print_log_level(int level) +static bool check_and_print_log_level(long level) { struct log_state { - int level = WSI_DEFAULT_LOG_LEVEL; + long level = WSI_DEFAULT_LOG_LEVEL; log_state() { - char *env = std::getenv("VULKAN_WSI_DEBUG_LEVEL"); - if (env != nullptr) + if (const char *env = std::getenv("VULKAN_WSI_DEBUG_LEVEL")) { - std::string env_dup = std::string(env); - try - { - level = std::stoi(env_dup); - } - catch (const std::exception &e) - { - std::fprintf(stderr, "Error: %s\n", e.what()); - } + level = strtol(env, nullptr, 0); } } }; static log_state state; - int result = level <= state.level; + bool result = level <= state.level; if (result) { switch (level) @@ -87,7 +78,7 @@ static int check_and_print_log_level(int level) return result; } -void wsi_log_message(int level, const char *file, int line, const char *format, ...) +void wsi_log_message(long level, const char *file, int line, const char *format, ...) { if (check_and_print_log_level(level)) { diff --git a/util/log.hpp b/util/log.hpp index 2bd71de..2bc7f2b 100644 --- a/util/log.hpp +++ b/util/log.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -45,7 +45,7 @@ namespace util * @param[in] format A C-style formatting string. */ -void wsi_log_message(int level, const char *file, int line, const char *format, ...) +void wsi_log_message(long level, const char *file, int line, const char *format, ...) #ifdef __GNUC__ __attribute__((format(printf, 4, 5))) #endif @@ -57,8 +57,12 @@ static constexpr bool wsi_log_enable = false; static constexpr bool wsi_log_enable = true; #endif -#define WSI_LOG(level, ...) \ - do { if (::util::wsi_log_enable) ::util::wsi_log_message(level, __FILE__, __LINE__, __VA_ARGS__); } while (0) +#define WSI_LOG(level, ...) \ + do \ + { \ + if (::util::wsi_log_enable) \ + ::util::wsi_log_message(level, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) #define WSI_LOG_ERROR(...) WSI_LOG(1, __VA_ARGS__) #define WSI_LOG_WARNING(...) WSI_LOG(2, __VA_ARGS__)