summaryrefslogtreecommitdiff
path: root/common/src/debug.c
blob: 2dc7e7c8e016e2a2fcda119321da445c7b71613c (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>		// localtime_r
#include <sys/time.h>	// gettimeofday
#include <unistd.h>
#include <string.h>		// strrchr

#ifndef __G_LIB_H__
	#define G_UNLIKELY(x) x
#endif


#include "debug.h"

enum {
	_DEBUG_OUTPUT_DIRECTION_CONSOLE = (1 << 0),
	_DEBUG_OUTPUT_DIRECTION_SYSLOG = (1 << 1),
	_DEBUG_OUTPUT_DIRECTION_FILE = (1 << 2),
	_DEBUG_OUTPUT_DIRECTION_DLOG = (1 << 3),
};

// Configuration for output
// console message slower than dlog about 30~40 ms
#define _DEBUG_OUTPUT_DIRECTION (_DEBUG_OUTPUT_DIRECTION_DLOG)

#include <dlog.h>

#ifndef INI_PATH
#define INI_PATH "/var/log"
#endif

#ifndef _DEBUG_MODULE
#define _DEBUG_MODULE "IVUG"
#endif

#define _DEBUG_ERR_TRACE_FILE_NAME	INI_PATH"/"_DEBUG_MODULE"_FATAL.txt"
#define _DEBUG_PREFIX "["_DEBUG_MODULE"]"

static bool bInit = false;

static char *get_time_string(unsigned long dwmSec)
{
	static char buffer[30];

	unsigned long msec=0;
	unsigned long sec=0;
	unsigned long min=0;
	unsigned long hour=0;

// Don't forget turn on compiler optimization options.
	sec = ( dwmSec / 1000 );
	msec = ( dwmSec % 1000 );

	min = (sec / 60 );
	sec = (sec % 60 );

	hour = (min / 60 );
	min = (min % 60 );

	snprintf(buffer, (size_t)sizeof(buffer), "%1d:%02d:%02d.%03d",	(int)hour,(int)min,(int)sec,(int)msec);

	return buffer;
}


/*Retrieves the number of milliseconds that have elapsed since the system was started*/
static unsigned long get_sys_elapsed_time(void)
{
	static struct timeval init_time = { 0 , 0 };
	static bool bFirst = false;
	struct timeval current_time;

	if ( bFirst == false )
	{
		bFirst = true;
		gettimeofday(&init_time, NULL);
	}

	gettimeofday(&current_time, NULL);

	// 	return 	(current_time.tv_sec - init_time.tv_sec) * 1000UL + 	(UINT32)((current_time.tv_usec - init_time.tv_usec) / 1000.0)	;
	return 	((current_time.tv_sec * 1E3 + current_time.tv_usec / 1E3) - (init_time.tv_sec * 1E3 + init_time.tv_usec / 1E3));
}

void _custom_debug_init()
{
	get_sys_elapsed_time();
}

void _custom_debug_deinit()
{

}

void
_custom_err_trace_write( const char *func_name, int line_num, const char *fmt, ... )
{
	FILE *f    = NULL;
	va_list ap = {0};
	char buf[128];

	time_t current_time;
	struct tm new_time;

	current_time = time( NULL );
	localtime_r( &current_time, &new_time );

	f = fopen( _DEBUG_ERR_TRACE_FILE_NAME, "a" );
	if( f == NULL )
	{
		printf( "Failed to open file.[%s]\n", _DEBUG_ERR_TRACE_FILE_NAME );
		return;
	}

	fprintf( f, "[%.19s][%05d][%s]", asctime_r( &new_time, buf ), line_num, func_name );

	va_start( ap, fmt );
	vfprintf( f, fmt, ap );
	va_end( ap );

	fprintf( f, "\n" );

	fclose( f );
}

void
_custom_err_trace_fvprintf( const char *func_name, int line_num, const char *fmt, va_list ap )
{
	FILE *f    = NULL;

	time_t current_time;
	struct tm new_time;
	char buf[128];

	current_time = time( NULL );
	localtime_r( &current_time, &new_time );

	f = fopen( _DEBUG_ERR_TRACE_FILE_NAME, "a" );
	if( f == NULL )
	{
		printf( "Failed to open file.[%s]\n", _DEBUG_ERR_TRACE_FILE_NAME );
		return;
	}

	fprintf( f, "[%.19s][[F:%-16.16s L:%5d] ", asctime_r( &new_time, buf ), func_name, line_num );
	vfprintf( f, fmt, ap );
	fprintf( f, "\n" );

	fclose( f );
}

/*
typedef enum log_priority {
        DLOG_UNKNOWN = 0,
        DLOG_DEFAULT,
        DLOG_VERBOSE,
        DLOG_DEBUG,
        DLOG_INFO,
        DLOG_WARN,
        DLOG_ERROR,
        DLOG_FATAL,
        DLOG_SILENT,
} log_priority;

#define LOG(priority, tag, ...) \
#define LOG_VA(priority, tag, fmt, args) \
*/

inline log_priority convert_to_dlog_priority(int msg_level)
{
/*
DBG_MSG_LOW 	= 0,
DBG_MSG_MED 		= 1,
DBG_MSG_HIGH		= 2,

DBG_MSG_WARN		= 3,
DBG_MSG_ERROR		= 4,
DBG_MSG_FATAL		= 5,

DBG_MSG_CUST5		= 6,
DBG_MSG_CUST6		= 7,
DBG_MSG_CUST7		= 8,
DBG_MSG_CUST8		= 9,
DBG_MSG_CUST9		= 10,
DBG_MSG_CUST10	= 11,
DBG_MSG_CUST11	= 12,
DBG_MSG_CUST12	= 13,
DBG_MSG_CUST13	= 14,
*/

	static log_priority priority[] = {
		DLOG_VERBOSE,
		DLOG_VERBOSE,
		DLOG_WARN, 		// MSG HIGH
		DLOG_WARN,
		DLOG_ERROR,
		DLOG_FATAL,		// 5
	};

	if ( msg_level <= DBG_MSG_FATAL)
		return priority[msg_level];

	return DLOG_DEBUG;

}



void __custom_debug_msg(debug_msg_type *debug_msg, const char *msg, ...)
{
	va_list va;

	static const char *level ;

	if ( G_UNLIKELY(bInit == false) )
	{
		_custom_debug_init();
		bInit = true;
	}

#define DIRECORY_SPLITTER '/'
	const char*pFileName = NULL;

	debug_msg->time = get_sys_elapsed_time();

	pFileName  =   strrchr(debug_msg->fname, DIRECORY_SPLITTER);
	pFileName = (NULL == pFileName)?debug_msg->fname:(pFileName+1);

	level = debug_msg->szlevel;

	// File
	char *time_string = get_time_string(debug_msg->time);

	va_start(va, msg);

	if ( _DEBUG_OUTPUT_DIRECTION & _DEBUG_OUTPUT_DIRECTION_CONSOLE )
	{

		printf(_DEBUG_PREFIX"%s[F:%-16.16s L:%5d][%s:%s] ", time_string , pFileName, debug_msg->nline , debug_msg->szcategory, level );
		vprintf(msg, va);
		printf("\n");
	}

	if ( _DEBUG_OUTPUT_DIRECTION & _DEBUG_OUTPUT_DIRECTION_DLOG )
	{
		static char buf[2048];

		int i ;

		i = snprintf(buf, 2048, "%s[F:%-16.16s L:%5d][%s] ",time_string , pFileName, debug_msg->nline , level);

		vsnprintf( buf + i, 2048 - i, msg, va);

// Prevent Format string attack
		print_log(convert_to_dlog_priority(debug_msg->msg_level), debug_msg->szcategory, "%s", buf);
//		print_log(prio, _DEBUG_MODULE, "%s[F:%-16.16s L:%5d][%s:%s] ",time_string , pFileName, debug_msg->nline , szCategory[debug_msg->category], level );
//		vprint_log(prio,_DEBUG_MODULE, msg, va);
	}

#if 0
	if ( _DEBUG_OUTPUT_DIRECTION & _DEBUG_OUTPUT_DIRECTION_FILE )
	{
		fprintf(_DEBUG_PREFIX"%s[F:%-16.16s L:%5d][%s:%s] ",time_string , pFileName, debug_msg->nline , szCategory[debug_msg->category], level );
		vprintf(msg, va);
		fprintf("\n");
	}
#endif
	va_end(va);


	if ( G_UNLIKELY(debug_msg->msg_level == DBG_MSG_FATAL) )
	{
		fflush (stdout);
		va_start(va, msg);
		_custom_err_trace_fvprintf(pFileName, debug_msg->nline, msg, va);		// Save to file.
		va_end(va);
		assert(0);
	}

}


#ifdef FMRADIO_FEATURE_ENABLE_GSTREAMER_LOGGING

enum {
	DEBUG_COLOR_DEFAULT 	= 0,
	DEBUG_COLOR_BLACK		= 30,
	DEBUG_COLOR_RED 		= 31,
	DEBUG_COLOR_GREEN		= 32,
	DEBUG_COLOR_YELLOW		= 33,
	DEBUG_COLOR_BLUE		= 34,
	DEBUG_COLOR_MAGENTA 	= 35,
	DEBUG_COLOR_CYAN		= 36,
	DEBUG_COLOR_WHITE		= 37,
};

static gchar *custom_print_object(GObject *object)
{
	if (object == NULL)
	{
		return g_strdup("Unknown");
	}
/*
	if (*(GType *) ptr == GST_TYPE_CAPS)
	{
		return gst_caps_to_string((GstCaps *) ptr);
	}

	if (*(GType *) ptr == GST_TYPE_STRUCTURE)
	{
		return gst_structure_to_string((GstStructure *) ptr);
	}
*/
	if (GST_IS_PAD(object))
	{
		return g_strdup_printf("%s:%s", GST_STR_NULL( GST_OBJECT_NAME( GST_PAD_PARENT(object))) , GST_STR_NULL( GST_PAD_NAME(object) ));
	}

	if (GST_IS_ELEMENT(object))
	{
		return g_strdup_printf("%s", GST_STR_NULL(GST_ELEMENT_NAME(object)));
	}

	if (G_IS_OBJECT(object))
	{
		return g_strdup_printf("%s(0x%0x)", G_OBJECT_TYPE_NAME(object), object);
	}

	return g_strdup_printf("0x%08x", object);
}

void custom_log_func(GstDebugCategory *category, GstDebugLevel level,
		const gchar *file, const gchar *function, gint line,
		GObject *object, GstDebugMessage *message, gpointer unused)
{
	static const char *szLevel[] = {"LOW", "MED", "HIGH", "WARN", "ERROR", "FATAL"};

	static const gint levelcolor[] = {
		DEBUG_COLOR_DEFAULT,		/* GST_LEVEL_NONE */
		DEBUG_COLOR_RED,			/* GST_LEVEL_ERROR */
		DEBUG_COLOR_YELLOW, 		/* GST_LEVEL_WARNING */
		DEBUG_COLOR_GREEN,			/* GST_LEVEL_INFO */
		DEBUG_COLOR_CYAN,			/* GST_LEVEL_DEBUG */
		DEBUG_COLOR_WHITE,			/* GST_LEVEL_LOG */
	};

	if (level > gst_debug_category_get_threshold(category))
		return;

	gchar *obj = custom_print_object(object);

#define DIRECORY_SPLITTER '/'

	const char*pFileName = NULL;

	pFileName  =   strrchr(file, DIRECORY_SPLITTER);
	pFileName = (NULL == pFileName) ? file:(pFileName+1);

// File
	char *time_string = get_time_string(get_sys_elapsed_time());

	log_print_rel(LOG_CAMCORDER,LOG_CLASS_ERR,
		_DEBUG_PREFIX "%s[F:%-16.16s L:%5d][%s][%s][%s] %s\n", time_string, pFileName, line,
				gst_debug_category_get_name(category), gst_debug_level_get_name(level),obj,
				gst_debug_message_get(message));

	g_free(obj);
}


#endif // FMRADIO_FEATURE_ENABLE_GSTREAMER_LOGGING