1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*!
8  @addtogroup cc_pal_log
9  @{
10  */
11 
12 /*!
13  @file
14  @brief This file contains the PAL layer log definitions.
15 
16  The log is disabled by default.
17  */
18 
19 
20 #ifndef _CC_PAL_LOG_H_
21 #define _CC_PAL_LOG_H_
22 
23 #include "cc_pal_types.h"
24 #include "cc_pal_log_plat.h"
25 
26 /* PAL log levels (to be used in CC_PAL_logLevel) */
27 /*! PAL log level - disabled. */
28 #define CC_PAL_LOG_LEVEL_NULL      (-1)
29 /*! PAL log level - error. */
30 #define CC_PAL_LOG_LEVEL_ERR       0
31 /*! PAL log level - warning. */
32 #define CC_PAL_LOG_LEVEL_WARN      1
33 /*! PAL log level - info. */
34 #define CC_PAL_LOG_LEVEL_INFO      2
35 /*! PAL log level - debug. */
36 #define CC_PAL_LOG_LEVEL_DEBUG     3
37 /*! PAL log level - trace. */
38 #define CC_PAL_LOG_LEVEL_TRACE     4
39 /*! PAL log level - data. */
40 #define CC_PAL_LOG_LEVEL_DATA      5
41 
42 #ifndef CC_PAL_LOG_CUR_COMPONENT
43 /* Setting default component mask in case caller did not define */
44 /* (a mask that is always on for every log mask value but full masking) */
45 /*! Default log debugged component. */
46 #define CC_PAL_LOG_CUR_COMPONENT 0xFFFFFFFF
47 #endif
48 #ifndef CC_PAL_LOG_CUR_COMPONENT_NAME
49 /*! Default log debugged component. */
50 #define CC_PAL_LOG_CUR_COMPONENT_NAME "CC"
51 #endif
52 
53 /* Select compile time log level (default if not explicitly specified by caller) */
54 #ifndef CC_PAL_MAX_LOG_LEVEL /* Can be overriden by external definition of this constant */
55 #ifdef DEBUG
56 /*! Default debug log level, when debug is set to on. */
57 #define CC_PAL_MAX_LOG_LEVEL  CC_PAL_LOG_LEVEL_ERR /*CC_PAL_LOG_LEVEL_DEBUG*/
58 #else /* Disable logging */
59 /*! Default debug log level, when debug is set to off. */
60 #define CC_PAL_MAX_LOG_LEVEL CC_PAL_LOG_LEVEL_NULL
61 #endif
62 #endif /*CC_PAL_MAX_LOG_LEVEL*/
63 /*! Evaluate \p CC_PAL_MAX_LOG_LEVEL in case provided by caller. */
64 #define __CC_PAL_LOG_LEVEL_EVAL(level) level
65 /*! The maximal log-level definition. */
66 #define _CC_PAL_MAX_LOG_LEVEL __CC_PAL_LOG_LEVEL_EVAL(CC_PAL_MAX_LOG_LEVEL)
67 
68 
69 #ifdef __ARM_DS5__
70 #define inline __inline
71 #endif
72 
73 #ifdef ARM_DSM
74 /*! Log initialization function. */
75 #define CC_PalLogInit() do {} while (0)
76 /*! Log set-level function - sets the level of logging in case of debug. */
77 #define CC_PalLogLevelSet(setLevel) do {} while (0)
78 /*! Log set-mask function - sets the component-masking in case of debug. */
79 #define CC_PalLogMaskSet(setMask) do {} while (0)
80 #else
81 #if _CC_PAL_MAX_LOG_LEVEL > CC_PAL_LOG_LEVEL_NULL
82 /*! Log initialization function - platform dependent. */
83 void CC_PalLogInit(void);
84 /*! Log set-level function - sets the level of logging in case of debug. */
85 void CC_PalLogLevelSet(int setLevel);
86 /*! Log set-mask function - sets the component-masking in case of debug. */
87 void CC_PalLogMaskSet(uint32_t setMask);
88 /*! Global variable for log level. */
89 extern int CC_PAL_logLevel;
90 /*! Global variable for log mask. */
91 extern uint32_t CC_PAL_logMask;
92 #else /* No log - functions are not platform dependent in case of DEBUG=0*/
93 /*! Log initialization function. */
CC_PalLogInit(void)94 static inline void CC_PalLogInit(void) {}
95 /*! Log set-level function - sets the level of logging in case of debug. */
CC_PalLogLevelSet(int setLevel)96 static inline void CC_PalLogLevelSet(int setLevel) {CC_UNUSED_PARAM(setLevel);}
97 /*! Log set-mask function - sets the component-masking in case of debug. */
CC_PalLogMaskSet(uint32_t setMask)98 static inline void CC_PalLogMaskSet(uint32_t setMask) {CC_UNUSED_PARAM(setMask);}
99 #endif
100 #endif
101 
102 /*! Filter logging based on \p logMask, and dispatch to platform-specific
103 logging mechanism. */
104 #define _CC_PAL_LOG(level, format, ...)  \
105     if (CC_PAL_logMask & CC_PAL_LOG_CUR_COMPONENT) \
106         CC_PalLog(CC_PAL_LOG_LEVEL_ ## level, "%s:%s: " format "\n", CC_PAL_LOG_CUR_COMPONENT_NAME, __func__, ##__VA_ARGS__)
107 
108 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_ERR)
109 /*! Log messages according to log level.*/
110 #define CC_PAL_LOG_ERR(format, ... ) \
111     _CC_PAL_LOG(ERR, format, ##__VA_ARGS__)
112 #else
113 /*! Log messages according to log level.*/
114 #define CC_PAL_LOG_ERR( ... ) do {} while (0)
115 #endif
116 
117 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_WARN)
118 /*! Log messages according to log level.*/
119 #define CC_PAL_LOG_WARN(format, ... ) \
120     if (CC_PAL_logLevel >= CC_PAL_LOG_LEVEL_WARN) \
121         _CC_PAL_LOG(WARN, format, ##__VA_ARGS__)
122 #else
123 /*! Log messages according to log level.*/
124 #define CC_PAL_LOG_WARN( ... ) do {} while (0)
125 #endif
126 
127 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_INFO)
128 /*! Log messages according to log level.*/
129 #define CC_PAL_LOG_INFO(format, ... ) \
130     if (CC_PAL_logLevel >= CC_PAL_LOG_LEVEL_INFO) \
131         _CC_PAL_LOG(INFO, format, ##__VA_ARGS__)
132 #else
133 /*! Log messages according to log level.*/
134 #define CC_PAL_LOG_INFO( ... ) do {} while (0)
135 #endif
136 
137 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_DEBUG)
138 /*! Log messages according to log level.*/
139 #define CC_PAL_LOG_DEBUG(format, ... ) \
140     if (CC_PAL_logLevel >= CC_PAL_LOG_LEVEL_DEBUG) \
141         _CC_PAL_LOG(DEBUG, format, ##__VA_ARGS__)
142 
143 /*! Log message buffer.*/
144 #define CC_PAL_LOG_DUMP_BUF(msg, buf, size)     \
145     do {                        \
146     int i;                      \
147     uint8_t *pData = (uint8_t*)buf;         \
148                             \
149     PRINTF("%s (%d):\n", msg, size);        \
150     for (i = 0; i < size; i++) {            \
151         PRINTF("0x%02X ", pData[i]);        \
152         if ((i & 0xF) == 0xF) {         \
153             PRINTF("\n");           \
154         }                   \
155     }                       \
156     PRINTF("\n");                   \
157     } while (0)
158 #else
159 /*! Log debug messages.*/
160 #define CC_PAL_LOG_DEBUG( ... ) do {} while (0)
161 /*! Log debug buffer.*/
162 #define CC_PAL_LOG_DUMP_BUF(msg, buf, size) do {} while (0)
163 #endif
164 
165 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_TRACE)
166 /*! Log debug trace.*/
167 #define CC_PAL_LOG_TRACE(format, ... ) \
168     if (CC_PAL_logLevel >= CC_PAL_LOG_LEVEL_TRACE) \
169         _CC_PAL_LOG(TRACE, format, ##__VA_ARGS__)
170 #else
171 /*! Log debug trace.*/
172 #define CC_PAL_LOG_TRACE(...) do {} while (0)
173 #endif
174 
175 #if (_CC_PAL_MAX_LOG_LEVEL >= CC_PAL_LOG_LEVEL_TRACE)
176 /*! Log debug data.*/
177 #define CC_PAL_LOG_DATA(format, ...) \
178     if (CC_PAL_logLevel >= CC_PAL_LOG_LEVEL_TRACE) \
179         _CC_PAL_LOG(DATA, format, ##__VA_ARGS__)
180 #else
181 /*! Log debug data.*/
182 #define CC_PAL_LOG_DATA( ...) do {} while (0)
183 #endif
184 
185 /*!
186  @}
187  */
188 
189 #endif /*_CC_PAL_LOG_H_*/
190