summaryrefslogtreecommitdiff
path: root/src/common/logging
diff options
context:
space:
mode:
authorGravatar Lioncash2018-07-20 15:42:42 -0400
committerGravatar Lioncash2018-07-20 15:58:46 -0400
commitf63ccbd93640bfd1c8bb8aa68f1ed1b0e0a91e58 (patch)
treee54a887d80b0e00520930e32bcea11a9bf420d43 /src/common/logging
parentlogging/backend: Add missing standard includes (diff)
downloadyuzu-f63ccbd93640bfd1c8bb8aa68f1ed1b0e0a91e58.tar.gz
yuzu-f63ccbd93640bfd1c8bb8aa68f1ed1b0e0a91e58.tar.xz
yuzu-f63ccbd93640bfd1c8bb8aa68f1ed1b0e0a91e58.zip
logging/filter: Use std::string_view in ParseFilterString()
Allows avoiding constructing std::string instances, since this only reads an arbitrary sequence of characters. We can also make ParseFilterRule() internal, since it doesn't depend on any private instance state of Filter
Diffstat (limited to 'src/common/logging')
-rw-r--r--src/common/logging/filter.cpp75
-rw-r--r--src/common/logging/filter.h6
2 files changed, 40 insertions, 41 deletions
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
10namespace Log { 10namespace Log {
11 11namespace {
12Filter::Filter(Level default_level) {
13 ResetAll(default_level);
14}
15
16void Filter::ResetAll(Level level) {
17 class_levels.fill(level);
18}
19
20void Filter::SetClassLevel(Class log_class, Level level) {
21 class_levels[static_cast<size_t>(log_class)] = level;
22}
23
24void 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
42template <typename It> 12template <typename It>
43static Level GetLevelByName(const It begin, const It end) { 13Level 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
53template <typename It> 23template <typename It>
54static Class GetClassByName(const It begin, const It end) { 24Class 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
64bool Filter::ParseFilterRule(const std::string::const_iterator begin, 34template <typename Iterator>
65 const std::string::const_iterator end) { 35bool 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
65Filter::Filter(Level default_level) {
66 ResetAll(default_level);
67}
68
69void Filter::ResetAll(Level level) {
70 class_levels.fill(level);
71}
72
73void Filter::SetClassLevel(Class log_class, Level level) {
74 class_levels[static_cast<size_t>(log_class)] = level;
75}
76
77void 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
94bool Filter::CheckMessage(Class log_class, Level level) const { 95bool 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
12namespace Log { 12namespace 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;