ConfigurationGlobals.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, 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. #if canImport(Synchronization)
  16. public import Synchronization
  17. #endif
  18. #if canImport(Synchronization)
  19. #if compiler(>=6.0)
  20. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  21. extension DDLogLevel: @retroactive AtomicRepresentable {}
  22. #else
  23. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  24. extension DDLogLevel: AtomicRepresentable {}
  25. #endif
  26. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  27. private let _dynamicLogLevel = Atomic(DDLogLevel.all)
  28. #endif
  29. private let _dynamicLogLevelLock = NSLock()
  30. nonisolated(unsafe) private var _dynamicLogLevelStorage = DDLogLevel.all
  31. private func _readDynamicLogLevel() -> DDLogLevel {
  32. #if canImport(Synchronization)
  33. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  34. return _dynamicLogLevel.load(ordering: .relaxed)
  35. }
  36. #endif
  37. _dynamicLogLevelLock.lock()
  38. defer { _dynamicLogLevelLock.unlock() }
  39. return _dynamicLogLevelStorage
  40. }
  41. private func _writeDynamicLogLevel(_ newValue: DDLogLevel) {
  42. #if canImport(Synchronization)
  43. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  44. _dynamicLogLevel.store(newValue, ordering: .relaxed)
  45. return
  46. }
  47. #endif
  48. _dynamicLogLevelLock.lock()
  49. defer { _dynamicLogLevelLock.unlock() }
  50. _dynamicLogLevelStorage = newValue
  51. }
  52. /// The log level that can dynamically limit log messages (vs. the static ``DDDefaultLogLevel``). This log level will only be checked, if the message passes the ``DDDefaultLogLevel``.
  53. public nonisolated(unsafe) var dynamicLogLevel: DDLogLevel {
  54. get {
  55. _readDynamicLogLevel()
  56. }
  57. set {
  58. _writeDynamicLogLevel(newValue)
  59. }
  60. }
  61. /// Resets the ``dynamicLogLevel`` to ``DDLogLevel/all``.
  62. /// - SeeAlso: ``dynamicLogLevel``
  63. @inlinable
  64. public func resetDynamicLogLevel() {
  65. dynamicLogLevel = .all
  66. }
  67. @available(*, deprecated, message: "Please use dynamicLogLevel", renamed: "dynamicLogLevel")
  68. public var defaultDebugLevel: DDLogLevel {
  69. get {
  70. dynamicLogLevel
  71. }
  72. set {
  73. dynamicLogLevel = newValue
  74. }
  75. }
  76. @available(*, deprecated, message: "Please use resetDynamicLogLevel", renamed: "resetDynamicLogLevel")
  77. public func resetDefaultDebugLevel() {
  78. resetDynamicLogLevel()
  79. }
  80. #if canImport(Synchronization)
  81. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  82. private let _asyncLoggingEnabled = Atomic(true)
  83. #endif
  84. private let _asyncLoggingEnabledLock = NSLock()
  85. nonisolated(unsafe) private var _asyncLoggingEnabledStorage = true
  86. private func _readAsyncLoggingEnabled() -> Bool {
  87. #if canImport(Synchronization)
  88. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  89. return _asyncLoggingEnabled.load(ordering: .relaxed)
  90. }
  91. #endif
  92. _asyncLoggingEnabledLock.lock()
  93. defer { _asyncLoggingEnabledLock.unlock() }
  94. return _asyncLoggingEnabledStorage
  95. }
  96. private func _writeAsyncLoggingEnabled(_ newValue: Bool) {
  97. #if canImport(Synchronization)
  98. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  99. _asyncLoggingEnabled.store(newValue, ordering: .relaxed)
  100. return
  101. }
  102. #endif
  103. _asyncLoggingEnabledLock.lock()
  104. defer { _asyncLoggingEnabledLock.unlock() }
  105. _asyncLoggingEnabledStorage = newValue
  106. }
  107. /// If `true`, all logs (except errors) are logged asynchronously by default.
  108. public nonisolated(unsafe) var asyncLoggingEnabled: Bool {
  109. get {
  110. _readAsyncLoggingEnabled()
  111. }
  112. set {
  113. _writeAsyncLoggingEnabled(newValue)
  114. }
  115. }