1 /*
2 * Copyright (c) Qualcomm Innovation Center, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 #include <executorch/backends/qualcomm/runtime/Logging.h>
10 #include <executorch/backends/qualcomm/runtime/backends/QnnImplementation.h>
11 #include <executorch/backends/qualcomm/runtime/backends/QnnLogger.h>
12
13 #include <iostream>
14 #include <memory>
15
16 #include "QnnLog.h"
17 namespace executorch {
18 namespace backends {
19 namespace qnn {
LoggingCallback(const char * fmt,QnnLog_Level_t level,std::uint64_t,va_list args)20 void LoggingCallback(
21 const char* fmt,
22 QnnLog_Level_t level,
23 std::uint64_t /*timestamp*/,
24 va_list args) {
25 constexpr const int kLogBufferSize = 512;
26 QnnExecuTorchLogLevel log_level;
27 switch (level) {
28 case QNN_LOG_LEVEL_ERROR:
29 log_level = QnnExecuTorchLogLevel::kLogLevelError;
30 break;
31 case QNN_LOG_LEVEL_WARN:
32 log_level = QnnExecuTorchLogLevel::kLogLevelWarn;
33 break;
34 default:
35 log_level = QnnExecuTorchLogLevel::kLogLevelInfo;
36 break;
37 }
38
39 char buffer[kLogBufferSize];
40 vsnprintf(buffer, kLogBufferSize, fmt, args);
41 QNN_EXECUTORCH_LOG(log_level, buffer);
42 }
QnnLogger(const QnnImplementation & implementation,QnnLog_Callback_t callback,QnnExecuTorchLogLevel log_level)43 QnnLogger::QnnLogger(
44 const QnnImplementation& implementation,
45 QnnLog_Callback_t callback,
46 QnnExecuTorchLogLevel log_level)
47 : handle_(nullptr), implementation_(implementation) {
48 const QnnInterface& qnn_interface = implementation.GetQnnInterface();
49
50 QnnLog_Level_t qnn_log_level = QNN_LOG_LEVEL_ERROR;
51 if (log_level > QnnExecuTorchLogLevel::kLogOff) {
52 switch (log_level) {
53 case QnnExecuTorchLogLevel::kLogLevelError:
54 qnn_log_level = QNN_LOG_LEVEL_ERROR;
55 break;
56 case QnnExecuTorchLogLevel::kLogLevelWarn:
57 qnn_log_level = QNN_LOG_LEVEL_WARN;
58 break;
59 case QnnExecuTorchLogLevel::kLogLevelInfo:
60 qnn_log_level = QNN_LOG_LEVEL_INFO;
61 break;
62 case QnnExecuTorchLogLevel::kLogLevelVerbose:
63 qnn_log_level = QNN_LOG_LEVEL_VERBOSE;
64 break;
65 case QnnExecuTorchLogLevel::kLogLevelDebug:
66 qnn_log_level = QNN_LOG_LEVEL_DEBUG;
67 break;
68 default:
69 QNN_EXECUTORCH_LOG_ERROR("Unknown logging level %d", log_level);
70 }
71 QNN_EXECUTORCH_LOG_INFO("create QNN Logger with log_level %d", log_level);
72 Qnn_ErrorHandle_t error =
73 qnn_interface.qnn_log_create(callback, qnn_log_level, &handle_);
74
75 if (error != QNN_SUCCESS) {
76 QNN_EXECUTORCH_LOG_WARN(
77 "Failed to create log_handle for backend "
78 " %u, qnn_log_level %d, error=%d",
79 qnn_interface.GetBackendId(),
80 qnn_log_level,
81 QNN_GET_ERROR_CODE(error));
82
83 // ignore error and continue to create backend handle...
84 handle_ = nullptr;
85 }
86 }
87 }
88
~QnnLogger()89 QnnLogger::~QnnLogger() {
90 const QnnInterface& qnn_interface = implementation_.GetQnnInterface();
91 if (handle_ != nullptr) {
92 Qnn_ErrorHandle_t error = qnn_interface.qnn_log_free(handle_);
93 if (error != QNN_SUCCESS) {
94 QNN_EXECUTORCH_LOG_ERROR(
95 "Failed to free QNN log_handle. Backend "
96 "ID %u, error %d",
97 qnn_interface.GetBackendId(),
98 QNN_GET_ERROR_CODE(error));
99 }
100 handle_ = nullptr;
101 }
102 }
103 } // namespace qnn
104 } // namespace backends
105 } // namespace executorch
106