Remove hmi-debug.h
[apps/agl-service-windowmanager-2017.git] / src / util.cpp
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 #include "util.hpp"
18
19 #include <time.h>
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23
24 #include <unistd.h>
25
26 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
27
28 unique_fd::~unique_fd()
29 {
30     if (this->fd != -1)
31     {
32         close(this->fd);
33     }
34 }
35
36 void rectangle::fit(unsigned long to_width, unsigned long to_height)
37 {
38     // fit rect within (to_width x to_height)
39
40     if (to_width <= width()) {
41         // scale to fit with
42         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
43         set_right(left() + to_width - 1);
44     } else {
45         // scale to fit height
46         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
47         set_bottom(top() + to_height - 1);
48     }
49 }
50
51 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
52 {
53     long inner_w = width();
54     long inner_h = height();
55
56     set_left((outer_w - inner_w) / 2);
57     set_right(left() + inner_w - 1);
58     set_top((outer_h - inner_h) / 2);
59     set_bottom(top() + inner_h - 1);
60 }
61
62 void rectangle::set_aspect(double ratio)
63 {
64     // aspect ratio is width:height (= width/height)
65     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
66     //      Portrait of HD's ratio is 9:16 (= 0.5625)
67     //
68     // width / height = ratio
69     // width * height = area
70     //
71     // width = sqrt(ratio * area)
72     // height = width / ratio
73
74     long orig_w = width();
75     long orig_h = height();
76
77     if (ratio >= 1) {
78         // width >= height
79         // try to keep width
80         set_right(left() + orig_w - 1);
81         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
82     } else {
83         set_bottom(top() + orig_h - 1);
84         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
85     }
86 }
87
88 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
89 {
90     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
91     if(log_level < level)
92     {
93         return;
94     }
95
96     char *message;
97     struct timespec tp;
98     unsigned int time;
99
100     clock_gettime(CLOCK_REALTIME, &tp);
101     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
102
103     va_list args;
104     va_start(args, log);
105     if (log == NULL || vasprintf(&message, log, args) < 0)
106         message = NULL;
107     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
108     va_end(args);
109     free(message);
110 }
111
112 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
113     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
114     if(log_level < level)
115     {
116         return;
117     }
118
119     char *message;
120     struct timespec tp;
121     unsigned int time;
122
123     clock_gettime(CLOCK_REALTIME, &tp);
124         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
125
126         va_list args;
127         va_start(args, log);
128         if (log == NULL || vasprintf(&message, log, args) < 0)
129         message = NULL;
130     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);
131     va_end(args);
132         free(message);
133 }
134
135 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
136 {
137     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
138     if (log_level < level)
139     {
140         return;
141     }
142     char *message;
143     va_list args;
144     va_start(args, log);
145     if (log == NULL || vasprintf(&message, log, args) < 0)
146         message = NULL;
147     fprintf(stderr, "%s \n", message);
148     va_end(args);
149     free(message);
150 }