Skip to content

Commit

Permalink
Add logging to the demo app. (pytorch#2337)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#2337
bypass-github-export-checks

Reviewed By: cccclai

Differential Revision: D54711006

fbshipit-source-id: f1865b36c3a548a06da00d6e02ee1652970d1747
  • Loading branch information
shoumikhin authored and facebook-github-bot committed Mar 9, 2024
1 parent 36c21bd commit ceb1f1d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ContentView: View {
TopBar(title: "ExecuTorch Demo")
ClassificationLabelView(controller: classificationController)
Spacer()
ClassificationTimeView(controller: classificationController).hidden()
ClassificationTimeView(controller: classificationController)
ModeSelector(controller: classificationController)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
* LICENSE file in the root directory of this source tree.
*/

import ExecuTorch
import ImageClassification
import UIKit

import os.log

public enum MobileNetClassifierError: Error {
case inputPointer
case rawData
Expand Down Expand Up @@ -43,6 +46,16 @@ public class MobileNetClassifier: ImageClassification {
mobileNetClassifier = ETMobileNetClassifier(filePath: modelFilePath)
rawDataBuffer = [UInt8](repeating: 0, count: Int(Self.cropSize * Self.cropSize) * 4)
normalizedBuffer = [Float](repeating: 0, count: rawDataBuffer.count / 4 * 3)

#if DEBUG
Log.shared.add(sink: self)
#endif
}

deinit {
#if DEBUG
Log.shared.remove(sink: self)
#endif
}

public func classify(image: UIImage) throws -> [Classification] {
Expand Down Expand Up @@ -133,3 +146,24 @@ public class MobileNetClassifier: ImageClassification {
return expInput.map { $0 / sumExpInput }
}
}

#if DEBUG
extension MobileNetClassifier: LogSink {
public func log(level: LogLevel, timestamp: TimeInterval, filename: String, line: UInt, message: String) {
let logMessage = "executorch:\(filename):\(line) \(message)"

switch level {
case .debug:
os_log(.debug, "%{public}@", logMessage)
case .info:
os_log(.info, "%{public}@", logMessage)
case .error:
os_log(.error, "%{public}@", logMessage)
case .fatal:
os_log(.fault, "%{public}@", logMessage)
default:
os_log("%{public}@", logMessage)
}
}
}
#endif
4 changes: 2 additions & 2 deletions extension/apple/ExecuTorch/Exported/ExecuTorchLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ NS_SWIFT_NAME(Log)
*
* @param sink The log sink to add.
*/
- (void)addSink:(id<ExecuTorchLogSink>)sink;
- (void)addSink:(id<ExecuTorchLogSink>)sink NS_SWIFT_NAME(add(sink:));

/**
* Removes a previously added log sink.
*
* @param sink The log sink to remove.
*/
- (void)removeSink:(id<ExecuTorchLogSink>)sink;
- (void)removeSink:(id<ExecuTorchLogSink>)sink NS_SWIFT_NAME(remove(sink:));

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
Expand Down

0 comments on commit ceb1f1d

Please sign in to comment.