1 /*
2 * Copyright (C) 2021-2022 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 "rpc_log.h"
17
18 #include <stdint.h>
19 #include "securec.h"
20
21 #define LOG_NAME_MAX_LEN 5
22 #define LOG_PRINT_MAX_LEN 256
23
24 typedef struct {
25 RpcLogModule mod;
26 char name[LOG_NAME_MAX_LEN];
27 } LogInfo;
28
29 static LogInfo g_logInfo[RPC_LOG_MODULE_MAX] = {
30 {RPC_LOG_IPC, "IPC"},
31 {RPC_LOG_RPC, "RPC"},
32 {RPC_LOG_SER, "SER"},
33 };
34
RpcOutPrint(const char * buf,RpcLogLevel level)35 static void RpcOutPrint(const char *buf, RpcLogLevel level)
36 {
37 #ifdef IPCRPC_PRINTF
38 printf("%s\n", buf);
39 return;
40 #endif
41 switch (level) {
42 case RPC_LOG_DBG:
43 HILOG_DEBUG(RPC_HILOG_ID, "%{public}s", buf);
44 break;
45 case RPC_LOG_INFO:
46 HILOG_INFO(RPC_HILOG_ID, "%{public}s", buf);
47 break;
48 case RPC_LOG_WARN:
49 HILOG_WARN(RPC_HILOG_ID, "%{public}s", buf);
50 break;
51 case RPC_LOG_ERROR:
52 HILOG_ERROR(RPC_HILOG_ID, "%{public}s", buf);
53 break;
54 default:
55 break;
56 }
57 }
58
RpcLog(RpcLogModule module,RpcLogLevel level,const char * fmt,...)59 void RpcLog(RpcLogModule module, RpcLogLevel level, const char *fmt, ...)
60 {
61 int32_t ulPos;
62 char szStr[LOG_PRINT_MAX_LEN] = {0};
63 va_list arg;
64 int32_t ret;
65
66 if (module >= RPC_LOG_MODULE_MAX || level >= RPC_LOG_LEVEL_MAX) {
67 HILOG_ERROR(RPC_HILOG_ID, "rpc log type or module error");
68 return;
69 }
70
71 ret = sprintf_s(szStr, sizeof(szStr), "[%s]", g_logInfo[module].name);
72 if (ret < 0) {
73 HILOG_ERROR(RPC_HILOG_ID, "rpc log error");
74 return;
75 }
76 ulPos = strlen(szStr);
77 (void)memset_s(&arg, sizeof(va_list), 0, sizeof(va_list));
78 va_start(arg, fmt);
79 ret = vsprintf_s(&szStr[ulPos], sizeof(szStr) - ulPos, fmt, arg);
80 va_end(arg);
81 if (ret < 0) {
82 HILOG_ERROR(RPC_HILOG_ID, "rpc log len error");
83 return;
84 }
85 RpcOutPrint(szStr, level);
86
87 return;
88 }
89