• 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 #ifdef ANDROID_PLATFORM
42 const char* tag = "ArkCompiler";
PrintLog(LogLevel level,const char * fmt,...)43 void StdLog::PrintLog(LogLevel level, const char* fmt, ...)
44 {
45     std::string formatted = StripFormatString(fmt);
46     va_list args;
47     va_start(args, fmt);
48     __android_log_vprint(static_cast<int>(level), tag, formatted.c_str(), args);
49     va_end(args);
50 }
51 #else
52 constexpr int32_t MAX_BUFFER_SIZE = 100;
PrintLog(LogLevel level,const char * fmt,...)53 void StdLog::PrintLog(LogLevel level, const char* fmt, ...)
54 {
55     std::string formatted = StripFormatString(fmt);
56     va_list args;
57     va_start(args, fmt);
58 
59     char buf[MAX_BUFFER_SIZE];
60     if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, formatted.c_str(), args) < 0 && errno == EINVAL) {
61         return;
62     }
63     va_end(args);
64 
65     char timeBuf[MAX_BUFFER_SIZE];
66     const char* domainTag = "[ArkCompiler Debugger]";
67     std::string levelTag;
68     switch (level) {
69         case LogLevel::FATAL:
70             levelTag = "[FATAL]";
71             break;
72         case LogLevel::ERROR:
73             levelTag = "[ERROR]";
74             break;
75         case LogLevel::WARN:
76             levelTag = "[WARN]";
77             break;
78         case LogLevel::INFO:
79             levelTag = "[INFO]";
80             break;
81         case LogLevel::DEBUG:
82             levelTag = "[DEBUG]";
83             break;
84         default:
85             levelTag = "[DEFAULT]";
86             break;
87     }
88 
89     if (snprintf_s(timeBuf, sizeof(timeBuf), sizeof(timeBuf) - 1, "%s %s %d",
90             domainTag, levelTag.c_str(), std::this_thread::get_id()) < 0) {
91         return;
92     }
93 
94     printf("%s %s\r\n", timeBuf, buf);
95 }
96 #endif
97 } // namespace OHOS::ArkCompiler::Toolchain