1 /*
2 * Copyright (C) 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 #include "log.h"
16 #ifdef HDC_HILOG
17 #include "hilog/log.h"
18 #endif
19
20 namespace Hdc {
PrintLogEx(const char * functionName,int line,uint8_t logLevel,const char * msg,...)21 void PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char *msg, ...)
22 {
23 char buf[BUF_SIZE_DEFAULT4] = { 0 }; // only 4k to avoid stack overflow in 32bit or L0
24 va_list vaArgs;
25 va_start(vaArgs, msg);
26 const int retSize = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, msg, vaArgs);
27 va_end(vaArgs);
28 if (retSize < 0) {
29 return;
30 }
31
32 #ifdef HDC_HILOG
33 string tmpPath = functionName;
34 string filePath = GetFileNameAny(tmpPath);
35 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0xD002D13, "HDC_LOG"};
36 switch (static_cast<int>(logLevel)) {
37 case static_cast<int>(LOG_DEBUG):
38 // Info level log can be printed default in hilog, debug can't
39 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
40 filePath.c_str(), line, buf);
41 break;
42 case static_cast<int>(LOG_INFO):
43 OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
44 filePath.c_str(), line, buf);
45 break;
46 case static_cast<int>(LOG_WARN):
47 OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
48 filePath.c_str(), line, buf);
49 break;
50 case static_cast<int>(LOG_FATAL):
51 OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, "[%{public}s:%{public}d] %{public}s",
52 filePath.c_str(), line, buf);
53 break;
54 default:
55 break;
56 }
57 #endif
58 }
59 }