e6a34cf11c8dccdc4285b7f9b71cf1666e4366d4
[apps/agl-service-windowmanager-2017.git] / include / hmi-debug.h
1 #ifndef __HMI_DEBUG_H__
2 #define __HMI_DEBUG_H__
3
4 #include <time.h>
5 #include <stdio.h>
6 #include <afb/afb-binding.h>
7
8 enum LOG_LEVEL{
9     LOG_LEVEL_NONE = 0,
10     LOG_LEVEL_ERROR,
11     LOG_LEVEL_WARNING,
12     LOG_LEVEL_NOTICE,
13     LOG_LEVEL_INFO,
14     LOG_LEVEL_DEBUG,
15     LOG_LEVEL_MAX = LOG_LEVEL_ERROR
16 };
17
18 #define HMI_ERROR(prefix, args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILE__, __FUNCTION__, __LINE__, prefix, args, ##__VA_ARGS__)
19 #define HMI_WARNING(prefix, args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILE__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
20 #define HMI_NOTICE(prefix, args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILE__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
21 #define HMI_INFO(prefix, args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILE__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
22 #define HMI_DEBUG(prefix, args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILE__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
23
24 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
25
26 static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
27 {
28     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?0:atoi(getenv("USE_HMI_DEBUG"));
29     if(log_level < level)
30     {
31         return;
32     }
33
34     char *message;
35     struct timespec tp;
36     unsigned int time;
37
38     clock_gettime(CLOCK_REALTIME, &tp);
39         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
40
41         va_list args;
42         va_start(args, log);
43         if (log == NULL || vasprintf(&message, log, args) < 0)
44         message = NULL;
45     fprintf(stderr,  "[%10.3f] [%s %s] [%s:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], func, line, message);
46     va_end(args);
47         free(message);
48 }
49
50 #endif  //__HMI_DEBUG_H__