1 /*
2 * Copyright (c) 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 #ifndef DFX_MUSL_LOG_H
16 #define DFX_MUSL_LOG_H
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 // Log type
23 typedef enum {
24 LOG_TYPE_MIN = 0,
25 LOG_APP = 0,
26 // Log to kmsg, only used by init phase.
27 LOG_INIT = 1,
28 // Used by core service, framework.
29 LOG_CORE = 3,
30 LOG_KMSG = 4,
31 LOG_TYPE_MAX
32 } LogType;
33
34 // Log level
35 typedef enum {
36 LOG_LEVEL_MIN = 0,
37 LOG_DEBUG = 3,
38 LOG_INFO = 4,
39 LOG_WARN = 5,
40 LOG_ERROR = 6,
41 LOG_FATAL = 7,
42 LOG_LEVEL_MAX,
43 } LogLevel;
44
45 #ifndef LOG_DOMAIN
46 #define LOG_DOMAIN 0xD002D11
47 #endif
48
49 #ifndef LOG_TAG
50 #define LOG_TAG "DfxFaultLogger"
51 #endif
52
53 #ifdef ENABLE_SIGHAND_MUSL_LOG
54 #define BUF_LENGTH 1024
55 extern int HiLogAdapterPrintArgs(
56 const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap);
57 extern int vsnprintfp_s(char *strDest, size_t destMax, size_t count, int priv, const char *format, va_list arglist);
58
MuslHiLogPrinter(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)59 __attribute__ ((visibility("hidden"))) int MuslHiLogPrinter(
60 LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
61 {
62 int ret;
63 va_list ap;
64 va_start(ap, fmt);
65 ret = HiLogAdapterPrintArgs(type, level, domain, tag, fmt, ap);
66 va_end(ap);
67 return ret;
68 }
69
DfxLog(const LogLevel logLevel,const unsigned int domain,const char * tag,const char * fmt,...)70 __attribute__ ((visibility("hidden"))) int DfxLog(
71 const LogLevel logLevel, const unsigned int domain, const char* tag, const char *fmt, ...)
72 {
73 int ret;
74 char buf[BUF_LENGTH] = {0};
75 va_list args;
76 va_start(args, fmt);
77 ret = vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, fmt, args);
78 va_end(args);
79 MuslHiLogPrinter(LOG_CORE, logLevel, domain, tag, "%{public}s", buf);
80 return ret;
81 }
82
83 #define DfxLogDebug(...) DfxLog(LOG_DEBUG, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)
84 #define DfxLogInfo(...) DfxLog(LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)
85 #define DfxLogWarn(...) DfxLog(LOG_WARN, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)
86 #define DfxLogError(...) DfxLog(LOG_ERROR, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)
87 #define DfxLogFatal(...) DfxLog(LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)
88 #else
89 #define DfxLogDebug(...)
90 #define DfxLogInfo(...)
91 #define DfxLogWarn(...)
92 #define DfxLogError(...)
93 #define DfxLogFatal(...)
94 #endif
95
96 #ifdef __cplusplus
97 }
98 #endif
99 #endif