1 /*
2  * Copyright (c) 2023-2024 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 "hilog_adapter.h"
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include "hilog/log.h"
20 
21 namespace OHOS::NWeb {
22 namespace {
23 constexpr uint32_t BROWSER_UID_BASE = 20000000;
24 constexpr uint32_t LOG_APP_DOMAIN = 0xD004500;
25 constexpr uint32_t LOG_RENDER_DOMAIN = 0xD004501;
26 constexpr uint32_t LOG_CONSOLE_DOMAIN = 0x001194;
27 
28 const ::LogLevel LOG_LEVELS[] = {
29     LOG_DEBUG,
30     LOG_INFO,
31     LOG_WARN,
32     LOG_ERROR,
33     LOG_FATAL,
34     LOG_LEVEL_MAX,
35 };
36 } // namespace
37 
38 extern "C" {
39 int HiLogPrintArgs(LogType type, LogLevel level, unsigned int domain, const char* tag, const char* fmt, va_list ap);
40 
HiLogAdapterPrintLog(uint32_t level,const char * tag,const char * fmt,va_list ap)41 int HiLogAdapterPrintLog(uint32_t level, const char* tag, const char* fmt, va_list ap)
42 {
43     uint32_t domain = LOG_RENDER_DOMAIN;
44     if ((getuid() / BROWSER_UID_BASE) != 0) {
45         domain = LOG_APP_DOMAIN;
46     }
47     return HiLogPrintArgs(LOG_CORE, LOG_LEVELS[level], domain, tag, fmt, ap);
48 }
49 
HiLogAdapterConsoleLog(uint32_t level,const char * tag,const char * fmt,va_list ap)50 int HiLogAdapterConsoleLog(uint32_t level, const char* tag, const char* fmt, va_list ap)
51 {
52     return HiLogPrintArgs(LOG_APP, LOG_LEVELS[level], LOG_CONSOLE_DOMAIN, tag, fmt, ap);
53 }
54 }
55 
PrintLog(LogLevelAdapter level,const char * tag,const char * fmt,...)56 int HiLogAdapter::PrintLog(LogLevelAdapter level, const char* tag, const char* fmt, ...)
57 {
58     int ret;
59     va_list ap;
60     va_start(ap, fmt);
61     ret = HiLogAdapterPrintLog(static_cast<uint32_t>(level), tag, fmt, ap);
62     va_end(ap);
63     return ret;
64 }
65 
PrintConsoleLog(LogLevelAdapter level,const char * tag,const char * fmt,...)66 int HiLogAdapter::PrintConsoleLog(LogLevelAdapter level, const char* tag, const char* fmt, ...)
67 {
68     int ret;
69     va_list ap;
70     va_start(ap, fmt);
71     ret = HiLogAdapterConsoleLog(static_cast<uint32_t>(level), tag, fmt, ap);
72     va_end(ap);
73     return ret;
74 }
75 } // namespace OHOS::NWeb
76