Skip to content

Commit f86b47b

Browse files
authored
Merge pull request #34 from sukov/version-2.1
Version 2.1.0
2 parents d8204b9 + d8bf3c8 commit f86b47b

35 files changed

Lines changed: 1195 additions & 346 deletions

CachingPlayerItem.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'CachingPlayerItem'
3-
s.version = '2.0.2'
3+
s.version = '2.1.0'
44
s.summary = 'Cache & Play audio and video files'
55

66
s.homepage = 'https://github.com/sukov/CachingPlayerItem'

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- CachingPlayerItem (2.0.1)
2+
- CachingPlayerItem (2.1.0)
33
- Nimble (9.2.1)
44
- Quick (4.0.0)
55

@@ -18,7 +18,7 @@ EXTERNAL SOURCES:
1818
:path: "../"
1919

2020
SPEC CHECKSUMS:
21-
CachingPlayerItem: 7a369d20ddc09f5ac79322fd8ecd5f8369523216
21+
CachingPlayerItem: 6bde08e430aaea51b65cf6b460f0e1497644113a
2222
Nimble: e7e615c0335ee4bf5b0d786685451e62746117d5
2323
Quick: 6473349e43b9271a8d43839d9ba1c442ed1b7ac4
2424

Example/Pods/Local Podspecs/CachingPlayerItem.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 273 additions & 269 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/CachingPlayerItem/CachingPlayerItem-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/AppLogger.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// AppLogger.swift
3+
// CachingPlayerItem
4+
//
5+
// Created by Gorjan Shukov on 07/17/25.
6+
//
7+
8+
import Foundation
9+
10+
/// Log levels for `AppLogger`.
11+
public enum LogLevel: Int, Comparable, CustomStringConvertible {
12+
case none = 0
13+
case info
14+
case warning
15+
case error
16+
17+
/// Emoji representation of the log level.
18+
public var description: String {
19+
switch self {
20+
case .info:
21+
return "ℹ️"
22+
case .warning:
23+
return "⚠️"
24+
case .error:
25+
return "🔴"
26+
case .none:
27+
return ""
28+
}
29+
}
30+
31+
/// Comparable conformance.
32+
public static func < (lhs: LogLevel, rhs: LogLevel) -> Bool {
33+
lhs.rawValue < rhs.rawValue
34+
}
35+
}
36+
37+
/// Internal logger for the library.
38+
final class AppLogger {
39+
private static let dateFormatter: DateFormatter = {
40+
let formatter = DateFormatter()
41+
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
42+
return formatter
43+
}()
44+
45+
static var currentLevel: LogLevel { CachingPlayerItemConfiguration.logLevel }
46+
47+
static func info(_ message: @autoclosure () -> String, file: String = #file, function: String = #function, line: Int = #line) {
48+
log(.info, message(), file: file, function: function, line: line)
49+
}
50+
51+
static func warning(_ message: @autoclosure () -> String, file: String = #file, function: String = #function, line: Int = #line) {
52+
log(.warning, message(), file: file, function: function, line: line)
53+
}
54+
55+
static func error(_ message: @autoclosure () -> String, file: String = #file, function: String = #function, line: Int = #line) {
56+
log(.error, message(), file: file, function: function, line: line)
57+
}
58+
59+
/// Logs a message if the specified log level is greater than or equal to the current log level.
60+
private static func log(_ level: LogLevel, _ message: @autoclosure () -> String, file: String, function: String, line: Int) {
61+
guard level >= currentLevel, currentLevel != .none else { return }
62+
63+
let fileName = (file as NSString).lastPathComponent
64+
let timestamp = dateFormatter.string(from: Date())
65+
66+
print("[CachingPlayerItem \(level)] [\(timestamp)] [\(fileName):\(line)] \(function) > \(message())")
67+
}
68+
}

Source/CachingPlayerItem.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public final class CachingPlayerItem: AVPlayerItem {
4747

4848
/// Useful for keeping relevant model associated with CachingPlayerItem instance. This is a **strong** reference, be mindful not to create a **retain cycle**.
4949
public var passOnObject: Any?
50+
/// `delegate` for status updates.
5051
public weak var delegate: CachingPlayerItemDelegate?
5152

5253
// MARK: Public init
@@ -276,14 +277,15 @@ public final class CachingPlayerItem: AVPlayerItem {
276277
switch status {
277278
case .readyToPlay:
278279
// Player item is ready to play.
280+
AppLogger.info("CachingPlayerItem status: ready to play")
279281
DispatchQueue.main.async { self.delegate?.playerItemReadyToPlay?(self) }
280282
case .failed:
281283
// Player item failed. See error.
282-
print("CachingPlayerItem status: failed with error: \(String(describing: error))")
284+
AppLogger.error("CachingPlayerItem status: failed with error: \(String(describing: error))")
283285
DispatchQueue.main.async { self.delegate?.playerItemDidFailToPlay?(self, withError: self.error) }
284286
case .unknown:
285287
// Player item is not yet ready.
286-
print("CachingPlayerItem status: uknown with error: \(String(describing: error))")
288+
AppLogger.error("CachingPlayerItem status: uknown with error: \(String(describing: error))")
287289
@unknown default:
288290
break
289291
}

Source/CachingPlayerItemConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public enum CachingPlayerItemConfiguration {
2424

2525
/// Flag for deciding whether an `NSFileWriteOutOfSpaceError` should be thrown when there is not enough available disk space left for caching the entire media file. Defaults to `true`.
2626
public static var shouldCheckAvailableDiskSpaceBeforeCaching: Bool = true
27+
28+
/// Log level. Defaults to `none`.
29+
public static var logLevel: LogLevel = .none
2730
}
2831

2932
fileprivate extension Int {

Source/MediaFileHandle.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class MediaFileHandle {
2323
if !FileManager.default.fileExists(atPath: filePath) {
2424
FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil)
2525
} else {
26-
print("CachingPlayerItem warning: File already exists at \(filePath). A non empty file can cause unexpected behavior.")
26+
AppLogger.warning("File already exists at \(filePath). A non empty file can cause unexpected behavior.")
2727
}
2828
}
2929

@@ -47,7 +47,7 @@ extension MediaFileHandle {
4747
do {
4848
return try FileManager.default.attributesOfItem(atPath: filePath)
4949
} catch let error as NSError {
50-
print("FileAttribute error: \(error)")
50+
AppLogger.error("Failed fetching attributes for \(filePath) with error: \(error)")
5151
}
5252
return nil
5353
}
@@ -92,7 +92,7 @@ extension MediaFileHandle {
9292
do {
9393
try FileManager.default.removeItem(atPath: filePath)
9494
} catch let error {
95-
print("File deletion error: \(error)")
95+
AppLogger.error("File deletion failed at \(filePath) with error: \(error)")
9696
}
9797
}
9898
}

0 commit comments

Comments
 (0)