• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef DFX_MUSL_LOG_H
16 #define DFX_MUSL_LOG_H
17 
18 #include "dfx_log_define.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #ifdef ENABLE_SIGHAND_MUSL_LOG
25 
26 /* Log type */
27 typedef enum {
28     /* min log type */
29     LOG_TYPE_MIN = 0,
30     /* Used by app log. */
31     LOG_APP = 0,
32     /* Log to kmsg, only used by init phase. */
33     LOG_INIT = 1,
34     /* Used by core service, framework. */
35     LOG_CORE = 3,
36     /* Used by kmsg log. */
37     LOG_KMSG = 4,
38     /* max log type */
39     LOG_TYPE_MAX
40 } LogType;
41 
42 /* Log level */
43 typedef enum {
44     /* min log level */
45     LOG_LEVEL_MIN = 0,
46     /* Designates lower priority log. */
47     LOG_DEBUG = 3,
48     /* Designates useful information. */
49     LOG_INFO = 4,
50     /* Designates hazardous situations. */
51     LOG_WARN = 5,
52     /* Designates very serious errors. */
53     LOG_ERROR = 6,
54     /* Designates major fatal anomaly. */
55     LOG_FATAL = 7,
56     /* max log level */
57     LOG_LEVEL_MAX,
58 } LogLevel;
59 
60 extern int HiLogAdapterPrintArgs(
61     const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap);
62 extern int vsnprintfp_s(char *strDest, size_t destMax, size_t count, int priv, const char *format, va_list arglist);
63 
MuslHiLogPrinter(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)64 __attribute__ ((visibility("hidden"))) int MuslHiLogPrinter(
65     LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
66 {
67     int ret;
68     va_list ap;
69     va_start(ap, fmt);
70     ret = HiLogAdapterPrintArgs(type, level, domain, tag, fmt, ap);
71     va_end(ap);
72     return ret;
73 }
74 
DfxLogPrint(const LogLevel logLevel,const unsigned int domain,const char * tag,const char * fmt,...)75 __attribute__ ((visibility("hidden"))) int DfxLogPrint(
76     const LogLevel logLevel, const unsigned int domain, const char* tag, const char *fmt, ...)
77 {
78     int ret;
79     char buf[LOG_BUF_LEN] = {0};
80     va_list args;
81     va_start(args, fmt);
82     ret = vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, fmt, args);
83     va_end(args);
84     MuslHiLogPrinter(LOG_CORE, logLevel, domain, tag, "%{public}s", buf);
85     return ret;
86 }
87 
88 #define DFXLOG_PRINT(prio, domain, tag, ...) DfxLogPrint(prio, domain, tag, ##__VA_ARGS__)
89 
90 #define DFXLOG_DEBUG(...) DFXLOG_PRINT(LOG_DEBUG, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
91 #define DFXLOG_INFO(...) DFXLOG_PRINT(LOG_INFO, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
92 #define DFXLOG_WARN(...) DFXLOG_PRINT(LOG_WARN, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
93 #define DFXLOG_ERROR(...) DFXLOG_PRINT(LOG_ERROR, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
94 #define DFXLOG_FATAL(...) DFXLOG_PRINT(LOG_FATAL, LOG_DOMAIN, LOG_TAG, ##__VA_ARGS__)
95 
96 #else
97 
98 #define DFXLOG_PRINT(prio, domain, tag, ...)
99 
100 #define DFXLOG_DEBUG(...)
101 #define DFXLOG_INFO(...)
102 #define DFXLOG_WARN(...)
103 #define DFXLOG_ERROR(...)
104 #define DFXLOG_FATAL(...)
105 #endif
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 #endif