Support M
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos5.git] / mobicore / common / LogWrapper / log.h
1 /** Log wrapper for Android.
2 * @{
3 * @file
4 *
5 * Maps LOG_*() macros to __android_log_print() if LOG_ANDROID is defined.
6 * Adds some extra info to log output like LOG_TAG, file name and line number.
7 *
8 * <!-- Copyright Giesecke & Devrient GmbH 2010 - 2011 -->
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #ifndef TLCWRAPPERANDROIDLOG_H_
35 #define TLCWRAPPERANDROIDLOG_H_
36
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <android/log.h>
41
42 /** LOG_I(fmt, args...)
43 * Informative logging, only shown in debug version
44 */
45
46 /** LOG_W(fmt, args...)
47 * Warnings logging, only shown in debug version
48 */
49
50 /** LOG_E(fmt, args...)
51 * Error logging, shown in debug and release version
52 */
53
54 /** LOG_V(fmt, args...)
55 * Verbose logging, shown in debug version if the including file defines LOG_VERBOSE
56 */
57
58 /** LOG_I_BUF(szDescriptor, blob, sizeOfBlob)
59 * Binary logging, line-wise output to LOG_I
60 */
61
62 #define EOL "\n"
63 #define DUMMY_FUNCTION() do{}while(0)
64
65 #ifdef LOG_ANDROID
66 // log to adb logcat
67 #ifdef NDEBUG // no logging in debug version
68 #define LOG_I(fmt, args...) DUMMY_FUNCTION()
69 #define LOG_W(fmt, args...) DUMMY_FUNCTION()
70 #else
71 // add LINE
72 #define LOG_I(fmt, args...) LOG_i(fmt";%d", ## args, __LINE__)
73 #define LOG_W(fmt, args...) LOG_w(fmt";%d", ## args, __LINE__)
74 #endif
75 // LOG_E is always defined
76 #define _LOG_E(fmt, args...) LOG_e(fmt, ## args)
77
78 // actually mapping to log system, adding level and tag.
79 #define LOG_i(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
80 #define LOG_w(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
81 #define LOG_e(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
82
83 #else //!defined(LOG_ANDROID)
84 // log to std.out using printf
85
86 // #level / #LOG_TAG ( process_id): __VA_ARGS__
87 // Example:
88 // I/McDrvBasicTest_0_1( 4075): setUp
89 #define _LOG_x(_x_,...) \
90 do \
91 { \
92 printf("%s/%s(%d): ",_x_,LOG_TAG,getpid()); \
93 printf(__VA_ARGS__); \
94 printf(EOL); \
95 } while(1!=1)
96
97
98 #ifdef NDEBUG // no logging in debug version
99 #define LOG_I(fmt, args...) DUMMY_FUNCTION()
100 #define LOG_W(fmt, args...) DUMMY_FUNCTION()
101 #else
102 #define LOG_I(...) _LOG_x("I", __VA_ARGS__)
103 #define LOG_W(...) _LOG_x("W", __VA_ARGS__)
104 #endif
105 #define _LOG_E(...) _LOG_x("E", __VA_ARGS__)
106
107 #endif //defined(LOG_ANDROID)
108
109 #if defined(LOG_VERBOSE)
110 #define LOG_V LOG_I
111 #else
112 #define LOG_V(...) DUMMY_FUNCTION()
113 #endif
114
115 /** LOG_E() needs to be more prominent:
116 * Display "*********** ERROR ***********" before actual error message.
117 */
118 #define LOG_E(...) \
119 do \
120 { \
121 _LOG_E(" *****************************"); \
122 _LOG_E(" *** ERROR: " __VA_ARGS__); \
123 _LOG_E(" *** Detected in %s:%i/%s()", __FILE__, __LINE__, __FUNCTION__); \
124 _LOG_E(" *****************************"); \
125 } while(1!=1)
126
127 #define LOG_ERRNO(MESSAGE) \
128 LOG_E("%s failed with \"%s\"(errno %i)", MESSAGE, strerror(errno), errno);
129
130 #define LOG_I_BUF LOG_I_Buf
131
132 __attribute__ ((unused))
133 static void LOG_I_Buf(
134 const char * szDescriptor,
135 const void * blob,
136 size_t sizeOfBlob
137 ) {
138
139 #define CPL 0x10 // chars per line
140 #define OVERHEAD 20
141
142 char buffer[CPL * 4 + OVERHEAD];
143
144 uint32_t index = 0;
145
146 uint32_t moreThanOneLine = (sizeOfBlob > CPL);
147 uint32_t blockLen = CPL;
148 uint32_t addr = 0;
149 uint32_t i = 0;
150
151 if (NULL != szDescriptor)
152 {
153 index += sprintf(&buffer[index], "%s", szDescriptor);
154 }
155
156 if (moreThanOneLine)
157 {
158 if (NULL == szDescriptor)
159 {
160 index += sprintf(&buffer[index], "memory dump");
161 }
162 index += sprintf(&buffer[index], " (0x%08x, %d bytes)", (uint32_t)blob,sizeOfBlob);
163 LOG_I("%s", buffer);
164 index = 0;
165 }
166 else if (NULL == szDescriptor)
167 {
168 index += sprintf(&buffer[index], "Data at 0x%08x: ", (uint32_t)blob);
169 }
170
171 if(sizeOfBlob == 0) {
172 LOG_I("%s", buffer);
173 }
174 else
175 {
176 while (sizeOfBlob > 0)
177 {
178 if (sizeOfBlob < blockLen)
179 {
180 blockLen = sizeOfBlob;
181 }
182
183 // address
184 if (moreThanOneLine)
185 {
186 index += sprintf(&buffer[index], "0x%08X | ",addr);
187 addr += CPL;
188 }
189 // bytes as hex
190 for (i=0; i<blockLen; ++i)
191 {
192 index += sprintf(&buffer[index], "%02x ", ((const char *)blob)[i] );
193 }
194 // spaces if necessary
195 if ((blockLen < CPL) && (moreThanOneLine))
196 {
197 // add spaces
198 for (i=0; i<(3*(CPL-blockLen)); ++i) {
199 index += sprintf(&buffer[index], " ");
200 }
201 }
202 // bytes as ASCII
203 index += sprintf(&buffer[index], "| ");
204 for (i=0; i<blockLen; ++i)
205 {
206 char c = ((const char *)blob)[i];
207 index += sprintf(&buffer[index], "%c",(c>32)?c:'.');
208 }
209
210 blob = &(((const char *)blob)[blockLen]);
211 sizeOfBlob -= blockLen;
212
213 // print line to logcat / stdout
214 LOG_I("%s", buffer);
215 index = 0;
216 }
217 }
218 }
219
220 #endif /** TLCWRAPPERANDROIDLOG_H_ */
221
222 /** @} */