BTLogger.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #import <Foundation/Foundation.h>
  2. #import "BTLogger_Internal.h"
  3. #define variadicLogLevel(level, format) \
  4. va_list args; \
  5. va_start(args, format); \
  6. [self logLevel:level format:format arguments:args]; \
  7. va_end(args);
  8. @implementation BTLogger
  9. + (instancetype)sharedLogger {
  10. static BTLogger *instance;
  11. static dispatch_once_t onceToken;
  12. dispatch_once(&onceToken, ^{
  13. instance = [self new];
  14. });
  15. return instance;
  16. }
  17. - (instancetype)init {
  18. self = [super init];
  19. if (self) {
  20. _level = BTLogLevelInfo;
  21. }
  22. return self;
  23. }
  24. - (void)log:(NSString *)format, ... {
  25. variadicLogLevel(BTLogLevelInfo, format)
  26. }
  27. - (void)critical:(NSString *)format, ... {
  28. variadicLogLevel(BTLogLevelCritical, format)
  29. }
  30. - (void)error:(NSString *)format, ... {
  31. variadicLogLevel(BTLogLevelError, format)
  32. }
  33. - (void)warning:(NSString *)format, ... {
  34. variadicLogLevel(BTLogLevelWarning, format)
  35. }
  36. - (void)info:(NSString *)format, ... {
  37. variadicLogLevel(BTLogLevelInfo, format)
  38. }
  39. - (void)debug:(NSString *)format, ... {
  40. variadicLogLevel(BTLogLevelDebug, format)
  41. }
  42. - (void)logLevel:(BTLogLevel)level format:(NSString *)format arguments:(va_list)arguments {
  43. if (level <= self.level) {
  44. NSString *message = [[NSString alloc] initWithFormat:format arguments:arguments];
  45. if (self.logBlock) {
  46. self.logBlock(level, message);
  47. } else {
  48. NSString *levelString = [[self class] levelString:level];
  49. NSLog(@"[BraintreeSDK] %@ %@", [levelString uppercaseString], message);
  50. }
  51. }
  52. }
  53. + (NSString *)levelString:(BTLogLevel)level {
  54. switch (level) {
  55. case BTLogLevelCritical:
  56. return @"Critical";
  57. case BTLogLevelError:
  58. return @"Error";
  59. case BTLogLevelWarning:
  60. return @"Warning";
  61. case BTLogLevelInfo:
  62. return @"Info";
  63. case BTLogLevelDebug:
  64. return @"Debug";
  65. default:
  66. return nil;
  67. }
  68. }
  69. @end