LogFormatterCustom.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "LogFormatterCustom.h"
  21. #import <Foundation/Foundation.h>
  22. @implementation LogFormatterCustom {
  23. NSString *processName;
  24. NSString *processID;
  25. }
  26. static NSString *dateFormatString = @"yyyy-MM-dd HH:mm:ss.SSSZZZZZ";
  27. - (instancetype)init
  28. {
  29. self = [super init];
  30. if (self) {
  31. processName = [[NSProcessInfo processInfo] processName];
  32. processID = [NSString stringWithFormat:@"%i", (int)getpid()];
  33. }
  34. return self;
  35. }
  36. - (NSString *)stringFromDate:(NSDate *)date {
  37. atomic_fetch_add_explicit(&atomicLoggerCount, 0, memory_order_relaxed);
  38. if (atomicLoggerCount <= 1) {
  39. // Single-threaded mode.
  40. if (threadUnsafeDateFormatter == nil) {
  41. threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
  42. [threadUnsafeDateFormatter setDateFormat:dateFormatString];
  43. }
  44. return [threadUnsafeDateFormatter stringFromDate:date];
  45. } else {
  46. // Multi-threaded mode.
  47. // NSDateFormatter is NOT thread-safe.
  48. NSString *key = @"MyCustomFormatter_NSDateFormatter";
  49. NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
  50. NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];
  51. if (dateFormatter == nil) {
  52. dateFormatter = [[NSDateFormatter alloc] init];
  53. [dateFormatter setDateFormat:dateFormatString];
  54. [threadDictionary setObject:dateFormatter forKey:key];
  55. }
  56. return [dateFormatter stringFromDate:date];
  57. }
  58. }
  59. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  60. NSString *logLevel;
  61. switch (logMessage->_flag) {
  62. case DDLogFlagError : logLevel = @"[Err]"; break;
  63. case DDLogFlagWarning : logLevel = @"[Warn]"; break;
  64. case DDLogFlagInfo : logLevel = @"[Info]"; break;
  65. case DDLogFlagDebug : logLevel = @"[Debug]"; break;
  66. case DDLogFlagNotice : logLevel = @"[Notice]"; break;
  67. default : logLevel = @"[Verbose]"; break;
  68. }
  69. NSString *dateAndTime = [self stringFromDate:logMessage.timestamp];
  70. return [NSString stringWithFormat:@"%@ %@[%@:%@] %@:%lu %@ %@", dateAndTime, processName, processID, logMessage->_threadID, logMessage->_fileName, logMessage->_line, logLevel, logMessage->_message];
  71. }
  72. - (void)didAddToLogger:(id <DDLogger>)logger {
  73. atomic_fetch_add_explicit(&atomicLoggerCount, 1, memory_order_relaxed);
  74. }
  75. - (void)willRemoveFromLogger:(id <DDLogger>)logger {
  76. atomic_fetch_sub_explicit(&atomicLoggerCount, 1, memory_order_relaxed);
  77. }
  78. @end