DDOSLogger.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2019, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. #import "DDOSLogger.h"
  16. #import <os/log.h>
  17. @interface DDOSLogger () {
  18. NSString *_subsystem;
  19. NSString *_category;
  20. }
  21. @property (copy, nonatomic, readonly) NSString *subsystem;
  22. @property (copy, nonatomic, readonly) NSString *category;
  23. @property (strong, nonatomic, readwrite) os_log_t logger;
  24. @end
  25. @implementation DDOSLogger
  26. @synthesize subsystem = _subsystem;
  27. @synthesize category = _category;
  28. #pragma mark - Initialization
  29. /**
  30. * Assertion
  31. * Swift: (String, String)?
  32. */
  33. - (instancetype)initWithSubsystem:(NSString *)subsystem category:(NSString *)category {
  34. NSAssert((subsystem == nil) == (category == nil), @"Either both subsystem and category or neither can be nil.");
  35. if (self = [super init]) {
  36. _subsystem = [subsystem copy];
  37. _category = [category copy];
  38. }
  39. return self;
  40. }
  41. static DDOSLogger *sharedInstance;
  42. - (instancetype)init {
  43. return [self initWithSubsystem:nil category:nil];
  44. }
  45. + (instancetype)sharedInstance {
  46. static dispatch_once_t DDOSLoggerOnceToken;
  47. dispatch_once(&DDOSLoggerOnceToken, ^{
  48. sharedInstance = [[[self class] alloc] init];
  49. });
  50. return sharedInstance;
  51. }
  52. #pragma mark - os_log
  53. - (os_log_t)getLogger {
  54. if (self.subsystem == nil || self.category == nil) {
  55. return OS_LOG_DEFAULT;
  56. }
  57. __auto_type subdomain = self.subsystem.UTF8String;
  58. __auto_type category = self.category.UTF8String;
  59. return os_log_create(subdomain, category);
  60. }
  61. - (os_log_t)logger {
  62. if (_logger == nil) {
  63. _logger = [self getLogger];
  64. }
  65. return _logger;
  66. }
  67. #pragma mark - DDLogger
  68. - (void)logMessage:(DDLogMessage *)logMessage {
  69. // Skip captured log messages
  70. if ([logMessage->_fileName isEqualToString:@"DDASLLogCapture"]) {
  71. return;
  72. }
  73. if(@available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *)) {
  74. NSString * message = _logFormatter ? [_logFormatter formatLogMessage:logMessage] : logMessage->_message;
  75. if (message != nil) {
  76. const char *msg = [message UTF8String];
  77. __auto_type logger = [self logger];
  78. switch (logMessage->_flag) {
  79. case DDLogFlagError :
  80. os_log_error(logger, "%{public}s", msg);
  81. break;
  82. case DDLogFlagWarning :
  83. case DDLogFlagInfo :
  84. os_log_info(logger, "%{public}s", msg);
  85. break;
  86. case DDLogFlagDebug :
  87. case DDLogFlagVerbose :
  88. default :
  89. os_log_debug(logger, "%{public}s", msg);
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. - (DDLoggerName)loggerName {
  96. return DDLoggerNameOS;
  97. }
  98. @end