1 /*
2 * Copyright (c) 2021 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 "base/log/log_wrapper.h"
17
18 #include <cstring>
19
20 #include "hilog/log.h"
21
22 #ifdef ACE_INSTANCE_LOG
23 #include "core/common/container.h"
24 #endif
25
26 extern "C" {
27 int HiLogPrintArgs(LogType type, LogLevel level, unsigned int domain, const char* tag, const char* fmt, va_list ap);
28 }
29
30 namespace OHOS::Ace {
31
32 namespace {
33
34 const ::LogLevel LOG_LEVELS[] = {
35 LOG_DEBUG,
36 LOG_INFO,
37 LOG_WARN,
38 LOG_ERROR,
39 LOG_FATAL,
40 };
41
42 const char* LOG_TAGS[] = {
43 "Ace",
44 "JSApp",
45 };
46
47 constexpr uint32_t LOG_DOMAINS[] = {
48 0xD003900,
49 0xD003B00,
50 };
51
52 constexpr LogType LOG_TYPES[] = {
53 LOG_CORE,
54 LOG_APP,
55 };
56
57 }
58
59 // initial static member object
60 LogLevel LogWrapper::level_ = LogLevel::DEBUG;
61
GetSeparatorCharacter()62 char LogWrapper::GetSeparatorCharacter()
63 {
64 return '/';
65 }
66
PrintLog(LogDomain domain,LogLevel level,const char * fmt,va_list args)67 void LogWrapper::PrintLog(LogDomain domain, LogLevel level, const char* fmt, va_list args)
68 {
69 #ifdef ACE_PRIVATE_LOG
70 std::string newFmt(fmt);
71 ReplaceFormatString("{private}", "{public}", newFmt);
72 HiLogPrintArgs(LOG_TYPES[static_cast<uint32_t>(domain)], LOG_LEVELS[static_cast<uint32_t>(level)],
73 LOG_DOMAINS[static_cast<uint32_t>(domain)], LOG_TAGS[static_cast<uint32_t>(domain)], newFmt.c_str(), args);
74 #else
75 HiLogPrintArgs(LOG_TYPES[static_cast<uint32_t>(domain)], LOG_LEVELS[static_cast<uint32_t>(level)],
76 LOG_DOMAINS[static_cast<uint32_t>(domain)], LOG_TAGS[static_cast<uint32_t>(domain)], fmt, args);
77 #endif
78 }
79
GetId()80 int32_t LogWrapper::GetId()
81 {
82 #ifdef ACE_INSTANCE_LOG
83 return Container::CurrentId();
84 #else
85 return 0;
86 #endif
87 }
88
89 } // namespace OHOS::Ace
90