CustomLogFormatter.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include <folly/logging/LogFormatter.h>
  19. #include <string>
  20. namespace folly {
  21. /**
  22. * A LogFormatter implementation that produces messages in a format specified
  23. * using a config.
  24. *
  25. * The glog message format is:
  26. *
  27. * {L}{m:02d}{D:02d} {H:2d}:{M:02d}:{S:02d}.{USECS:06d} {THREAD:5d}
  28. * {FILE}:{LINE}]
  29. *
  30. * L: A 1-character code describing the log level (e.g., E, W, I, V)
  31. * m: month
  32. * D: day
  33. * H: hour, 24-hour format
  34. * M: minute
  35. * S: second
  36. * USECS: microseconds
  37. * THREAD: Thread ID
  38. * FILE: Filename (just the last component)
  39. * FUN: The function that logged the message
  40. * LINE: Line number
  41. *
  42. * TODO: enable support for the following 2:
  43. * - THREADNAME: the thread name.
  44. * - THREADCTX: thread-local log context data, if it has been set. (This is
  45. * a Facebook-specific modification)
  46. */
  47. class CustomLogFormatter : public LogFormatter {
  48. public:
  49. explicit CustomLogFormatter(StringPiece format, bool colored);
  50. std::string formatMessage(
  51. const LogMessage& message,
  52. const LogCategory* handlerCategory) override;
  53. private:
  54. void parseFormatString(StringPiece format);
  55. std::string logFormat_;
  56. std::string singleLineLogFormat_;
  57. std::size_t staticEstimatedWidth_{0};
  58. std::size_t fileNameCount_{0};
  59. std::size_t functionNameCount_{0};
  60. const bool colored_;
  61. };
  62. } // namespace folly