Skip to content

Commit ceb1f1d

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Add logging to the demo app. (#2337)
Summary: Pull Request resolved: #2337 bypass-github-export-checks Reviewed By: cccclai Differential Revision: D54711006 fbshipit-source-id: f1865b36c3a548a06da00d6e02ee1652970d1747
1 parent 36c21bd commit ceb1f1d

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Sources/App/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct ContentView: View {
3333
TopBar(title: "ExecuTorch Demo")
3434
ClassificationLabelView(controller: classificationController)
3535
Spacer()
36-
ClassificationTimeView(controller: classificationController).hidden()
36+
ClassificationTimeView(controller: classificationController)
3737
ModeSelector(controller: classificationController)
3838
}
3939
}

examples/demo-apps/apple_ios/ExecuTorchDemo/ExecuTorchDemo/Sources/MobileNet/MobileNetClassifier.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
import ExecuTorch
910
import ImageClassification
1011
import UIKit
1112

13+
import os.log
14+
1215
public enum MobileNetClassifierError: Error {
1316
case inputPointer
1417
case rawData
@@ -43,6 +46,16 @@ public class MobileNetClassifier: ImageClassification {
4346
mobileNetClassifier = ETMobileNetClassifier(filePath: modelFilePath)
4447
rawDataBuffer = [UInt8](repeating: 0, count: Int(Self.cropSize * Self.cropSize) * 4)
4548
normalizedBuffer = [Float](repeating: 0, count: rawDataBuffer.count / 4 * 3)
49+
50+
#if DEBUG
51+
Log.shared.add(sink: self)
52+
#endif
53+
}
54+
55+
deinit {
56+
#if DEBUG
57+
Log.shared.remove(sink: self)
58+
#endif
4659
}
4760

4861
public func classify(image: UIImage) throws -> [Classification] {
@@ -133,3 +146,24 @@ public class MobileNetClassifier: ImageClassification {
133146
return expInput.map { $0 / sumExpInput }
134147
}
135148
}
149+
150+
#if DEBUG
151+
extension MobileNetClassifier: LogSink {
152+
public func log(level: LogLevel, timestamp: TimeInterval, filename: String, line: UInt, message: String) {
153+
let logMessage = "executorch:\(filename):\(line) \(message)"
154+
155+
switch level {
156+
case .debug:
157+
os_log(.debug, "%{public}@", logMessage)
158+
case .info:
159+
os_log(.info, "%{public}@", logMessage)
160+
case .error:
161+
os_log(.error, "%{public}@", logMessage)
162+
case .fatal:
163+
os_log(.fault, "%{public}@", logMessage)
164+
default:
165+
os_log("%{public}@", logMessage)
166+
}
167+
}
168+
}
169+
#endif

extension/apple/ExecuTorch/Exported/ExecuTorchLog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ NS_SWIFT_NAME(Log)
5959
*
6060
* @param sink The log sink to add.
6161
*/
62-
- (void)addSink:(id<ExecuTorchLogSink>)sink;
62+
- (void)addSink:(id<ExecuTorchLogSink>)sink NS_SWIFT_NAME(add(sink:));
6363

6464
/**
6565
* Removes a previously added log sink.
6666
*
6767
* @param sink The log sink to remove.
6868
*/
69-
- (void)removeSink:(id<ExecuTorchLogSink>)sink;
69+
- (void)removeSink:(id<ExecuTorchLogSink>)sink NS_SWIFT_NAME(remove(sink:));
7070

7171
+ (instancetype)new NS_UNAVAILABLE;
7272
- (instancetype)init NS_UNAVAILABLE;

0 commit comments

Comments
 (0)