1 /*
2 * Copyright (c) 2024 MediaTek Inc.
3 *
4 * Licensed under the BSD License (the "License"); you may not use this file
5 * except in compliance with the License. See the license file in the root
6 * directory of this source tree for more details.
7 */
8
9 #pragma once
10
11 #include "api/NeuronAdapter.h"
12
13 #include <android/log.h>
14 #include <sys/system_properties.h>
15
16 #include <cstdlib>
17 #include <string>
18
19 namespace executorch {
20 namespace backends {
21 namespace neuron {
22
23 #define AndroidLog(priority, tag, format, ...) \
24 __android_log_print(priority, tag, format, ##__VA_ARGS__)
25
26 #define LogError(tag, format, ...) \
27 AndroidLog(ANDROID_LOG_ERROR, tag, format, ##__VA_ARGS__)
28
29 #define LogWarn(tag, format, ...) \
30 AndroidLog(ANDROID_LOG_WARN, tag, format, ##__VA_ARGS__)
31
32 #define LogInfo(tag, format, ...) \
33 AndroidLog(ANDROID_LOG_INFO, tag, format, ##__VA_ARGS__)
34
35 #define STRINGIFY(x) #x
36 #define TOSTRING(x) STRINGIFY(x)
37
38 #define CHECK_VALID_PTR(ptr) \
39 do { \
40 if (__builtin_expect(ptr == nullptr, 0)) { \
41 LogError( \
42 "NeuronBackend", \
43 "Check fail: " #ptr \
44 " == nullptr at line " TOSTRING(__LINE__) " at file " __FILE__); \
45 return NEURON_UNEXPECTED_NULL; \
46 } \
47 } while (0)
48
49 #define CHECK_NO_ERROR(value) \
50 do { \
51 if (__builtin_expect(value != NEURON_NO_ERROR, 0)) { \
52 LogError( \
53 "NeuronBackend", \
54 "Check fail: " #value " != NEURON_NO_ERROR at line " TOSTRING( \
55 __LINE__) " at file " __FILE__); \
56 return value; \
57 } \
58 } while (0)
59
60 #define CHECK_TRUE(value) \
61 do { \
62 if (__builtin_expect(value != true, 0)) { \
63 LogError( \
64 "NeuronBackend", \
65 "Check fail: " #value \
66 " != true at line " TOSTRING(__LINE__) " at file " __FILE__); \
67 return NEURON_BAD_STATE; \
68 } \
69 } while (0)
70
ReadSystemProperty(std::string & property)71 inline int ReadSystemProperty(std::string& property) {
72 char property_value[PROP_VALUE_MAX];
73 if (__system_property_get(property.c_str(), property_value)) {
74 LogInfo("Get System Property %s : %s", property.c_str(), property_value);
75 try {
76 int value = std::stoi(property_value);
77 return value;
78 } catch (...) {
79 return -1;
80 }
81 }
82 return -1;
83 }
84
85 } // namespace neuron
86 } // namespace backends
87 } // namespace executorch
88