• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "log_wrapper.h"
17 
18 #include <string>
19 #ifdef ANDROID_PLATFORM
20 #include <android/log.h>
21 #else
22 #include <thread>
23 #include "securec.h"
24 #endif
25 
26 namespace OHOS::ArkCompiler::Toolchain {
StripString(const std::string & prefix,std::string & str)27 void StripString(const std::string& prefix, std::string& str)
28 {
29     for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
30         str.erase(pos, prefix.size());
31     }
32 }
33 
StripFormatString(const char * fmt)34 std::string StripFormatString(const char* fmt)
35 {
36     std::string newFmt(fmt);
37     StripString("{public}", newFmt);
38     StripString("{private}", newFmt);
39     return newFmt;
40 }
41 
42 #ifdef ANDROID_PLATFORM
43 const char* tag = "ArkCompiler";
PrintLog(LogLevel level,const char * fmt,...)44 void StdLog::PrintLog(LogLevel level, const char* fmt, ...)
45 {
46     std::string formatted = StripFormatString(fmt);
47     va_list args;
48     va_start(args, fmt);
49     __android_log_vprint(static_cast<int>(level), tag, formatted.c_str(), args);
50     va_end(args);
51 }
52 #else
53 #ifndef ENABLE_HILOG
54 constexpr int32_t MAX_BUFFER_SIZE = 100;
PrintLog(LogLevel level,const char * fmt,...)55 void StdLog::PrintLog(LogLevel level, const char* fmt, ...)
56 {
57     std::string formatted = StripFormatString(fmt);
58     va_list args;
59     va_start(args, fmt);
60 
61     char buf[MAX_BUFFER_SIZE];
62     if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, formatted.c_str(), args) < 0 && errno == EINVAL) {
63         return;
64     }
65     va_end(args);
66 
67     char timeBuf[MAX_BUFFER_SIZE];
68     const char* domainTag = "[ArkCompiler Debugger]";
69     std::string levelTag;
70     switch (level) {
71         case LogLevel::FATAL:
72             levelTag = "[FATAL]";
73             break;
74         case LogLevel::ERROR:
75             levelTag = "[ERROR]";
76             break;
77         case LogLevel::WARN:
78             levelTag = "[WARN]";
79             break;
80         case LogLevel::INFO:
81             levelTag = "[INFO]";
82             break;
83         case LogLevel::DEBUG:
84             levelTag = "[DEBUG]";
85             break;
86         default:
87             levelTag = "[DEFAULT]";
88             break;
89     }
90 
91     if (snprintf_s(timeBuf, sizeof(timeBuf), sizeof(timeBuf) - 1, "%s %s %d",
92             domainTag, levelTag.c_str(), std::this_thread::get_id()) < 0) {
93         return;
94     }
95 
96     printf("%s %s\r\n", timeBuf, buf);
97 }
98 #endif
99 #endif
100 } // namespace OHOS::ArkCompiler::Toolchain