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 "dfx/log/log_base.h"
17 #include <securec.h>
18 #include <string>
19 #include "hilog/log.h"
20
21 static constexpr unsigned int FFRT_LOG_DOMAIN = 0xD001719;
22 static constexpr char FFRT_LOG_TAG[] = "ffrt";
23
StringReplace(std::string & str,const std::string & oldStr,const std::string & newStr)24 inline void StringReplace(std::string& str, const std::string& oldStr, const std::string& newStr)
25 {
26 std::string::size_type pos = 0u;
27 while ((pos = str.find(oldStr, pos)) != std::string::npos) {
28 str.replace(pos, oldStr.length(), newStr);
29 pos += newStr.length();
30 }
31 }
PrintLogString(LogLevel logLevel,const char * fmt,va_list arg)32 static inline void PrintLogString(LogLevel logLevel, const char* fmt, va_list arg)
33 {
34 char buf[1024] = {0};
35 std::string format(fmt);
36 StringReplace(format, "%{public}", "%");
37 if (vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format.c_str(), arg) > 0) {
38 HiLogPrint(LOG_CORE, logLevel, FFRT_LOG_DOMAIN, FFRT_LOG_TAG, "%{public}s", buf);
39 }
40 }
41
LogErr(const char * format,...)42 void LogErr(const char* format, ...)
43 {
44 va_list args;
45 va_start(args, format);
46 PrintLogString(LOG_ERROR, format, args);
47 va_end(args);
48 }
49
LogWarn(const char * format,...)50 void LogWarn(const char* format, ...)
51 {
52 va_list args;
53 va_start(args, format);
54 PrintLogString(LOG_WARN, format, args);
55 va_end(args);
56 }
57
LogInfo(const char * format,...)58 void LogInfo(const char* format, ...)
59 {
60 va_list args;
61 va_start(args, format);
62 PrintLogString(LOG_INFO, format, args);
63 va_end(args);
64 }
65
LogDebug(const char * format,...)66 void LogDebug(const char* format, ...)
67 {
68 va_list args;
69 va_start(args, format);
70 PrintLogString(LOG_DEBUG, format, args);
71 va_end(args);
72 }