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 "rd_log_print.h"
17 
18 #include "hilog/log.h"
19 #include "securec.h"
20 
21 namespace DocumentDB {
22 namespace {
PrintLog(LogPrint::Level level,const char * tag,const std::string & msg)23 void PrintLog(LogPrint::Level level, const char *tag, const std::string &msg)
24 {
25     if (msg.empty()) {
26         return;
27     }
28 #ifdef DB_DEBUG_ENV
29 #define FORMAT "%s"
30 #else
31 #define FORMAT "%{public}s"
32 #endif
33     OHOS::HiviewDFX::HiLogLabel label = { LOG_CORE, 0xD001631, tag }; // 0xD001631 is identity of the log
34     switch (level) {
35         case LogPrint::Level::LEVEL_DEBUG:
36             (void)HILOG_IMPL(label.type, LOG_DEBUG, label.domain, label.tag, FORMAT, msg.c_str());
37             break;
38         case LogPrint::Level::LEVEL_INFO:
39             (void)HILOG_IMPL(label.type, LOG_INFO, label.domain, label.tag, FORMAT, msg.c_str());
40             break;
41         case LogPrint::Level::LEVEL_WARN:
42             (void)HILOG_IMPL(label.type, LOG_WARN, label.domain, label.tag, FORMAT, msg.c_str());
43             break;
44         case LogPrint::Level::LEVEL_ERROR:
45             (void)HILOG_IMPL(label.type, LOG_ERROR, label.domain, label.tag, FORMAT, msg.c_str());
46             break;
47         case LogPrint::Level::LEVEL_FATAL:
48             (void)HILOG_IMPL(label.type, LOG_FATAL, label.domain, label.tag, FORMAT, msg.c_str());
49             break;
50         default:
51             break;
52     }
53 }
54 
PreparePrivateLog(const char * format,std::string & outStrFormat)55 void PreparePrivateLog(const char *format, std::string &outStrFormat)
56 {
57     static const std::string PRIVATE_TAG = "s{private}";
58     outStrFormat = format;
59     std::string::size_type pos = outStrFormat.find(PRIVATE_TAG);
60     if (pos != std::string::npos) {
61         outStrFormat.replace(pos, PRIVATE_TAG.size(), ".3s");
62     }
63 }
64 } // namespace
65 
Log(Level level,const char * tag,const char * format,...)66 void LogPrint::Log(Level level, const char *tag, const char *format, ...)
67 {
68     static const int maxLogLength = 1024;
69 
70     va_list argList;
71     va_start(argList, format);
72     char logBuff[maxLogLength];
73     std::string msg;
74     std::string formatTemp;
75     PreparePrivateLog(format, formatTemp);
76     int bytes = vsnprintf_s(logBuff, maxLogLength, maxLogLength - 1, formatTemp.c_str(), argList);
77     if (bytes < 0) {
78         msg = "log buffer overflow!";
79     } else {
80         msg = logBuff;
81     }
82     va_end(argList);
83 
84     PrintLog(level, tag, msg);
85 }
86 } // namespace DocumentDB
87