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
)
}
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I'm testing my App and the preview in Xcode keeps refreezing by itself preventing me from testing my code. It refreshes about once a second despite me not doing anything. Any ideas? Thank you!
Topic:
Developer Tools & Services
SubTopic:
Xcode
Error Diagnostics
I'm able to run my app on a simulator, but previews aren't working even for the simplest test preview.
I build my project with XcodeGen.
Here is my project.yml file:
name: Ecstasy
options:
deploymentTarget:
iOS: 17.0
xcodeVersion: "15.2"
developmentLanguage: en
targets:
Ecstasy:
type: application
platform: iOS
sources:
- path: Sources
- path: Resources
info:
path: Configurations/Info.plist
properties:
CFBundleDevelopmentRegion: "$(DEVELOPMENT_LANGUAGE)"
CFBundleExecutable: "$(EXECUTABLE_NAME)"
CFBundleIdentifier: "$(PRODUCT_BUNDLE_IDENTIFIER)"
CFBundleInfoDictionaryVersion: "6.0"
CFBundleName: "$(PRODUCT_NAME)"
CFBundlePackageType: "APPL"
CFBundleShortVersionString: "1.0"
CFBundleVersion: "1"
UILaunchStoryboardName: ""
UIViewControllerBasedStatusBarAppearance: true
UIStatusBarHidden: false
UIRequiresFullScreen: true
UISupportedInterfaceOrientations:
- UIInterfaceOrientationPortrait
UIUserInterfaceStyle: Light
settings:
base:
DEVELOPMENT_TEAM: MLJ2C965T7
PRODUCT_BUNDLE_IDENTIFIER: com.raw-e.Ecstasy
SWIFT_OPTIMIZATION_LEVEL: "-O"
SWIFT_COMPILATION_MODE: wholemodule
ENABLE_PREVIEWS: YES
DEBUG_INFORMATION_FORMAT: dwarf-with-dsym
CLANG_ENABLE_MODULES: YES
SWIFT_VERSION: 6.0
TARGETED_DEVICE_FAMILY: 1
ENABLE_BITCODE: NO
SWIFT_ACTIVE_COMPILATION_CONDITIONS: DEBUG
SWIFT_EMIT_LOC_STRINGS: YES
SWIFT_STRICT_CONCURRENCY: complete
ENABLE_USER_SCRIPT_SANDBOXING: YES
configs:
debug:
SWIFT_OPTIMIZATION_LEVEL: "-Onone"
SWIFT_COMPILATION_MODE: incremental
ENABLE_TESTABILITY: YES
GCC_OPTIMIZATION_LEVEL: 0
ONLY_ACTIVE_ARCH: YES
DEBUG_INFORMATION_FORMAT: dwarf
ENABLE_PREVIEWS: YES
SWIFT_ACTIVE_COMPILATION_CONDITIONS: "DEBUG PREVIEW"
release:
SWIFT_OPTIMIZATION_LEVEL: "-O"
SWIFT_COMPILATION_MODE: wholemodule
ENABLE_TESTABILITY: NO
GCC_OPTIMIZATION_LEVEL: s
ONLY_ACTIVE_ARCH: NO
dependencies:
- package: APITime
- package: GUITime
- package: LoggingTime
- package: Shares
packages:
APITime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/APITime" }
GUITime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/GUITime" }
LoggingTime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/LoggingTime" }
Shares: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/Shares" }
Topic:
Developer Tools & Services
SubTopic:
Xcode
Diagnostics
I'm able to build my project and run it on a simulator.
I generate my project with xcodegen which I've added a bunch of things to in attempting to fix this but failed!
This is my project.yml file:
name: Ecstasy
options:
deploymentTarget:
iOS: 17.0
xcodeVersion: "15.2"
developmentLanguage: en
targets:
Ecstasy:
type: application
platform: iOS
sources:
- path: Sources
- path: Resources
info:
path: Configurations/Info.plist
properties:
CFBundleDevelopmentRegion: "$(DEVELOPMENT_LANGUAGE)"
CFBundleExecutable: "$(EXECUTABLE_NAME)"
CFBundleIdentifier: "$(PRODUCT_BUNDLE_IDENTIFIER)"
CFBundleInfoDictionaryVersion: "6.0"
CFBundleName: "$(PRODUCT_NAME)"
CFBundlePackageType: "APPL"
CFBundleShortVersionString: "1.0"
CFBundleVersion: "1"
UILaunchStoryboardName: ""
UIViewControllerBasedStatusBarAppearance: true
UIStatusBarHidden: false
UIRequiresFullScreen: true
UISupportedInterfaceOrientations:
- UIInterfaceOrientationPortrait
UIUserInterfaceStyle: Light
settings:
base:
DEVELOPMENT_TEAM: MLJ2C965T7
PRODUCT_BUNDLE_IDENTIFIER: com.raw-e.Ecstasy
SWIFT_OPTIMIZATION_LEVEL: "-O"
SWIFT_COMPILATION_MODE: wholemodule
ENABLE_PREVIEWS: YES
DEBUG_INFORMATION_FORMAT: dwarf-with-dsym
CLANG_ENABLE_MODULES: YES
SWIFT_VERSION: 5.0
TARGETED_DEVICE_FAMILY: 1
ENABLE_BITCODE: NO
SWIFT_ACTIVE_COMPILATION_CONDITIONS: DEBUG
SWIFT_EMIT_LOC_STRINGS: YES
SWIFT_STRICT_CONCURRENCY: complete
ENABLE_USER_SCRIPT_SANDBOXING: YES
configs:
debug:
SWIFT_OPTIMIZATION_LEVEL: "-Onone"
SWIFT_COMPILATION_MODE: incremental
ENABLE_TESTABILITY: YES
GCC_OPTIMIZATION_LEVEL: 0
ONLY_ACTIVE_ARCH: YES
DEBUG_INFORMATION_FORMAT: dwarf
ENABLE_PREVIEWS: YES
SWIFT_ACTIVE_COMPILATION_CONDITIONS: "DEBUG PREVIEW"
release:
SWIFT_OPTIMIZATION_LEVEL: "-O"
SWIFT_COMPILATION_MODE: wholemodule
ENABLE_TESTABILITY: NO
GCC_OPTIMIZATION_LEVEL: s
ONLY_ACTIVE_ARCH: NO
dependencies:
- package: APITime
- package: GUITime
- package: LoggingTime
- package: Shares
packages:
APITime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/APITime" }
GUITime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/GUITime" }
LoggingTime: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/LoggingTime" }
Shares: { path: "/Users/raw-e/Desktop/Useful Swift Things/My Packages/Shares" }
Topic:
Developer Tools & Services
SubTopic:
Xcode