xref: /OK3568_Linux_fs/external/linux-rga/im2d_api/src/im2d_log.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
3  * Authors:
4  *  Cerf Yu <cerf.yu@rock-chips.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 
24 #ifndef __cplusplus
25 # include <stdatomic.h>
26 #else
27 # include <atomic>
28 # define _Atomic(X) std::atomic< X >
29 using namespace std;
30 #endif
31 
32 #ifdef ANDROID
33 #include <android/log.h>
34 #include <sys/system_properties.h>
35 #endif
36 
37 #include "im2d_log.h"
38 
39 static int rga_log_property_get(void);
40 static int rga_log_level_property_get(void);
41 
42 __thread char g_rga_err_str[IM_ERR_MSG_LEN] = "The current error message is empty!";
43 static atomic_int g_log_en = ATOMIC_VAR_INIT(rga_log_property_get());
44 static atomic_int g_log_level = ATOMIC_VAR_INIT(rga_log_level_property_get());
45 static size_t g_start_time = rga_get_current_time_ms();
46 
rga_error_msg_set(const char * format,...)47 int rga_error_msg_set(const char* format, ...) {
48     int ret = 0;
49     va_list ap;
50 
51     va_start(ap, format);
52     ret = vsnprintf(g_rga_err_str, IM_ERR_MSG_LEN, format, ap);
53     va_end(ap);
54 
55     return ret;
56 }
57 
rga_log_property_get(void)58 static int inline rga_log_property_get(void) {
59 #ifdef ANDROID
60     char level[PROP_VALUE_MAX];
61     __system_property_get("vendor.rga.log" ,level);
62 #else
63     char *level = getenv("ROCKCHIP_RGA_LOG");
64     if (level == nullptr)
65         level = (char *)"0";
66 #endif
67 
68     return atoi(level);
69 }
70 
rga_log_level_property_get(void)71 static int inline rga_log_level_property_get(void) {
72 #ifdef ANDROID
73     char level[PROP_VALUE_MAX];
74     __system_property_get("vendor.rga.log_level" ,level);
75 #else
76     char *level = getenv("ROCKCHIP_RGA_LOG_LEVEL");
77     if (level == nullptr)
78         level = (char *)"0";
79 #endif
80 
81     return atoi(level);
82 }
83 
rga_log_level_init(void)84 int rga_log_level_init(void) {
85     return rga_log_level_get();
86 }
87 
rga_log_level_update(void)88 void rga_log_level_update(void) {
89     g_log_level = rga_log_level_get();
90 }
91 
rga_log_level_get(void)92 int rga_log_level_get(void) {
93     return g_log_level;
94 }
95 
rga_log_enable_get(void)96 int rga_log_enable_get(void) {
97     return g_log_en;
98 }
99 
rga_get_current_time_ms(void)100 size_t rga_get_current_time_ms(void) {
101     struct timeval tv;
102     gettimeofday(&tv,NULL);
103     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
104 }
105 
rga_get_start_time_ms(void)106 size_t rga_get_start_time_ms(void) {
107     return g_start_time;
108 }
109