1 /** Log wrapper for Android.
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.
8 * <!-- Copyright Giesecke & Devrient GmbH 2010 - 2011 -->
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
34 #ifndef TLCWRAPPERANDROIDLOG_H_
35 #define TLCWRAPPERANDROIDLOG_H_
40 #include <android/log.h>
42 /** LOG_I(fmt, args...)
43 * Informative logging, only shown in debug version
46 /** LOG_W(fmt, args...)
47 * Warnings logging, only shown in debug version
50 /** LOG_E(fmt, args...)
51 * Error logging, shown in debug and release version
54 /** LOG_V(fmt, args...)
55 * Verbose logging, shown in debug version if the including file defines LOG_VERBOSE
58 /** LOG_I_BUF(szDescriptor, blob, sizeOfBlob)
59 * Binary logging, line-wise output to LOG_I
63 #define DUMMY_FUNCTION() do{}while(0)
67 #ifdef NDEBUG // no logging in debug version
68 #define LOG_I(fmt, args...) DUMMY_FUNCTION()
69 #define LOG_W(fmt, args...) DUMMY_FUNCTION()
72 #define LOG_I(fmt, args...) LOG_i(fmt";%d", ## args, __LINE__)
73 #define LOG_W(fmt, args...) LOG_w(fmt";%d", ## args, __LINE__)
75 // LOG_E is always defined
76 #define _LOG_E(fmt, args...) LOG_e(fmt, ## args)
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__)
83 #else //!defined(LOG_ANDROID)
84 // log to std.out using printf
86 // #level / #LOG_TAG ( process_id): __VA_ARGS__
88 // I/McDrvBasicTest_0_1( 4075): setUp
89 #define _LOG_x(_x_,...) \
92 printf("%s/%s(%d): ",_x_,LOG_TAG,getpid()); \
93 printf(__VA_ARGS__); \
98 #ifdef NDEBUG // no logging in debug version
99 #define LOG_I(fmt, args...) DUMMY_FUNCTION()
100 #define LOG_W(fmt, args...) DUMMY_FUNCTION()
102 #define LOG_I(...) _LOG_x("I", __VA_ARGS__)
103 #define LOG_W(...) _LOG_x("W", __VA_ARGS__)
105 #define _LOG_E(...) _LOG_x("E", __VA_ARGS__)
107 #endif //defined(LOG_ANDROID)
109 #if defined(LOG_VERBOSE)
112 #define LOG_V(...) DUMMY_FUNCTION()
115 /** LOG_E() needs to be more prominent:
116 * Display "*********** ERROR ***********" before actual error message.
121 _LOG_E(" *****************************"); \
122 _LOG_E(" *** ERROR: " __VA_ARGS__); \
123 _LOG_E(" *** Detected in %s:%i/%s()", __FILE__, __LINE__, __FUNCTION__); \
124 _LOG_E(" *****************************"); \
127 #define LOG_ERRNO(MESSAGE) \
128 LOG_E("%s failed with \"%s\"(errno %i)", MESSAGE, strerror(errno), errno);
130 #define LOG_I_BUF LOG_I_Buf
132 __attribute__ ((unused
))
133 static void LOG_I_Buf(
134 const char * szDescriptor
,
139 #define CPL 0x10 // chars per line
142 char buffer
[CPL
* 4 + OVERHEAD
];
146 uint32_t moreThanOneLine
= (sizeOfBlob
> CPL
);
147 uint32_t blockLen
= CPL
;
151 if (NULL
!= szDescriptor
)
153 index
+= sprintf(&buffer
[index
], "%s", szDescriptor
);
158 if (NULL
== szDescriptor
)
160 index
+= sprintf(&buffer
[index
], "memory dump");
162 index
+= sprintf(&buffer
[index
], " (0x%08x, %d bytes)", (uint32_t)blob
,sizeOfBlob
);
166 else if (NULL
== szDescriptor
)
168 index
+= sprintf(&buffer
[index
], "Data at 0x%08x: ", (uint32_t)blob
);
171 if(sizeOfBlob
== 0) {
176 while (sizeOfBlob
> 0)
178 if (sizeOfBlob
< blockLen
)
180 blockLen
= sizeOfBlob
;
186 index
+= sprintf(&buffer
[index
], "0x%08X | ",addr
);
190 for (i
=0; i
<blockLen
; ++i
)
192 index
+= sprintf(&buffer
[index
], "%02x ", ((const char *)blob
)[i
] );
194 // spaces if necessary
195 if ((blockLen
< CPL
) && (moreThanOneLine
))
198 for (i
=0; i
<(3*(CPL
-blockLen
)); ++i
) {
199 index
+= sprintf(&buffer
[index
], " ");
203 index
+= sprintf(&buffer
[index
], "| ");
204 for (i
=0; i
<blockLen
; ++i
)
206 char c
= ((const char *)blob
)[i
];
207 index
+= sprintf(&buffer
[index
], "%c",(c
>32)?c
:'.');
210 blob
= &(((const char *)blob
)[blockLen
]);
211 sizeOfBlob
-= blockLen
;
213 // print line to logcat / stdout
220 #endif /** TLCWRAPPERANDROIDLOG_H_ */