Post

Replies

Boosts

Views

Activity

Why can't I use Task in an iOS only package?
Getting this error: 'Task' is only available in macOS 10.15 or newerSourceKit LoggerForPreviews.swift(130, 9): Add 'if #available' version check LoggerForPreviews.swift(129, 24): Add @available attribute to enclosing static method LoggerForPreviews.swift(5, 20): Add @available attribute to enclosing actor Does it have something to do with developing in VSCode? import Foundation import SwiftUI // Logger: A concise, globally accessible logging utility for SwiftUI previews public final actor PreviewLogger: Sendable { // LogLevel: Defines severity levels for logging public enum LogLevel: Int, Sendable, CaseIterable { // Define cases based on LOG_LEVEL_MAP case trace, debug, verbose, info, notice, warning, error, critical, fatal // Computed property to get order based on case declaration private var order: Int { switch self { case .trace: return 0 case .debug: return 1 case .verbose: return 2 case .info: return 3 case .notice: return 4 case .warning: return 5 case .error: return 6 case .critical: return 7 case .fatal: return 8 } } public var description: String { // Use capitalized raw value for description return String(describing: self).uppercased() } // Static function to compare log levels static func >= (lhs: LogLevel, rhs: LogLevel) -> Bool { return lhs.order >= rhs.order } } // Shared instance for global access public static let shared = PreviewLogger() // Current log level public var logLevelThreshold: LogLevel = .info private init() {} // Configure the logger's log level public func configure(logLevelThreshold: LogLevel) { self.logLevelThreshold = logLevelThreshold } // Helper function to center text within a given width private func centered(_ text: String, in separator: String) -> String { let totalLength = separator.count let textLength = text.count if textLength >= totalLength { return text } let padding = (totalLength - textLength) / 2 let padString = String(repeating: " ", count: padding) return padString + text } // Main logging function public func log( _ message: String, level: LogLevel = .info, file: String = #file, function: String = #function, line: Int = #line, callerFile: String? = nil, callerFunction: String? = nil, callerLine: Int? = nil ) { #if DEBUG guard ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" else { return } guard level >= logLevelThreshold else { return } let fileName = (file as NSString).lastPathComponent let cleanFunction = function.replacingOccurrences(of: "(_:file:function:line:)", with: "") let levelIcon: String switch level { case .trace: levelIcon = "πŸ”" case .debug: levelIcon = "πŸ› οΈ" case .verbose: levelIcon = "πŸ“" case .info: levelIcon = "ℹ️" case .notice: levelIcon = "πŸ“’" case .warning: levelIcon = "⚠️" case .error: levelIcon = "❌" case .critical: levelIcon = "🚨" case .fatal: levelIcon = "πŸ’€" } let header = "[\(levelIcon) \(level.description)]" let separator = "Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β· Β·" let finalSeparator = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" let centeredHeader = centered(header, in: separator) var output = """ \n\(separator) \(centeredHeader) """ let locationInfo = "πŸ“ \(fileName):\(line) ➜ \(cleanFunction)" let centeredLocation = centered(locationInfo, in: separator) output += "\n\(centeredLocation)" if let callerFile = callerFile, let callerLine = callerLine { let callerFileName = (callerFile as NSString).lastPathComponent let callerInfo = "πŸ“± Called from: \(callerFileName):\(callerLine)" let centeredCallerInfo = centered(callerInfo, in: separator) output += "\n\(centeredCallerInfo)" } output += """ \n\(separator)\n\(message)\n\(finalSeparator) """ print(output) #endif } // Static Methods public static func configure(logLevelThreshold: LogLevel) { Task { await shared.configure(logLevelThreshold: logLevelThreshold) } } public static func log( _ message: String, level: LogLevel = .info, file: String = #file, function: String = #function, line: Int = #line, callerFile: String? = nil, callerFunction: String? = nil, callerLine: Int? = nil ) { Task { await shared.log( message, level: level, file: file, function: function, line: line, callerFile: callerFile, callerFunction: callerFunction, callerLine: callerLine ) } } }
1
0
125
1w