Add onscreenapp
[apps/onscreenapp.git] / sample / app / 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 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include <time.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 enum LOG_LEVEL{
31     LOG_LEVEL_NONE = 0,
32     LOG_LEVEL_ERROR,
33     LOG_LEVEL_WARNING,
34     LOG_LEVEL_NOTICE,
35     LOG_LEVEL_INFO,
36     LOG_LEVEL_DEBUG,
37     LOG_LEVEL_MAX = LOG_LEVEL_DEBUG
38 };
39
40 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
41
42 #define HMI_ERROR(prefix, args,...) _HMI_LOG(LOG_LEVEL_ERROR, __FILENAME__, __FUNCTION__, __LINE__, prefix, args, ##__VA_ARGS__)
43 #define HMI_WARNING(prefix, args,...) _HMI_LOG(LOG_LEVEL_WARNING, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
44 #define HMI_NOTICE(prefix, args,...) _HMI_LOG(LOG_LEVEL_NOTICE, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
45 #define HMI_INFO(prefix, args,...)  _HMI_LOG(LOG_LEVEL_INFO, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
46 #define HMI_DEBUG(prefix, args,...) _HMI_LOG(LOG_LEVEL_DEBUG, __FILENAME__, __FUNCTION__,__LINE__, prefix, args,##__VA_ARGS__)
47
48 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
49
50 static void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
51 {
52     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
53     if(log_level < level)
54     {
55         return;
56     }
57
58     char *message;
59     struct timespec tp;
60     unsigned int time;
61
62     clock_gettime(CLOCK_REALTIME, &tp);
63         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
64
65         va_list args;
66         va_start(args, log);
67         if (log == NULL || vasprintf(&message, log, args) < 0)
68         message = NULL;
69     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
70     va_end(args);
71         free(message);
72 }
73
74 #ifdef __cplusplus
75 }
76 #endif
77 #endif  //__HMI_DEBUG_H__