diff options
| author | 2018-07-20 17:17:04 -0700 | |
|---|---|---|
| committer | 2018-07-20 17:17:04 -0700 | |
| commit | 8afc21f175548287c1f678e28c999567299eec4e (patch) | |
| tree | 4e7425138bc85a064db386194ffce1caa8e6a384 /src | |
| parent | Merge pull request #745 from lioncash/package (diff) | |
| parent | logging/filter: Use std::string_view in ParseFilterString() (diff) | |
| download | yuzu-8afc21f175548287c1f678e28c999567299eec4e.tar.gz yuzu-8afc21f175548287c1f678e28c999567299eec4e.tar.xz yuzu-8afc21f175548287c1f678e28c999567299eec4e.zip | |
Merge pull request #743 from lioncash/view
logging: Use std::string_view where applicable
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/logging/backend.cpp | 25 | ||||
| -rw-r--r-- | src/common/logging/backend.h | 7 | ||||
| -rw-r--r-- | src/common/logging/filter.cpp | 75 | ||||
| -rw-r--r-- | src/common/logging/filter.h | 6 |
4 files changed, 56 insertions, 57 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index ed1e93cc2..59b999935 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp | |||
| @@ -3,19 +3,20 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <array> | 6 | #include <atomic> |
| 7 | #include <chrono> | 7 | #include <chrono> |
| 8 | #include <climits> | 8 | #include <climits> |
| 9 | #include <condition_variable> | 9 | #include <condition_variable> |
| 10 | #include <memory> | 10 | #include <memory> |
| 11 | #include <mutex> | ||
| 11 | #include <thread> | 12 | #include <thread> |
| 13 | #include <vector> | ||
| 12 | #ifdef _WIN32 | 14 | #ifdef _WIN32 |
| 13 | #include <share.h> // For _SH_DENYWR | 15 | #include <share.h> // For _SH_DENYWR |
| 14 | #else | 16 | #else |
| 15 | #define _SH_DENYWR 0 | 17 | #define _SH_DENYWR 0 |
| 16 | #endif | 18 | #endif |
| 17 | #include "common/assert.h" | 19 | #include "common/assert.h" |
| 18 | #include "common/common_funcs.h" // snprintf compatibility define | ||
| 19 | #include "common/logging/backend.h" | 20 | #include "common/logging/backend.h" |
| 20 | #include "common/logging/log.h" | 21 | #include "common/logging/log.h" |
| 21 | #include "common/logging/text_formatter.h" | 22 | #include "common/logging/text_formatter.h" |
| @@ -48,11 +49,11 @@ public: | |||
| 48 | backends.push_back(std::move(backend)); | 49 | backends.push_back(std::move(backend)); |
| 49 | } | 50 | } |
| 50 | 51 | ||
| 51 | void RemoveBackend(const std::string& backend_name) { | 52 | void RemoveBackend(std::string_view backend_name) { |
| 52 | std::lock_guard<std::mutex> lock(writing_mutex); | 53 | std::lock_guard<std::mutex> lock(writing_mutex); |
| 53 | auto it = std::remove_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { | 54 | const auto it = |
| 54 | return !strcmp(i->GetName(), backend_name.c_str()); | 55 | std::remove_if(backends.begin(), backends.end(), |
| 55 | }); | 56 | [&backend_name](const auto& i) { return backend_name == i->GetName(); }); |
| 56 | backends.erase(it, backends.end()); | 57 | backends.erase(it, backends.end()); |
| 57 | } | 58 | } |
| 58 | 59 | ||
| @@ -64,10 +65,10 @@ public: | |||
| 64 | filter = f; | 65 | filter = f; |
| 65 | } | 66 | } |
| 66 | 67 | ||
| 67 | Backend* GetBackend(const std::string& backend_name) { | 68 | Backend* GetBackend(std::string_view backend_name) { |
| 68 | auto it = std::find_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { | 69 | const auto it = |
| 69 | return !strcmp(i->GetName(), backend_name.c_str()); | 70 | std::find_if(backends.begin(), backends.end(), |
| 70 | }); | 71 | [&backend_name](const auto& i) { return backend_name == i->GetName(); }); |
| 71 | if (it == backends.end()) | 72 | if (it == backends.end()) |
| 72 | return nullptr; | 73 | return nullptr; |
| 73 | return it->get(); | 74 | return it->get(); |
| @@ -265,11 +266,11 @@ void AddBackend(std::unique_ptr<Backend> backend) { | |||
| 265 | Impl::Instance().AddBackend(std::move(backend)); | 266 | Impl::Instance().AddBackend(std::move(backend)); |
| 266 | } | 267 | } |
| 267 | 268 | ||
| 268 | void RemoveBackend(const std::string& backend_name) { | 269 | void RemoveBackend(std::string_view backend_name) { |
| 269 | Impl::Instance().RemoveBackend(backend_name); | 270 | Impl::Instance().RemoveBackend(backend_name); |
| 270 | } | 271 | } |
| 271 | 272 | ||
| 272 | Backend* GetBackend(const std::string& backend_name) { | 273 | Backend* GetBackend(std::string_view backend_name) { |
| 273 | return Impl::Instance().GetBackend(backend_name); | 274 | return Impl::Instance().GetBackend(backend_name); |
| 274 | } | 275 | } |
| 275 | 276 | ||
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index 57cdf6b2d..b3f4b9cef 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h | |||
| @@ -4,10 +4,9 @@ | |||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <chrono> | 6 | #include <chrono> |
| 7 | #include <cstdarg> | ||
| 8 | #include <memory> | 7 | #include <memory> |
| 9 | #include <string> | 8 | #include <string> |
| 10 | #include <utility> | 9 | #include <string_view> |
| 11 | #include "common/file_util.h" | 10 | #include "common/file_util.h" |
| 12 | #include "common/logging/filter.h" | 11 | #include "common/logging/filter.h" |
| 13 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| @@ -106,9 +105,9 @@ private: | |||
| 106 | 105 | ||
| 107 | void AddBackend(std::unique_ptr<Backend> backend); | 106 | void AddBackend(std::unique_ptr<Backend> backend); |
| 108 | 107 | ||
| 109 | void RemoveBackend(const std::string& backend_name); | 108 | void RemoveBackend(std::string_view backend_name); |
| 110 | 109 | ||
| 111 | Backend* GetBackend(const std::string& backend_name); | 110 | Backend* GetBackend(std::string_view backend_name); |
| 112 | 111 | ||
| 113 | /** | 112 | /** |
| 114 | * Returns the name of the passed log class as a C-string. Subclasses are separated by periods | 113 | * Returns the name of the passed log class as a C-string. Subclasses are separated by periods |
diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp index 6ed087beb..2dd331152 100644 --- a/src/common/logging/filter.cpp +++ b/src/common/logging/filter.cpp | |||
| @@ -8,39 +8,9 @@ | |||
| 8 | #include "common/string_util.h" | 8 | #include "common/string_util.h" |
| 9 | 9 | ||
| 10 | namespace Log { | 10 | namespace Log { |
| 11 | 11 | namespace { | |
| 12 | Filter::Filter(Level default_level) { | ||
| 13 | ResetAll(default_level); | ||
| 14 | } | ||
| 15 | |||
| 16 | void Filter::ResetAll(Level level) { | ||
| 17 | class_levels.fill(level); | ||
| 18 | } | ||
| 19 | |||
| 20 | void Filter::SetClassLevel(Class log_class, Level level) { | ||
| 21 | class_levels[static_cast<size_t>(log_class)] = level; | ||
| 22 | } | ||
| 23 | |||
| 24 | void Filter::ParseFilterString(const std::string& filter_str) { | ||
| 25 | auto clause_begin = filter_str.cbegin(); | ||
| 26 | while (clause_begin != filter_str.cend()) { | ||
| 27 | auto clause_end = std::find(clause_begin, filter_str.cend(), ' '); | ||
| 28 | |||
| 29 | // If clause isn't empty | ||
| 30 | if (clause_end != clause_begin) { | ||
| 31 | ParseFilterRule(clause_begin, clause_end); | ||
| 32 | } | ||
| 33 | |||
| 34 | if (clause_end != filter_str.cend()) { | ||
| 35 | // Skip over the whitespace | ||
| 36 | ++clause_end; | ||
| 37 | } | ||
| 38 | clause_begin = clause_end; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | template <typename It> | 12 | template <typename It> |
| 43 | static Level GetLevelByName(const It begin, const It end) { | 13 | Level GetLevelByName(const It begin, const It end) { |
| 44 | for (u8 i = 0; i < static_cast<u8>(Level::Count); ++i) { | 14 | for (u8 i = 0; i < static_cast<u8>(Level::Count); ++i) { |
| 45 | const char* level_name = GetLevelName(static_cast<Level>(i)); | 15 | const char* level_name = GetLevelName(static_cast<Level>(i)); |
| 46 | if (Common::ComparePartialString(begin, end, level_name)) { | 16 | if (Common::ComparePartialString(begin, end, level_name)) { |
| @@ -51,7 +21,7 @@ static Level GetLevelByName(const It begin, const It end) { | |||
| 51 | } | 21 | } |
| 52 | 22 | ||
| 53 | template <typename It> | 23 | template <typename It> |
| 54 | static Class GetClassByName(const It begin, const It end) { | 24 | Class GetClassByName(const It begin, const It end) { |
| 55 | for (ClassType i = 0; i < static_cast<ClassType>(Class::Count); ++i) { | 25 | for (ClassType i = 0; i < static_cast<ClassType>(Class::Count); ++i) { |
| 56 | const char* level_name = GetLogClassName(static_cast<Class>(i)); | 26 | const char* level_name = GetLogClassName(static_cast<Class>(i)); |
| 57 | if (Common::ComparePartialString(begin, end, level_name)) { | 27 | if (Common::ComparePartialString(begin, end, level_name)) { |
| @@ -61,8 +31,8 @@ static Class GetClassByName(const It begin, const It end) { | |||
| 61 | return Class::Count; | 31 | return Class::Count; |
| 62 | } | 32 | } |
| 63 | 33 | ||
| 64 | bool Filter::ParseFilterRule(const std::string::const_iterator begin, | 34 | template <typename Iterator> |
| 65 | const std::string::const_iterator end) { | 35 | bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { |
| 66 | auto level_separator = std::find(begin, end, ':'); | 36 | auto level_separator = std::find(begin, end, ':'); |
| 67 | if (level_separator == end) { | 37 | if (level_separator == end) { |
| 68 | LOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: {}", | 38 | LOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: {}", |
| @@ -77,7 +47,7 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, | |||
| 77 | } | 47 | } |
| 78 | 48 | ||
| 79 | if (Common::ComparePartialString(begin, level_separator, "*")) { | 49 | if (Common::ComparePartialString(begin, level_separator, "*")) { |
| 80 | ResetAll(level); | 50 | instance.ResetAll(level); |
| 81 | return true; | 51 | return true; |
| 82 | } | 52 | } |
| 83 | 53 | ||
| @@ -87,9 +57,40 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, | |||
| 87 | return false; | 57 | return false; |
| 88 | } | 58 | } |
| 89 | 59 | ||
| 90 | SetClassLevel(log_class, level); | 60 | instance.SetClassLevel(log_class, level); |
| 91 | return true; | 61 | return true; |
| 92 | } | 62 | } |
| 63 | } // Anonymous namespace | ||
| 64 | |||
| 65 | Filter::Filter(Level default_level) { | ||
| 66 | ResetAll(default_level); | ||
| 67 | } | ||
| 68 | |||
| 69 | void Filter::ResetAll(Level level) { | ||
| 70 | class_levels.fill(level); | ||
| 71 | } | ||
| 72 | |||
| 73 | void Filter::SetClassLevel(Class log_class, Level level) { | ||
| 74 | class_levels[static_cast<size_t>(log_class)] = level; | ||
| 75 | } | ||
| 76 | |||
| 77 | void Filter::ParseFilterString(std::string_view filter_view) { | ||
| 78 | auto clause_begin = filter_view.cbegin(); | ||
| 79 | while (clause_begin != filter_view.cend()) { | ||
| 80 | auto clause_end = std::find(clause_begin, filter_view.cend(), ' '); | ||
| 81 | |||
| 82 | // If clause isn't empty | ||
| 83 | if (clause_end != clause_begin) { | ||
| 84 | ParseFilterRule(*this, clause_begin, clause_end); | ||
| 85 | } | ||
| 86 | |||
| 87 | if (clause_end != filter_view.cend()) { | ||
| 88 | // Skip over the whitespace | ||
| 89 | ++clause_end; | ||
| 90 | } | ||
| 91 | clause_begin = clause_end; | ||
| 92 | } | ||
| 93 | } | ||
| 93 | 94 | ||
| 94 | bool Filter::CheckMessage(Class log_class, Level level) const { | 95 | bool Filter::CheckMessage(Class log_class, Level level) const { |
| 95 | return static_cast<u8>(level) >= static_cast<u8>(class_levels[static_cast<size_t>(log_class)]); | 96 | return static_cast<u8>(level) >= static_cast<u8>(class_levels[static_cast<size_t>(log_class)]); |
diff --git a/src/common/logging/filter.h b/src/common/logging/filter.h index 2a4f7c845..d5ffc5a58 100644 --- a/src/common/logging/filter.h +++ b/src/common/logging/filter.h | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <cstddef> | 8 | #include <cstddef> |
| 9 | #include <string> | 9 | #include <string_view> |
| 10 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 11 | 11 | ||
| 12 | namespace Log { | 12 | namespace Log { |
| @@ -40,9 +40,7 @@ public: | |||
| 40 | * - `Service:Info` -- Sets the level of Service to Info. | 40 | * - `Service:Info` -- Sets the level of Service to Info. |
| 41 | * - `Service.FS:Trace` -- Sets the level of the Service.FS class to Trace. | 41 | * - `Service.FS:Trace` -- Sets the level of the Service.FS class to Trace. |
| 42 | */ | 42 | */ |
| 43 | void ParseFilterString(const std::string& filter_str); | 43 | void ParseFilterString(std::string_view filter_view); |
| 44 | bool ParseFilterRule(const std::string::const_iterator start, | ||
| 45 | const std::string::const_iterator end); | ||
| 46 | 44 | ||
| 47 | /// Matches class/level combination against the filter, returning true if it passed. | 45 | /// Matches class/level combination against the filter, returning true if it passed. |
| 48 | bool CheckMessage(Class log_class, Level level) const; | 46 | bool CheckMessage(Class log_class, Level level) const; |