DDASLLogger.m 3.7 KB

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