1 /**
2 * Copyright 2019-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "src/common/log.h"
17 #include <cstring>
18 #include <cstdio>
19
20 #ifdef ENABLE_ARM
21 #if defined(__ANDROID__) || defined(ANDROID)
22 #include <android/log.h>
23 #endif
24 #endif
25
26 // namespace to support utils module definition namespace mindspore constexpr const char *ANDROID_LOG_TAG = "MS_LITE";
27 namespace mindspore {
28 #if defined(__ANDROID__) || defined(ANDROID)
29 constexpr const char *ANDROID_LOG_TAG = "MS_LITE";
30 #endif
31
StrToInt(const char * env)32 int StrToInt(const char *env) {
33 if (env == nullptr) {
34 return 2;
35 }
36 if (strcmp(env, "0") == 0) {
37 return 0;
38 }
39 if (strcmp(env, "1") == 0) {
40 return 1;
41 }
42 if (strcmp(env, "2") == 0) {
43 return 2;
44 }
45 if (strcmp(env, "3") == 0) {
46 return 3;
47 }
48 return 2;
49 }
50
IsPrint(int level)51 bool IsPrint(int level) {
52 static const char *const env = std::getenv("GLOG_v");
53 static const int ms_level = StrToInt(env);
54 if (level < 0) {
55 level = 2;
56 }
57 return level >= ms_level;
58 }
59
60 #ifdef ENABLE_ARM
61 #if defined(__ANDROID__) || defined(ANDROID)
GetAndroidLogLevel(LiteLogLevel level)62 static int GetAndroidLogLevel(LiteLogLevel level) {
63 switch (level) {
64 case LiteLogLevel::DEBUG:
65 return ANDROID_LOG_DEBUG;
66 case LiteLogLevel::INFO:
67 return ANDROID_LOG_INFO;
68 case LiteLogLevel::WARNING:
69 return ANDROID_LOG_WARN;
70 case LiteLogLevel::ERROR:
71 default:
72 return ANDROID_LOG_ERROR;
73 }
74 }
75 #endif
76 #endif
77
78 #ifdef MS_COMPILE_OHOS
PrintHiLog(LiteLogLevel level,const char * file,int line,const char * func,const char * msg)79 void PrintHiLog(LiteLogLevel level, const char *file, int line, const char *func, const char *msg) {
80 if (level == LiteLogLevel::DEBUG) {
81 OHOS::HiviewDFX::HiLog::Debug(MSLite_LABEL, FORMAT, file, line, func, msg);
82 } else if (level == LiteLogLevel::INFO) {
83 OHOS::HiviewDFX::HiLog::Info(MSLite_LABEL, FORMAT, file, line, func, msg);
84 } else if (level == LiteLogLevel::WARNING) {
85 OHOS::HiviewDFX::HiLog::Warn(MSLite_LABEL, FORMAT, file, line, func, msg);
86 } else if (level == LiteLogLevel::ERROR) {
87 OHOS::HiviewDFX::HiLog::Error(MSLite_LABEL, FORMAT, file, line, func, msg);
88 }
89 }
90 #endif
91
EnumStrForMsLogLevel(LiteLogLevel level)92 const char *EnumStrForMsLogLevel(LiteLogLevel level) {
93 if (level == LiteLogLevel::DEBUG) {
94 return "DEBUG";
95 } else if (level == LiteLogLevel::INFO) {
96 return "INFO";
97 } else if (level == LiteLogLevel::WARNING) {
98 return "WARNING";
99 } else if (level == LiteLogLevel::ERROR) {
100 return "ERROR";
101 } else {
102 return "NO_LEVEL";
103 }
104 }
105
OutputLog(const std::ostringstream & msg) const106 void LiteLogWriter::OutputLog(const std::ostringstream &msg) const {
107 if (IsPrint(static_cast<int>(log_level_))) {
108 #if defined(ENABLE_ARM) && (defined(__ANDROID__) || defined(ANDROID))
109 __android_log_print(GetAndroidLogLevel(log_level_), ANDROID_LOG_TAG, "[%s:%d] %s] %s", location_.file_,
110 location_.line_, location_.func_, msg.str().c_str());
111 #elif defined(MS_COMPILE_OHOS)
112 PrintHiLog(log_level_, location_.file_, location_.line_, location_.func_, msg.str().c_str());
113 #else
114 printf("%s [%s:%d] %s] %s\n", EnumStrForMsLogLevel(log_level_), location_.file_, location_.line_, location_.func_,
115 msg.str().c_str());
116 #endif
117 }
118 }
119
operator <(const LiteLogStream & stream) const120 void LiteLogWriter::operator<(const LiteLogStream &stream) const noexcept {
121 std::ostringstream msg;
122 msg << stream.sstream_->rdbuf();
123 OutputLog(msg);
124 }
125 } // namespace mindspore
126