1 // 2 // ETCoreMLLogging.h 3 // 4 // Copyright © 2024 Apple Inc. All rights reserved. 5 // 6 // Please refer to the license found in the LICENSE file in the root directory of the source tree. 7 8 #import <Foundation/Foundation.h> 9 10 #import <executorch/runtime/platform/log.h> 11 #import <os/log.h> 12 13 NS_ASSUME_NONNULL_BEGIN 14 15 /// The error domain for the delegate error. 16 extern NSErrorDomain const ETCoreMLErrorDomain; 17 18 /// The error codes that are exposed publicly. 19 typedef NS_ERROR_ENUM(ETCoreMLErrorDomain, ETCoreMLError) { 20 ETCoreMLErrorCorruptedData = 1, // AOT blob can't be parsed. 21 ETCoreMLErrorCorruptedMetadata, // AOT blob has incorrect or missing metadata. 22 ETCoreMLErrorCorruptedModel, // AOT blob has incorrect or missing CoreML model. 23 ETCoreMLErrorBrokenModel, // CoreML model doesn't match the input and output specification. 24 ETCoreMLErrorCompilationFailed, // CoreML model failed to compile. 25 ETCoreMLErrorModelCompilationNotSupported, // CoreML model compilation is not supported by the target. 26 ETCoreMLErrorModelProfilingNotSupported, // Model profiling is not supported by the target. 27 ETCoreMLErrorModelSaveFailed, // Failed to save CoreML model to disk. 28 ETCoreMLErrorModelCacheCreationFailed, // Failed to create model cache. 29 ETCoreMLErrorInternalError, // Internal error. 30 }; 31 32 @interface ETCoreMLErrorUtils : NSObject 33 34 + (NSError*)errorWithCode:(ETCoreMLError)code 35 underlyingError:(nullable NSError*)underlyingError 36 format:(nullable NSString*)description, ... NS_FORMAT_FUNCTION(3, 4); 37 38 + (NSError*)errorWithIntegerCode:(NSInteger)code 39 underlyingError:(nullable NSError*)underlyingError 40 format:(nullable NSString*)format 41 args:(va_list)args; 42 43 @property (class, strong, readonly, nonatomic, nullable) os_log_t loggingChannel; 44 45 @end 46 47 #pragma clang diagnostic push 48 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" 49 50 /// Record the error with `os_log_error` and fills `*errorOut` with `NSError`. 51 #define ETCoreMLLogErrorAndSetNSError(errorOut, errorCode, formatString, ...) \ 52 if (ET_LOG_ENABLED) { \ 53 ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \ 54 } else { \ 55 os_log_error(ETCoreMLErrorUtils.loggingChannel, formatString, ##__VA_ARGS__); \ 56 } \ 57 if (errorOut) { \ 58 *errorOut = \ 59 [NSError errorWithDomain:ETCoreMLErrorDomain \ 60 code:errorCode \ 61 userInfo:@{ \ 62 NSLocalizedDescriptionKey : [NSString stringWithFormat:@formatString, ##__VA_ARGS__] \ 63 }]; \ 64 } 65 66 /// Record the error and its underlying error with `os_log_error` and fills `*errorOut` with `NSError`. 67 #define ETCoreMLLogUnderlyingErrorAndSetNSError(errorOut, errorCode, underlyingNSError, formatString, ...) \ 68 if (ET_LOG_ENABLED) { \ 69 ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \ 70 } else { \ 71 os_log_error(ETCoreMLErrorUtils.loggingChannel, \ 72 formatString ", with underlying error= %@.", \ 73 ##__VA_ARGS__, \ 74 (underlyingNSError).localizedDescription); \ 75 } \ 76 if (errorOut) { \ 77 *errorOut = [ETCoreMLErrorUtils errorWithCode:errorCode \ 78 underlyingError:underlyingNSError \ 79 format:@formatString, ##__VA_ARGS__]; \ 80 } 81 82 #define ETCoreMLLogError(error, formatString, ...) \ 83 if (ET_LOG_ENABLED) { \ 84 ET_LOG(Error, "%s", [NSString stringWithFormat:@formatString, ##__VA_ARGS__].UTF8String); \ 85 } else { \ 86 os_log_error(ETCoreMLErrorUtils.loggingChannel, \ 87 formatString ", with error= %@.", \ 88 ##__VA_ARGS__, \ 89 (error).localizedDescription); \ 90 } 91 92 93 #pragma clang diagnostic pop 94 95 NS_ASSUME_NONNULL_END 96