RCTLog.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <Foundation/Foundation.h>
  8. #import <React/RCTAssert.h>
  9. #import <React/RCTDefines.h>
  10. #import <React/RCTUtils.h>
  11. #ifndef RCTLOG_ENABLED
  12. #define RCTLOG_ENABLED 1
  13. #endif
  14. /**
  15. * Thresholds for logs to display a redbox. You can override these values when debugging
  16. * in order to tweak the default logging behavior.
  17. */
  18. #ifndef RCTLOG_REDBOX_LEVEL
  19. #define RCTLOG_REDBOX_LEVEL RCTLogLevelError
  20. #endif
  21. /**
  22. * Logging macros. Use these to log information, warnings and errors in your
  23. * own code.
  24. */
  25. #define RCTLog(...) _RCTLog(RCTLogLevelInfo, __VA_ARGS__)
  26. #define RCTLogTrace(...) _RCTLog(RCTLogLevelTrace, __VA_ARGS__)
  27. #define RCTLogInfo(...) _RCTLog(RCTLogLevelInfo, __VA_ARGS__)
  28. #define RCTLogAdvice(string, ...) RCTLogWarn([@"(ADVICE) " stringByAppendingString:(NSString *)string], __VA_ARGS__)
  29. #define RCTLogWarn(...) _RCTLog(RCTLogLevelWarning, __VA_ARGS__)
  30. #define RCTLogError(...) _RCTLog(RCTLogLevelError, __VA_ARGS__)
  31. /**
  32. * An enum representing the severity of the log message.
  33. */
  34. typedef NS_ENUM(NSInteger, RCTLogLevel) {
  35. RCTLogLevelTrace = 0,
  36. RCTLogLevelInfo = 1,
  37. RCTLogLevelWarning = 2,
  38. RCTLogLevelError = 3,
  39. RCTLogLevelFatal = 4
  40. };
  41. /**
  42. * An enum representing the source of a log message.
  43. */
  44. typedef NS_ENUM(NSInteger, RCTLogSource) { RCTLogSourceNative = 1, RCTLogSourceJavaScript = 2 };
  45. /**
  46. * A block signature to be used for custom logging functions. In most cases you
  47. * will want to pass these arguments to the RCTFormatLog function in order to
  48. * generate a string.
  49. */
  50. typedef void (^RCTLogFunction)(
  51. RCTLogLevel level,
  52. RCTLogSource source,
  53. NSString *fileName,
  54. NSNumber *lineNumber,
  55. NSString *message);
  56. /**
  57. * A method to generate a string from a collection of log data. To omit any
  58. * particular data from the log, just pass nil or zero for the argument.
  59. */
  60. RCT_EXTERN NSString *
  61. RCTFormatLog(NSDate *timestamp, RCTLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message);
  62. /**
  63. * A method to generate a string RCTLogLevel
  64. */
  65. RCT_EXTERN NSString *RCTFormatLogLevel(RCTLogLevel);
  66. /**
  67. * A method to generate a string from a RCTLogSource
  68. */
  69. RCT_EXTERN NSString *RCTFormatLogSource(RCTLogSource);
  70. /**
  71. * The default logging function used by RCTLogXX.
  72. */
  73. extern RCTLogFunction RCTDefaultLogFunction;
  74. /**
  75. * These methods get and set the global logging threshold. This is the level
  76. * below which logs will be ignored. Default is RCTLogLevelInfo for debug and
  77. * RCTLogLevelError for production.
  78. */
  79. RCT_EXTERN void RCTSetLogThreshold(RCTLogLevel threshold);
  80. RCT_EXTERN RCTLogLevel RCTGetLogThreshold(void);
  81. /**
  82. * These methods get and set the global logging function called by the RCTLogXX
  83. * macros. You can use these to replace the standard behavior with custom log
  84. * functionality.
  85. */
  86. RCT_EXTERN void RCTSetLogFunction(RCTLogFunction logFunction);
  87. RCT_EXTERN RCTLogFunction RCTGetLogFunction(void);
  88. /**
  89. * This appends additional code to the existing log function, without replacing
  90. * the existing functionality. Useful if you just want to forward logs to an
  91. * extra service without changing the default behavior.
  92. */
  93. RCT_EXTERN void RCTAddLogFunction(RCTLogFunction logFunction);
  94. /**
  95. * This method temporarily overrides the log function while performing the
  96. * specified block. This is useful for testing purposes (to detect if a given
  97. * function logs something) or to suppress or override logging temporarily.
  98. */
  99. RCT_EXTERN void RCTPerformBlockWithLogFunction(void (^block)(void), RCTLogFunction logFunction);
  100. /**
  101. * This method adds a conditional prefix to any messages logged within the scope
  102. * of the passed block. This is useful for adding additional context to log
  103. * messages. The block will be performed synchronously on the current thread.
  104. */
  105. RCT_EXTERN void RCTPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix);
  106. /**
  107. * Private logging function - ignore this.
  108. */
  109. #if RCTLOG_ENABLED
  110. #define _RCTLog(lvl, ...) _RCTLogNativeInternal(lvl, __FILE__, __LINE__, __VA_ARGS__)
  111. #else
  112. #define _RCTLog(lvl, ...) \
  113. do { \
  114. } while (0)
  115. #endif
  116. RCT_EXTERN void _RCTLogNativeInternal(RCTLogLevel, const char *, int, NSString *, ...) NS_FORMAT_FUNCTION(4, 5);
  117. RCT_EXTERN void _RCTLogJavaScriptInternal(RCTLogLevel, NSString *);