Init.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /*
  18. * This file contains function to help configure the logging library behavior
  19. * during program start-up.
  20. */
  21. #include <folly/Range.h>
  22. namespace folly {
  23. /**
  24. * Initialize the logging library.
  25. *
  26. * This function performs the following steps:
  27. * - Call folly::getBaseLoggingConfig() to get the base logging configuration
  28. * for your program.
  29. * - Parse the input configString parameter with parseLogConfig(), and update
  30. * the base configuration with the settings from this argument.
  31. * - Apply these combined settings to the main LoggerDB singleton using
  32. * LoggerDB::updateConfig()
  33. *
  34. * This function will throw an exception on error. Most errors are normally
  35. * due to invalid logging configuration strings: e.g., invalid log level names
  36. * or referencing undefined log handlers.
  37. *
  38. * If you are invoking this from your program's main() function it is often
  39. * more convenient to use initLoggingOrDie() to terminate your program
  40. * gracefully on error rather than having to handle exceptions yourself.
  41. */
  42. void initLogging(folly::StringPiece configString = "");
  43. /**
  44. * Initialize the logging library, and exit the program on error.
  45. *
  46. * This function behaves like initLogging(), but if an error occurs processing
  47. * the logging configuration it will print an error message to stderr and then
  48. * call exit(1) to terminate the program.
  49. */
  50. void initLoggingOrDie(folly::StringPiece configString = "");
  51. /**
  52. * folly::getBaseLoggingConfig() allows individual executables to easily
  53. * customize their default logging configuration.
  54. *
  55. * You can define this function in your executable and folly::initLogging()
  56. * will call it to get the base logging configuration. The settings returned
  57. * by getBaseLoggingConfig() will then be modified by updating them with the
  58. * configuration string parameter passed to initLogging().
  59. *
  60. * This allows the user-specified configuration passed to initLogging() to
  61. * update the base configuration. The user-specified configuration can apply
  62. * additional settings, and it may also override settings for categories and
  63. * handlers defined in the base configuration.
  64. *
  65. * See folly/logging/example/main.cpp for an example that defines
  66. * getBaseLoggingConfig().
  67. *
  68. * If this function returns a non-null pointer, it should point to a
  69. * null-terminated string with static storage duration.
  70. */
  71. const char* getBaseLoggingConfig();
  72. } // namespace folly
  73. /**
  74. * A helper macro to set the default logging configuration in a program.
  75. *
  76. * This defines the folly::getBaseLoggingConfig() function, and makes it return
  77. * the specified string.
  78. *
  79. * This macro should be used at the top-level namespace in a .cpp file in your
  80. * program.
  81. */
  82. #define FOLLY_INIT_LOGGING_CONFIG(config) \
  83. namespace folly { \
  84. const char* getBaseLoggingConfig() { \
  85. static constexpr StringPiece configSP((config)); \
  86. return configSP.data(); \
  87. } \
  88. } \
  89. static_assert(true, "require a semicolon after FOLLY_INIT_LOGGING_CONFIG()")