proxygen
folly::LogName Class Reference

#include <LogName.h>

Classes

struct  Equals
 
struct  Hash
 

Static Public Member Functions

static std::string canonicalize (folly::StringPiece name)
 
static size_t hash (folly::StringPiece name)
 
static int cmp (folly::StringPiece nameA, folly::StringPiece nameB)
 
static folly::StringPiece getParent (folly::StringPiece name)
 

Detailed Description

The LogName class contains utility functions for processing log category names. It primarily handles canonicalization of names.

For instance, "foo.bar", "foo/bar", "foo..bar", and ".foo.bar..." all refer to the same log category.

Definition at line 29 of file LogName.h.

Member Function Documentation

std::string folly::LogName::canonicalize ( folly::StringPiece  name)
static

Return a canonicalized version of the log name.

'/' and '\' characters are converted to '.', then leading and trailing '.' characters are removed, and all sequences of consecutive '.' characters are replaced with a single '.'

Definition at line 26 of file LogName.cpp.

References folly::test::end(), folly::Range< Iter >::size(), and string.

Referenced by folly::LogCategory::LogCategory(), folly::parseLogConfigDynamic(), TEST(), and TEST_F().

26  {
27  std::string cname;
28  cname.reserve(input.size());
29 
30  // Ignore trailing category separator characters
31  size_t end = input.size();
32  while (end > 0 && isSeparator(input[end - 1])) {
33  --end;
34  }
35 
36  bool ignoreSeparator = true;
37  for (size_t idx = 0; idx < end; ++idx) {
38  if (isSeparator(input[idx])) {
39  if (ignoreSeparator) {
40  continue;
41  }
42  cname.push_back('.');
43  ignoreSeparator = true;
44  } else {
45  cname.push_back(input[idx]);
46  ignoreSeparator = false;
47  }
48  }
49  return cname;
50 }
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
const char * string
Definition: Conv.cpp:212
int folly::LogName::cmp ( folly::StringPiece  nameA,
folly::StringPiece  nameB 
)
static

Compare two log names.

The log name does not need to be pre-canonicalized. Returns 0 if and only if the two names refer to the same log category. Otherwise, returns -1 if the canonical version of nameA is less than the canonical version of nameB.

Definition at line 81 of file LogName.cpp.

References folly::Range< Iter >::empty(), folly::Range< Iter >::front(), s, and folly::Range< Iter >::uncheckedAdvance().

Referenced by folly::LogName::Equals::operator()(), and TEST().

81  {
82  // Ignore trailing separators
83  auto stripTrailingSeparators = [](StringPiece& s) {
84  while (!s.empty() && isSeparator(s.back())) {
85  s.uncheckedSubtract(1);
86  }
87  };
88  stripTrailingSeparators(a);
89  stripTrailingSeparators(b);
90 
91  // Advance ptr until it no longer points to a category separator.
92  // This is used to skip over consecutive sequences of separator characters.
93  auto skipOverSeparators = [](StringPiece& s) {
94  while (!s.empty() && isSeparator(s.front())) {
95  s.uncheckedAdvance(1);
96  }
97  };
98 
99  bool ignoreSeparator = true;
100  while (true) {
101  if (ignoreSeparator) {
102  skipOverSeparators(a);
103  skipOverSeparators(b);
104  }
105  if (a.empty()) {
106  return b.empty() ? 0 : -1;
107  } else if (b.empty()) {
108  return 1;
109  }
110  if (isSeparator(a.front())) {
111  if (!isSeparator(b.front())) {
112  return '.' - b.front();
113  }
114  ignoreSeparator = true;
115  } else {
116  if (a.front() != b.front()) {
117  return a.front() - b.front();
118  }
119  ignoreSeparator = false;
120  }
121  a.uncheckedAdvance(1);
122  b.uncheckedAdvance(1);
123  }
124 }
char b
char a
static set< string > s
Range< const char * > StringPiece
StringPiece folly::LogName::getParent ( folly::StringPiece  name)
static

Get the name of the parent log category.

Returns a StringPiece pointing into the input data. As a result, the parent log name may not be canonical if the input log name is not already canonical.

If the input log name refers to the root log category, an empty StringPiece will be returned.

Definition at line 126 of file LogName.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::empty(), name, and folly::Range< Iter >::size().

Referenced by folly::LoggerDB::getOrCreateCategoryLocked(), and TEST().

126  {
127  if (name.empty()) {
128  return name;
129  }
130 
131  ssize_t idx = name.size();
132 
133  // Skip over any trailing separator characters
134  while (idx > 0 && isSeparator(name[idx - 1])) {
135  --idx;
136  }
137 
138  // Now walk backwards to the next separator character
139  while (idx > 0 && !isSeparator(name[idx - 1])) {
140  --idx;
141  }
142 
143  // And again skip over any separator characters, in case there are multiple
144  // repeated characters.
145  while (idx > 0 && isSeparator(name[idx - 1])) {
146  --idx;
147  }
148 
149  return StringPiece(name.begin(), idx);
150 }
constexpr size_type size() const
Definition: Range.h:431
const char * name
Definition: http_parser.c:437
constexpr bool empty() const
Definition: Range.h:443
constexpr Iter begin() const
Definition: Range.h:452
Range< const char * > StringPiece
size_t folly::LogName::hash ( folly::StringPiece  name)
static

Hash a log name.

The log name does not need to be pre-canonicalized. The hash for equivalent log names will always be equal.

Definition at line 52 of file LogName.cpp.

References folly::test::end(), folly::Range< Iter >::size(), uint32_t, uint8_t, and folly::value().

Referenced by folly::LogName::Hash::operator()(), and TEST().

52  {
53  // Code based on StringPiece::hash(), but which ignores leading and trailing
54  // category separator characters, as well as multiple consecutive separator
55  // characters, so equivalent names result in the same hash.
56  uint32_t hash = 5381;
57 
58  size_t end = name.size();
59  while (end > 0 && isSeparator(name[end - 1])) {
60  --end;
61  }
62 
63  bool ignoreSeparator = true;
64  for (size_t idx = 0; idx < end; ++idx) {
65  uint8_t value;
66  if (isSeparator(name[idx])) {
67  if (ignoreSeparator) {
68  continue;
69  }
70  value = '.';
71  ignoreSeparator = true;
72  } else {
73  value = static_cast<uint8_t>(name[idx]);
74  ignoreSeparator = false;
75  }
76  hash = ((hash << 5) + hash) + value;
77  }
78  return hash;
79 }
constexpr size_type size() const
Definition: Range.h:431
static size_t hash(folly::StringPiece name)
Definition: LogName.cpp:52
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)

The documentation for this class was generated from the following files: