Migrate hmi-debug into util
[apps/agl-service-windowmanager.git] / src / util.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "util.hpp"
19
20 #include <time.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <fstream>
25
26 #include <unistd.h>
27
28 static char ERROR_FLAG[6][20] = {"NONE", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
29
30 void rectangle::fit(unsigned long to_width, unsigned long to_height)
31 {
32     // fit rect within (to_width x to_height)
33
34     if (to_width <= width()) {
35         // scale to fit with
36         set_bottom(top() + (static_cast<long>(to_width) * height() / width()) - 1);
37         set_right(left() + to_width - 1);
38     } else {
39         // scale to fit height
40         set_right(left() + (static_cast<long>(to_height) * width () / height()) - 1);
41         set_bottom(top() + to_height - 1);
42     }
43 }
44
45 void rectangle::center(unsigned long outer_w, unsigned long outer_h)
46 {
47     long inner_w = width();
48     long inner_h = height();
49
50     set_left((outer_w - inner_w) / 2);
51     set_right(left() + inner_w - 1);
52     set_top((outer_h - inner_h) / 2);
53     set_bottom(top() + inner_h - 1);
54 }
55
56 void rectangle::set_aspect(double ratio)
57 {
58     // aspect ratio is width:height (= width/height)
59     // e.g. Landscape of HD's ratio is 16:9 (= 1.777...)
60     //      Portrait of HD's ratio is 9:16 (= 0.5625)
61     //
62     // width / height = ratio
63     // width * height = area
64     //
65     // width = sqrt(ratio * area)
66     // height = width / ratio
67
68     long orig_w = width();
69     long orig_h = height();
70
71     if (ratio >= 1) {
72         // width >= height
73         // try to keep width
74         set_right(left() + orig_w - 1);
75         set_bottom(top() + static_cast<long>(orig_w / ratio + 0.5) - 1);
76     } else {
77         set_bottom(top() + orig_h - 1);
78         set_right(left() + static_cast<long>(orig_h * ratio + 0.5) - 1);
79     }
80 }
81
82 std::string get_file_path(const char *file_name, const char *log_category)
83 {
84     char const *default_base_path = getenv("AFM_APP_INSTALL_DIR");
85     std::string path("");
86
87     if(!file_name) {
88         return path;
89     }
90
91     if (!default_base_path)
92     {
93         HMI_ERROR("AFM_APP_INSTALL_DIR is not defined");
94     }
95     else
96     {
97         path = default_base_path;
98         path.append("/etc/");
99         path.append(file_name);
100     }
101
102     // Check for over-ride in /etc/xdg/windowmanager
103     std::string override_path("/etc/xdg/windowmanager/");
104     override_path.append(file_name);
105     std::ifstream i(override_path);
106     if (i.good())
107     {
108         path = override_path;
109     }
110     i.close();
111
112     HMI_INFO("Using %s", path.c_str());
113     return path;
114 }
115 void _HMI_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, const char* prefix, const char* log, ...)
116 {
117     const int log_level = (getenv("USE_HMI_DEBUG") == NULL)?LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
118     if(log_level < level)
119     {
120         return;
121     }
122
123     char *message;
124     struct timespec tp;
125     unsigned int time;
126
127     clock_gettime(CLOCK_REALTIME, &tp);
128     time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
129
130     va_list args;
131     va_start(args, log);
132     if (log == NULL || vasprintf(&message, log, args) < 0)
133         message = NULL;
134     fprintf(stderr,  "[%10.3f] [%s %s] [%s, %s(), Line:%d] >>> %s \n", time / 1000.0, prefix, ERROR_FLAG[level], file, func, line, message);
135     va_end(args);
136     free(message);
137 }
138
139 void _HMI_SEQ_LOG(enum LOG_LEVEL level, const char* file, const char* func, const int line, unsigned seq_num, const char* log, ...){
140     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR:atoi(getenv("USE_HMI_DEBUG"));
141     if(log_level < level)
142     {
143         return;
144     }
145
146     char *message;
147     struct timespec tp;
148     unsigned int time;
149
150     clock_gettime(CLOCK_REALTIME, &tp);
151         time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
152
153         va_list args;
154         va_start(args, log);
155         if (log == NULL || vasprintf(&message, log, args) < 0)
156         message = NULL;
157     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);
158     va_end(args);
159         free(message);
160 }
161
162 void _DUMP(enum LOG_LEVEL level, const char *log, ...)
163 {
164     const int log_level = (getenv("USE_HMI_DEBUG") == NULL) ? LOG_LEVEL_ERROR : atoi(getenv("USE_HMI_DEBUG"));
165     if (log_level < level)
166     {
167         return;
168     }
169     char *message;
170     va_list args;
171     va_start(args, log);
172     if (log == NULL || vasprintf(&message, log, args) < 0)
173         message = NULL;
174     fprintf(stderr, "%s \n", message);
175     va_end(args);
176     free(message);
177 }