LogName.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/Range.h>
  18. namespace folly {
  19. /**
  20. * The LogName class contains utility functions for processing log category
  21. * names. It primarily handles canonicalization of names.
  22. *
  23. * For instance, "foo.bar", "foo/bar", "foo..bar", and ".foo.bar..." all refer
  24. * to the same log category.
  25. */
  26. class LogName {
  27. public:
  28. /**
  29. * Return a canonicalized version of the log name.
  30. *
  31. * '/' and '\\' characters are converted to '.', then leading and trailing
  32. * '.' characters are removed, and all sequences of consecutive '.'
  33. * characters are replaced with a single '.'
  34. */
  35. static std::string canonicalize(folly::StringPiece name);
  36. /**
  37. * Hash a log name.
  38. *
  39. * The log name does not need to be pre-canonicalized.
  40. * The hash for equivalent log names will always be equal.
  41. */
  42. static size_t hash(folly::StringPiece name);
  43. /**
  44. * Compare two log names.
  45. *
  46. * The log name does not need to be pre-canonicalized.
  47. * Returns 0 if and only if the two names refer to the same log category.
  48. * Otherwise, returns -1 if the canonical version of nameA is less than the
  49. * canonical version of nameB.
  50. */
  51. static int cmp(folly::StringPiece nameA, folly::StringPiece nameB);
  52. /**
  53. * Get the name of the parent log category.
  54. *
  55. * Returns a StringPiece pointing into the input data.
  56. * As a result, the parent log name may not be canonical if the input log
  57. * name is not already canonical.
  58. *
  59. * If the input log name refers to the root log category, an empty
  60. * StringPiece will be returned.
  61. */
  62. static folly::StringPiece getParent(folly::StringPiece name);
  63. /**
  64. * Hash functor that can be used with standard library containers.
  65. */
  66. struct Hash {
  67. size_t operator()(folly::StringPiece key) const {
  68. return LogName::hash(key);
  69. }
  70. };
  71. /**
  72. * Equality functor that can be used with standard library containers.
  73. */
  74. struct Equals {
  75. bool operator()(folly::StringPiece a, folly::StringPiece b) const {
  76. return LogName::cmp(a, b) == 0;
  77. }
  78. };
  79. };
  80. } // namespace folly