xref: /aosp_15_r20/external/executorch/backends/apple/coreml/runtime/delegate/ETCoreMLLogging.mm (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1//
2// ETCoreMLLogging.mm
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 <ETCoreMLLogging.h>
9
10#import <ETCoreMLStrings.h>
11
12const NSErrorDomain ETCoreMLErrorDomain = @"com.apple.executorchcoreml";
13
14static os_log_t LoggingChannel() {
15    static dispatch_once_t onceToken;
16    static os_log_t coreChannel;
17    dispatch_once(&onceToken, ^{
18        coreChannel = os_log_create(ETCoreMLStrings.productIdentifier.UTF8String, ETCoreMLStrings.productName.UTF8String);
19        if (!coreChannel) {
20            os_log_error(OS_LOG_DEFAULT, "Failed to create os_log_t coreChannel");
21        }
22    });
23    return coreChannel;
24}
25
26
27@implementation ETCoreMLErrorUtils
28
29+ (NSError *)errorWithIntegerCode:(NSInteger)code
30                  underlyingError:(nullable NSError *)underlyingError
31                           format:(nullable NSString *)format
32                             args:(va_list)args {
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wformat-nonliteral"
35#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
36    NSString *description = format ? [[NSString alloc] initWithFormat:format arguments:args] : nil;
37#pragma clang diagnostic pop
38
39    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
40    if (description) {
41        userInfo[NSLocalizedDescriptionKey] =  description;
42    }
43    if (underlyingError) {
44        userInfo[NSUnderlyingErrorKey] = underlyingError;
45    }
46
47    return [NSError errorWithDomain:ETCoreMLErrorDomain
48                               code:code
49                           userInfo:userInfo];
50}
51
52+ (NSError *)errorWithCode:(ETCoreMLError)code
53           underlyingError:(nullable NSError *)underlyingError
54                    format:(nullable NSString *)format
55                      args:(va_list)args {
56    return [self errorWithIntegerCode:code underlyingError:underlyingError format:format args:args];
57}
58
59+ (NSError *)errorWithCode:(ETCoreMLError)type
60           underlyingError:(nullable NSError *)underlyingError
61                    format:(nullable NSString *)description, ... {
62    va_list args;
63    va_start(args, description);
64    NSError *error = [self errorWithCode:type underlyingError:underlyingError format:description args:args];
65    va_end(args);
66    return error;
67}
68
69+ (os_log_t)loggingChannel {
70    return LoggingChannel();
71}
72
73@end
74