summaryrefslogtreecommitdiff
path: root/mobicore/common/LogWrapper/log.h
blob: 78c9f948fb3ce17045f2943a5b6fddf853c694e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/** Log wrapper for Android.
 * @{
 * @file
 *
 * Maps LOG_*() macros to __android_log_print() if LOG_ANDROID is defined.
 * Adds some extra info to log output like LOG_TAG, file name and line number.
 *
 * <!-- Copyright Trustonic 2012-2013 -->
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef TLCWRAPPERANDROIDLOG_H_
#define TLCWRAPPERANDROIDLOG_H_

#include <errno.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#define GETPID getpid
#else
#include <process.h>
#define GETPID _getpid
#endif
#include <stdio.h>
#ifndef WIN32
#include <android/log.h>
#endif

/** LOG_I(fmt, args...)
 * Informative logging, only shown in debug version
 */

/** LOG_W(fmt, args...)
 * Warnings logging, only shown in debug version
 */

/** LOG_E(fmt, args...)
 * Error logging, shown in debug and release version
 */

/** LOG_V(fmt, args...)
 * Verbose logging, shown in debug version if the including file defines LOG_VERBOSE
 */

/** LOG_I_BUF(szDescriptor, blob, sizeOfBlob)
 * Binary logging, line-wise output to LOG_I
 */

#define EOL "\n"
#define DUMMY_FUNCTION()    do{}while(0)

#ifdef LOG_ANDROID
// log to adb logcat
#ifdef NDEBUG // no logging in debug version
    #define LOG_I(fmt, args...) DUMMY_FUNCTION()
    #define LOG_W(fmt, args...) DUMMY_FUNCTION()
#else
    // add LINE
    #define LOG_I(fmt, args...) LOG_i(fmt ";%d", ## args, __LINE__)
    #define LOG_W(fmt, args...) LOG_w(fmt ";%d", ## args, __LINE__)
#endif
    // LOG_E is always defined
    #define _LOG_E(fmt, args...) LOG_e(fmt, ## args)

    // actually mapping to log system, adding level and tag.
    #define LOG_i(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
    #define LOG_w(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
    #define LOG_e(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

#else //!defined(LOG_ANDROID)
// log to std.out using printf

    // #level / #LOG_TAG ( process_id): __VA_ARGS__
    // Example:
    // I/McDrvBasicTest_0_1( 4075): setUp
    #define _LOG_x(_x_,...) \
                do \
                { \
                    printf("%s/%s(%d): ",_x_,LOG_TAG,GETPID()); \
                    printf(__VA_ARGS__); \
                    printf(EOL); \
                } while(1!=1)


#ifdef NDEBUG // no logging in debug version
    #define LOG_I(fmt, ...) DUMMY_FUNCTION()
    #define LOG_W(fmt, ...) DUMMY_FUNCTION()
#else
    #define LOG_I(...)  _LOG_x("I", __VA_ARGS__)
    #define LOG_W(...)  _LOG_x("W", __VA_ARGS__)
#endif
    #define _LOG_E(...)  _LOG_x("E", __VA_ARGS__)

    #define LOG_i(...) printf(__VA_ARGS__)
	#define LOG_w(...) printf(__VA_ARGS__)
	#define LOG_e(...) printf(__VA_ARGS__)

#endif //defined(LOG_ANDROID)

#if defined(LOG_VERBOSE)
#define LOG_V LOG_I
#else
#define LOG_V(...) DUMMY_FUNCTION()
#endif

/** LOG_E() needs to be more prominent:
 * Display "*********** ERROR ***********" before actual error message.
 */
#define LOG_E(...) \
            do \
            { \
                _LOG_E("  *****************************"); \
                _LOG_E("  *** ERROR: ", __VA_ARGS__); \
                _LOG_E("  *** Detected in %s/%u()", __FUNCTION__, __LINE__); \
                _LOG_E("  *****************************"); \
            } while(1!=1)

#define LOG_ERRNO(MESSAGE) \
    LOG_E("%s failed with \"%s\"(errno %i)", MESSAGE, strerror(errno), errno);

#define LOG_I_BUF   LOG_I_Buf

#ifndef WIN32
__attribute__ ((unused))
#endif
static void LOG_I_Buf(
	const char *  szDescriptor,
	const void *  blob,
	size_t        sizeOfBlob
) {

	#define CPL         0x10  // chars per line
	#define OVERHEAD    20

	char buffer[CPL * 4 + OVERHEAD];

	uint32_t index = 0;

	uint32_t moreThanOneLine = (sizeOfBlob > CPL);
	uint32_t blockLen = CPL;
	uint32_t addr = 0;
	uint32_t i = 0;

	if (NULL != szDescriptor)
	{
		index += sprintf(&buffer[index], "%s", szDescriptor);
	}

	if (moreThanOneLine)
	{
		if (NULL == szDescriptor)
		{
			index += sprintf(&buffer[index], "memory dump");
		}
		index += sprintf(&buffer[index], " (0x%08x, %d bytes)", (uint32_t)blob,sizeOfBlob);
		LOG_I("%s", buffer);
		index = 0;
	}
	else if (NULL == szDescriptor)
	{
		index += sprintf(&buffer[index], "Data at 0x%08x: ", (uint32_t)blob);
	}

	if(sizeOfBlob == 0) {
		LOG_I("%s", buffer);
	}
	else
	{
		while (sizeOfBlob > 0)
		{
			if (sizeOfBlob < blockLen)
			{
				blockLen = sizeOfBlob;
			}

			// address
			if (moreThanOneLine)
			{
				index += sprintf(&buffer[index], "0x%08X | ",addr);
				addr += CPL;
			}
			// bytes as hex
			for (i=0; i<blockLen; ++i)
			{
				index += sprintf(&buffer[index], "%02x ", ((const char *)blob)[i] );
			}
			// spaces if necessary
			if ((blockLen < CPL) && (moreThanOneLine))
			{
				// add spaces
				for (i=0; i<(3*(CPL-blockLen)); ++i) {
				index += sprintf(&buffer[index], " ");
				}
			}
			// bytes as ASCII
			index += sprintf(&buffer[index], "| ");
			for (i=0; i<blockLen; ++i)
			{
				char c = ((const char *)blob)[i];
				index += sprintf(&buffer[index], "%c",(c>32)?c:'.');
			}

			blob = &(((const char *)blob)[blockLen]);
			sizeOfBlob -= blockLen;

			// print line to logcat / stdout
			LOG_I("%s", buffer);
			index = 0;
		}
	}
}

#endif /** TLCWRAPPERANDROIDLOG_H_ */

/** @} */