DDOSLogger.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2016, 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. static DDOSLogger *sharedInstance;
  18. @implementation DDOSLogger
  19. + (instancetype)sharedInstance {
  20. static dispatch_once_t DDOSLoggerOnceToken;
  21. dispatch_once(&DDOSLoggerOnceToken, ^{
  22. sharedInstance = [[[self class] alloc] init];
  23. });
  24. return sharedInstance;
  25. }
  26. - (instancetype)init {
  27. if (sharedInstance != nil) {
  28. return nil;
  29. }
  30. if (self = [super init]) {
  31. return self;
  32. }
  33. return nil;
  34. }
  35. - (void)logMessage:(DDLogMessage *)logMessage {
  36. // Skip captured log messages
  37. if ([logMessage->_fileName isEqualToString:@"DDASLLogCapture"]) {
  38. return;
  39. }
  40. if(@available(iOS 10.0, macOS 10.12, tvOS 10.0, watchOS 3.0, *)) {
  41. NSString * message = _logFormatter ? [_logFormatter formatLogMessage:logMessage] : logMessage->_message;
  42. if (message) {
  43. const char *msg = [message UTF8String];
  44. switch (logMessage->_flag) {
  45. case DDLogFlagError :
  46. os_log_error(OS_LOG_DEFAULT, "%{public}s", msg);
  47. break;
  48. case DDLogFlagWarning :
  49. case DDLogFlagInfo :
  50. os_log_info(OS_LOG_DEFAULT, "%{public}s", msg);
  51. break;
  52. case DDLogFlagDebug :
  53. case DDLogFlagVerbose :
  54. default :
  55. os_log_debug(OS_LOG_DEFAULT, "%{public}s", msg);
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. - (NSString *)loggerName {
  62. return @"cocoa.lumberjack.osLogger";
  63. }
  64. @end