You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DDASLLogger.m 3.7 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2015, 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 "DDASLLogger.h"
  16. #import <asl.h>
  17. #if !__has_feature(objc_arc)
  18. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  19. #endif
  20. const char* const kDDASLKeyDDLog = "DDLog";
  21. const char* const kDDASLDDLogValue = "1";
  22. static DDASLLogger *sharedInstance;
  23. @interface DDASLLogger () {
  24. aslclient _client;
  25. }
  26. @end
  27. @implementation DDASLLogger
  28. + (instancetype)sharedInstance {
  29. static dispatch_once_t DDASLLoggerOnceToken;
  30. dispatch_once(&DDASLLoggerOnceToken, ^{
  31. sharedInstance = [[[self class] alloc] init];
  32. });
  33. return sharedInstance;
  34. }
  35. - (instancetype)init {
  36. if (sharedInstance != nil) {
  37. return nil;
  38. }
  39. if ((self = [super init])) {
  40. // A default asl client is provided for the main thread,
  41. // but background threads need to create their own client.
  42. _client = asl_open(NULL, "com.apple.console", 0);
  43. }
  44. return self;
  45. }
  46. - (void)logMessage:(DDLogMessage *)logMessage {
  47. // Skip captured log messages
  48. if ([logMessage->_fileName isEqualToString:@"DDASLLogCapture"]) {
  49. return;
  50. }
  51. NSString * message = _logFormatter ? [_logFormatter formatLogMessage:logMessage] : logMessage->_message;
  52. if (logMessage) {
  53. const char *msg = [message UTF8String];
  54. size_t aslLogLevel;
  55. switch (logMessage->_flag) {
  56. // Note: By default ASL will filter anything above level 5 (Notice).
  57. // So our mappings shouldn't go above that level.
  58. case DDLogFlagError : aslLogLevel = ASL_LEVEL_CRIT; break;
  59. case DDLogFlagWarning : aslLogLevel = ASL_LEVEL_ERR; break;
  60. case DDLogFlagInfo : aslLogLevel = ASL_LEVEL_WARNING; break; // Regular NSLog's level
  61. case DDLogFlagDebug :
  62. case DDLogFlagVerbose :
  63. default : aslLogLevel = ASL_LEVEL_NOTICE; break;
  64. }
  65. static char const *const level_strings[] = { "0", "1", "2", "3", "4", "5", "6", "7" };
  66. // NSLog uses the current euid to set the ASL_KEY_READ_UID.
  67. uid_t const readUID = geteuid();
  68. char readUIDString[16];
  69. #ifndef NS_BLOCK_ASSERTIONS
  70. int l = snprintf(readUIDString, sizeof(readUIDString), "%d", readUID);
  71. #else
  72. snprintf(readUIDString, sizeof(readUIDString), "%d", readUID);
  73. #endif
  74. NSAssert(l < sizeof(readUIDString),
  75. @"Formatted euid is too long.");
  76. NSAssert(aslLogLevel < (sizeof(level_strings) / sizeof(level_strings[0])),
  77. @"Unhandled ASL log level.");
  78. aslmsg m = asl_new(ASL_TYPE_MSG);
  79. if (m != NULL) {
  80. if (asl_set(m, ASL_KEY_LEVEL, level_strings[aslLogLevel]) == 0 &&
  81. asl_set(m, ASL_KEY_MSG, msg) == 0 &&
  82. asl_set(m, ASL_KEY_READ_UID, readUIDString) == 0 &&
  83. asl_set(m, kDDASLKeyDDLog, kDDASLDDLogValue) == 0) {
  84. asl_send(_client, m);
  85. }
  86. asl_free(m);
  87. }
  88. //TODO handle asl_* failures non-silently?
  89. }
  90. }
  91. - (NSString *)loggerName {
  92. return @"cocoa.lumberjack.aslLogger";
  93. }
  94. @end