printf.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * C-style printf-like macros for the logging library.
  19. *
  20. * These are defined in their own separate header file to discourage their use
  21. * in new code. These macros make it somewhat easier to convert existing code
  22. * using printf()-like statements to the logging library. However, new code
  23. * should generally prefer to use one of the other macro forms instead
  24. * (simple argument concatenation, folly::format based, or iostream-like).
  25. *
  26. * These use a "C" suffix to the macro name since these use C-style format
  27. * syntax. (The "F" suffix is used for folly:format()-style.)
  28. */
  29. #include <folly/logging/Logger.h>
  30. #include <folly/logging/xlog.h>
  31. namespace folly {
  32. std::string loggingFormatPrintf(
  33. FOLLY_PRINTF_FORMAT const char* format,
  34. ...) noexcept FOLLY_PRINTF_FORMAT_ATTR(1, 2);
  35. } // namespace folly
  36. /**
  37. * Log a message to the specified logger using a printf-style format string.
  38. */
  39. #define FB_LOGC(logger, level, fmt, ...) \
  40. FB_LOG(logger, level, ::folly::loggingFormatPrintf(fmt, ##__VA_ARGS__))
  41. /**
  42. * Log a message to the file's default log category using a printf-style format
  43. * string.
  44. */
  45. #define XLOGC(level, fmt, ...) \
  46. XLOG_IMPL( \
  47. ::folly::LogLevel::level, \
  48. ::folly::LogStreamProcessor::APPEND, \
  49. ::folly::loggingFormatPrintf(fmt, ##__VA_ARGS__))
  50. /**
  51. * Log a message using a printf-style format string if and only if the
  52. * specified condition predicate evaluates to true. Note that the condition
  53. * is *only* evaluated if the log-level check passes.
  54. */
  55. #define XLOGC_IF(level, cond, fmt, ...) \
  56. XLOG_IF_IMPL( \
  57. ::folly::LogLevel::level, \
  58. cond, \
  59. ::folly::LogStreamProcessor::APPEND, \
  60. ::folly::loggingFormatPrintf(fmt, ##__VA_ARGS__))