Rework: Window Manager handles by application
[apps/agl-service-windowmanager-2017.git] / include / hmi-debug.h
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __HMI_DEBUG_H__
18 #define __HMI_DEBUG_H__
19
20 #include <time.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 enum LOG_LEVEL{
27     LOG_LEVEL_NONE = 0,
28     LOG_LEVEL_ERROR,
29     LOG_LEVEL_WARNING,
30     LOG_LEVEL_NOTICE,
31     LOG_LEVEL_INFO,
32     LOG_LEVEL_DEBUG,
33     LOG_LEVEL_MAX = LOG_LEVEL_DEBUG
34 };
35
36 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
37
38 #define HMI_ERROR(prefix, args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, prefix, args, ##__VA_ARGS__)
39 #define HMI_WARNING(prefix, args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
40 #define HMI_NOTICE(prefix, args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
41 #define HMI_INFO(prefix, args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
42 #define HMI_DEBUG(prefix, args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
43
44 #define HMI_SEQ_ERROR(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
45 #define HMI_SEQ_WARNING(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
46 #define HMI_SEQ_NOTICE(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
47 #define HMI_SEQ_INFO(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
48 #define HMI_SEQ_DEBUG(seq_num, args,...) _HMI_SEQ_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__, __LINE__, seq_num, args, ##__VA_ARGS__)
49
50 #define DUMP(args, ...) _DUMP(LOG_LEVEL_DEBUG, args, ##__VA_ARGS__)
51
52 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
53
54 static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
55 {
56     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
57     if(log_level < level)
58     {
59         return;
60     }
61
62     char *message;
63     struct timespec tp;
64     unsigned int time;
65
66     clock_gettime(CLOCK_REALTIME, &tp);
67     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
68
69     va_list args;
70     va_start(args, log);
71     if (log == NULL || vasprintf(&message, log, args) < 0)
72         message = NULL;
73     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
74     va_end(args);
75     free(message);
76 }
77
78 static void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
79     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
80     if(log_level < level)
81     {
82         return;
83     }
84
85     char *message;
86     struct timespec tp;
87     unsigned int time;
88
89     clock_gettime(CLOCK_REALTIME, &tp);
90         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
91
92         va_list args;
93         va_start(args, log);
94         if (log == NULL || vasprintf(&message, log, args) < 0)
95         message = NULL;
96     fprintf(stderr,  "[%10.3f] [wm %s] [%s, %s(), Line:%d] >>> req %d: %s \n", time / 1000.0, ERROR_FLAG[level], file, func, line, seq_num, message);
97     va_end(args);
98         free(message);
99 }
100
101 static void _DUMP(enum LOG_LEVEL level, const char *log, ...)
102 {
103     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
104     if (log_level < level)
105     {
106         return;
107     }
108     char *message;
109     va_list args;
110     va_start(args, log);
111     if (log == NULL || vasprintf(&message, log, args) < 0)
112         message = NULL;
113     fprintf(stderr, "%s \n", message);
114     va_end(args);
115     free(message);
116 }
117 #endif  //__HMI_DEBUG_H__