From 9ff23da2550dd159d81933c0236529ececc526b5 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 2 Aug 2015 12:55:31 -0400 Subject: Handle invalid `Log::Level::Count` Add a case of `Log::Level::Count` to all switch statements that dispatch on `Log::Level`. The case simply asserts `false` and notes the invalid log level. --- src/common/logging/backend.cpp | 5 ++++- src/common/logging/text_formatter.cpp | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index d85e5837..68580e1c 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -6,6 +6,7 @@ #include #include +#include "common/assert.h" #include "common/common_funcs.h" // snprintf compatibility define #include "common/logging/backend.h" #include "common/logging/filter.h" @@ -78,8 +79,10 @@ const char* GetLevelName(Level log_level) { LVL(Warning); LVL(Error); LVL(Critical); + case Level::Count: + ASSERT_MSG(false, "invalid log level"); + return "Unknown"; } - return "Unknown"; #undef LVL } diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index 94f3dfc1..e3bb148b 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -14,6 +14,7 @@ #include "common/logging/log.h" #include "common/logging/text_formatter.h" +#include "common/assert.h" #include "common/common_funcs.h" #include "common/string_util.h" @@ -82,6 +83,8 @@ void PrintColoredMessage(const Entry& entry) { color = FOREGROUND_RED | FOREGROUND_INTENSITY; break; case Level::Critical: // Bright magenta color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; + case Level::Count: + ASSERT_MSG(false, "invalid log level"); break; } SetConsoleTextAttribute(console_handle, color); @@ -101,6 +104,8 @@ void PrintColoredMessage(const Entry& entry) { color = ESC "[1;31m"; break; case Level::Critical: // Bright magenta color = ESC "[1;35m"; break; + case Level::Count: + ASSERT_MSG(false, "invalid log level"); break; } fputs(color, stderr); -- cgit v1.2.3 From 0298b7bedd17f88430486dd502fb7923b1d10f26 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 2 Aug 2015 18:30:24 -0400 Subject: Use UNREACHABLE macro for impossible cases in previous commit Use the UNREACHABLE macro instead of `ASSERT(false, ...);`. --- src/common/logging/backend.cpp | 3 +-- src/common/logging/text_formatter.cpp | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 68580e1c..0a081e7d 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -80,8 +80,7 @@ const char* GetLevelName(Level log_level) { LVL(Error); LVL(Critical); case Level::Count: - ASSERT_MSG(false, "invalid log level"); - return "Unknown"; + UNREACHABLE(); } #undef LVL } diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index e3bb148b..de195b0f 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -84,7 +84,7 @@ void PrintColoredMessage(const Entry& entry) { case Level::Critical: // Bright magenta color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; case Level::Count: - ASSERT_MSG(false, "invalid log level"); break; + UNREACHABLE(); } SetConsoleTextAttribute(console_handle, color); @@ -105,7 +105,7 @@ void PrintColoredMessage(const Entry& entry) { case Level::Critical: // Bright magenta color = ESC "[1;35m"; break; case Level::Count: - ASSERT_MSG(false, "invalid log level"); break; + UNREACHABLE(); } fputs(color, stderr); -- cgit v1.2.3