Use strtol instead of std::atoi in logging

strtol does not throw exceptions.

Change-Id: I6d6d3c32c2b61ed0307370bbfc3ce333adb5c504
Signed-off-by: Dennis Tsiang <dennis.tsiang@arm.com>
This commit is contained in:
Dennis Tsiang 2023-02-07 14:23:49 +00:00 committed by Rosen Zhelev
parent 2ad81d6b97
commit c33a17f103
2 changed files with 15 additions and 20 deletions

View file

@ -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))
{

View file

@ -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__)