1 /*
2 * Copyright (c) 2023 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/log.h"
17 #include "interface/native/log.h"
18
19 static int g_logLevel = LOG_LEVEL_MIN;
20
ConvertLogLevel(LogLevel level)21 static OHOS::Ace::LogLevel ConvertLogLevel(LogLevel level)
22 {
23 switch (level) {
24 case LogLevel::LOG_DEBUG:
25 return OHOS::Ace::LogLevel::DEBUG;
26 case LogLevel::LOG_INFO:
27 return OHOS::Ace::LogLevel::INFO;
28 case LogLevel::LOG_WARN:
29 return OHOS::Ace::LogLevel::WARN;
30 case LogLevel::LOG_ERROR:
31 return OHOS::Ace::LogLevel::ERROR;
32 case LogLevel::LOG_FATAL:
33 return OHOS::Ace::LogLevel::FATAL;
34 default:
35 return OHOS::Ace::LogLevel::DEBUG;
36 }
37 }
38
HiLogPrintArgs(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * fmt,va_list ap)39 int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
40 const char *fmt, va_list ap)
41 {
42 OHOS::HiviewDFX::Hilog::Platform::LogPrint(ConvertLogLevel(level), fmt, ap);
43 return 0;
44 }
45
HiLogPrint(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)46 int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
47 {
48 int ret;
49 va_list ap;
50 va_start(ap, fmt);
51 ret = HiLogPrintArgs(type, level, domain, tag, fmt, ap);
52 va_end(ap);
53 return ret;
54 }
55
HiLogSetAppMinLogLevel(LogLevel level)56 void HiLogSetAppMinLogLevel(LogLevel level)
57 {
58 g_logLevel = level;
59 }
60
HiLogIsLoggable(unsigned int domain,const char * tag,LogLevel level)61 bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level)
62 {
63 if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || (level < g_logLevel) || tag == nullptr) {
64 return false;
65 }
66
67 return true;
68 }
69