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 <string>
17 #include <thread>
18 #include <unistd.h>
19
20 #include "netstack_log.h"
21
22 namespace OHOS::HiviewDFX {
23 static constexpr uint32_t MAX_BUFFER_SIZE = 4096;
24
StripFormatString(const std::string & prefix,std::string & str)25 static void StripFormatString(const std::string &prefix, std::string &str)
26 {
27 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
28 str.erase(pos, prefix.size());
29 }
30 }
31
32 #define PRINT_LOG(LEVEL) \
33 do { \
34 std::string newFmt(fmt); \
35 StripFormatString("{public}", newFmt); \
36 StripFormatString("{private}", newFmt); \
37 \
38 va_list args; \
39 va_start(args, fmt); \
40 printf(#LEVEL " %d %u ", getpid(), std::this_thread::get_id()); \
41 fflush(stdout); \
42 vfprintf(stdout, newFmt.c_str(), args); \
43 fflush(stdout); \
44 printf("\n"); \
45 fflush(stdout); \
46 va_end(args); \
47 } while (0)
48
Debug(const HiLogLabel & label,const char * fmt,...)49 int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...)
50 {
51 if (label.domain != NETSTACK_LOG_DOMAIN) {
52 return 0;
53 }
54 PRINT_LOG(Debug);
55 return 0;
56 }
Info(const HiLogLabel & label,const char * fmt,...)57 int HiLog::Info(const HiLogLabel &label, const char *fmt, ...)
58 {
59 if (label.domain != NETSTACK_LOG_DOMAIN) {
60 return 0;
61 }
62 PRINT_LOG(Info);
63 return 0;
64 }
Warn(const HiLogLabel & label,const char * fmt,...)65 int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...)
66 {
67 if (label.domain != NETSTACK_LOG_DOMAIN) {
68 return 0;
69 }
70 PRINT_LOG(Warn);
71 return 0;
72 }
Error(const HiLogLabel & label,const char * fmt,...)73 int HiLog::Error(const HiLogLabel &label, const char *fmt, ...)
74 {
75 if (label.domain != NETSTACK_LOG_DOMAIN) {
76 return 0;
77 }
78 PRINT_LOG(Error);
79 return 0;
80 }
Fatal(const HiLogLabel & label,const char * fmt,...)81 int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...)
82 {
83 if (label.domain != NETSTACK_LOG_DOMAIN) {
84 return 0;
85 }
86 PRINT_LOG(Fatal);
87 return 0;
88 }
89 } // namespace OHOS::HiviewDFX