xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/xcam_log.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * xcam_log.cpp - xcam log
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *  Copyright (c) 2019, Fuzhou Rockchip Electronics Co., Ltd
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20 
21 #include <base/xcam_log.h>
22 #include <base/xcam_defs.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 #include <sys/time.h>
32 #ifdef ANDROID_OS
33 #include <cutils/properties.h>
34 #ifdef ALOGV
35 #undef ALOGV
36 #define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
37 #endif
38 #endif
39 
40 #define LOG_FILE_NAME_MAX_LENGTH 256
41 static char log_file_name[LOG_FILE_NAME_MAX_LENGTH] = {0};
42 /* use a 64 bits value to represent all modules bug level, and the
43  * module bit maps is as follow:
44  *
45  * bit:      7-4                                       3-0
46  * meaning:  [sub modules]                             [level]
47  *
48  * bit:      15          14       13          12       11-8
49  * meaning:  [ABLC]      [AF]     [AWB]       [AEC]    [sub modules]
50  *
51  * bit:      23          22       21          20       19       18        17          16
52  * meaning:  [AGAMMA]    [ACCM]   [ADEBAYER]  [AGIC]   [ALSC]   [ANR]     [ATMO]      [ADPCC]
53  *
54  * bit:      31          30       29          28       27       26        25          24
55  * meaning:  [ASHARP]    [AIE]    [ACP]       [ACSM]   [ALDCH]  [A3DLUT]  [ADEHAZE]   [AWDR]
56  *
57  * bit:      39          38       37          36       35       34        33          32
58  * meaning:  [ADEGAMMA]  [CAMHW]  [ANALYZER]  [XCORE]  [ASD]    [AFEC]    [ACGC]      [AORB]
59  *
60  * bit:      48          46       45          44       43       42        41          40
61  * meaning:  [IPC]   [RKSTREAM] [GROUPAEC] [AWBGROUP]  [CAMGROUP]   [ACAC]    [AMD]   [AMERGE]
62  *
63  * bit:                                                                               48
64  * meaning:                                                                           [AFD]
65  *
66  * bit:     [63-49]
67  * meaning:  [U]
68  *
69  * [U] means unused now.
70  * [level]: use 4 bits to define log levels.
71  *     each module log has following ascending levels:
72  *          0: error
73  *          1: warning
74  *          2: info
75  *          3: debug
76  *          4: verbose
77  *          5: low1
78  *          6-7: unused, now the same as debug
79  * [sub modules]: use bits 4-11 to define the sub modules of each module, the
80  *     specific meaning of each bit is decided by the module itself. These bits
81  *     is designed to implement the sub module's log switch.
82  * [modules]: AEC, AWB, AF ...
83  *
84  * set debug level example:
85  * eg. set module af log level to debug, and enable all sub modules of af:
86  *    Android:
87  *      setprop persist.vendor.rkisp.log 0x4ff4
88  *    Linux:
89  *      export persist_camera_engine_log=0x4ff4
90  *    And if only want enable the sub module 1 log of af:
91  *    Android:
92  *      setprop persist.vendor.rkisp.log 0x4014
93  *    Linux:
94  *      export persist_camera_engine_log=0x4014
95  */
96 static unsigned long long g_cam_engine_log_level = 0xff0;
97 
98 #if 0
99 typedef struct xcore_cam_log_module_info_s {
100     const char* module_name;
101     int log_level;
102     int sub_modules;
103 } xcore_cam_log_module_info_t;
104 #endif
105 
106 xcore_cam_log_module_info_t g_xcore_log_infos[XCORE_LOG_MODULE_MAX] = {
107     {"AEC", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_AEC
108     {"AWB", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_AWB
109     {"AF", XCORE_LOG_LEVEL_ERR, 0xff},        // XCORE_LOG_MODULE_AF
110     {"ABLC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ABLC
111     {"ADPCC", XCORE_LOG_LEVEL_ERR, 0xff},     // XCORE_LOG_MODULE_ADPCC
112     {"ATMO", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ATMO
113     {"ANR", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_ANR
114     {"ALSC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ALSC
115     {"AGIC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_AGIC
116     {"ADEBAYER", XCORE_LOG_LEVEL_ERR, 0xff},  // XCORE_LOG_MODULE_ADEBAYER
117     {"ACCM", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ACCM
118     {"AGAMMA", XCORE_LOG_LEVEL_ERR, 0xff},    // XCORE_LOG_MODULE_AGAMMA
119     {"AWDR", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_AWDR
120     {"ADEHAZE", XCORE_LOG_LEVEL_ERR, 0xff},   // XCORE_LOG_MODULE_ADEHAZE
121     {"A3DLUT", XCORE_LOG_LEVEL_ERR, 0xff},    // XCORE_LOG_MODULE_A3DLUT
122     {"ALDCH", XCORE_LOG_LEVEL_ERR, 0xff},     // XCORE_LOG_MODULE_ALDCH
123     {"ACSM", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ACSM
124     {"ACP", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_ACP
125     {"AIE", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_AIE
126     {"ASHARP", XCORE_LOG_LEVEL_ERR, 0xff},    // XCORE_LOG_MODULE_ASHARP
127     {"AORB", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_AORB
128     {"AFEC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_AFEC
129     {"ACGC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_ACGC
130     {"ASD", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_ASD
131     {"XCORE", XCORE_LOG_LEVEL_ERR, 0xff},     // XCORE_LOG_MODULE_XCORE
132     {"ANALYZER", XCORE_LOG_LEVEL_ERR, 0xff},  // XCORE_LOG_MODULE_ANALYZER
133     {"CAMHW", XCORE_LOG_LEVEL_ERR, 0xff},     // XCORE_LOG_MODULE_CAMHW
134     {"ADEGAMMA", XCORE_LOG_LEVEL_ERR, 0xff},  // XCORE_LOG_MODULE_ADEGAMMA
135     {"AMERGE", XCORE_LOG_LEVEL_ERR, 0xff},    // XCORE_LOG_MODULE_AMERGE
136     {"AMD", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_AMMD
137     {"ACAC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_AMMD
138     {"CAMGROUP", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_CAMGROUP
139     {"AWBGROUP", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_CAMGROUP
140     {"GROUPAEC", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_GROUPAEC
141     {"RKSTREAM", XCORE_LOG_LEVEL_ERR, 0xff},      // XCORE_LOG_MODULE_RKSTREAM
142     {"IPCSERVER", XCORE_LOG_LEVEL_ERR, 0xff},     // XCORE_LOG_MODULE_IPC
143     {"AFD", XCORE_LOG_LEVEL_ERR, 0xff},       // XCORE_LOG_MODULE_AFD
144 };
145 
xcam_get_enviroment_value(const char * variable,unsigned long long * value)146 bool xcam_get_enviroment_value(const char* variable, unsigned long long* value)
147 {
148     if (!variable || !value) {
149         return false;
150 
151     }
152 
153     char* valueStr = getenv(variable);
154     if (valueStr) {
155         *value = strtoull(valueStr, nullptr, 16);
156 
157         return true;
158     }
159     return false;
160 }
161 
xcam_get_runtime_log_level()162 void xcam_get_runtime_log_level() {
163     const char* file_name = "/tmp/.rkaiq_log";
164 
165     if (!access(file_name, F_OK)) {
166         FILE *fp = fopen(file_name, "r");
167         char level[64] = {'\0'};
168 
169         if (!fp)
170             return;
171 
172         fseek(fp, 0, SEEK_SET);
173         if (fp && fread(level, 1, sizeof (level), fp) > 0) {
174             for (int i = 0; i < XCORE_LOG_MODULE_MAX; i++) {
175                 g_xcore_log_infos[i].log_level = 0;
176                 g_xcore_log_infos[i].sub_modules = 0;
177             }
178             g_cam_engine_log_level = strtoull(level, nullptr, 16);
179             unsigned long long module_mask = g_cam_engine_log_level >> 12;
180             for (int i = 0; i < XCORE_LOG_MODULE_MAX; i++) {
181                 if (module_mask & (1ULL << i)) {
182                     g_xcore_log_infos[i].log_level = g_cam_engine_log_level & 0xf;
183                     g_xcore_log_infos[i].sub_modules = (g_cam_engine_log_level >> 4) & 0xff;
184                 }
185             }
186         }
187 
188         fclose (fp);
189     }
190 }
191 
xcam_get_log_level()192 int xcam_get_log_level() {
193 #ifdef ANDROID_OS
194     char property_value[PROPERTY_VALUE_MAX] = {0};
195 
196     property_get("persist.vendor.rkisp.log", property_value, "0");
197     g_cam_engine_log_level = strtoull(property_value, nullptr, 16);
198 
199 #else
200     xcam_get_enviroment_value("persist_camera_engine_log",
201                               &g_cam_engine_log_level);
202 #endif
203     printf("rkaiq log level %llx\n", g_cam_engine_log_level);
204     unsigned long long module_mask = g_cam_engine_log_level >> 12;
205 
206     for (int i = 0; i < XCORE_LOG_MODULE_MAX; i++) {
207         if (module_mask & (1ULL << i)) {
208             g_xcore_log_infos[i].log_level = g_cam_engine_log_level & 0xf;
209             g_xcore_log_infos[i].sub_modules = (g_cam_engine_log_level >> 4) & 0xff;
210         } else if ( g_cam_engine_log_level == 0) {
211             g_xcore_log_infos[i].log_level = 0;
212         }
213     }
214 
215     return 0;
216 }
217 
timeString()218 char* timeString() {
219     struct timeval tv;
220     gettimeofday(&tv, NULL);
221     struct tm * timeinfo = localtime(&tv.tv_sec);
222     static char timeStr[64];
223     sprintf(timeStr, "%.2d:%.2d:%.2d.%.6ld", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, tv.tv_usec);
224     return timeStr;
225 }
226 
xcam_print_log(int module,int sub_modules,int level,const char * format,...)227 void xcam_print_log (int module, int sub_modules, int level, const char* format, ...) {
228     if ((g_cam_engine_log_level & 0xf) == 0) return;
229     char buffer[XCAM_MAX_STR_SIZE] = {0};
230     va_list va_list;
231     va_start (va_list, format);
232     vsnprintf (buffer, XCAM_MAX_STR_SIZE, format, va_list);
233     va_end (va_list);
234 
235 #ifdef NDEBUG
236     if (strlen (log_file_name) > 0) {
237         FILE* p_file = fopen (log_file_name, "ab+");
238         if (NULL != p_file) {
239             fwrite (buffer, sizeof (buffer[0]), strlen (buffer), p_file);
240             fclose (p_file);
241         } else {
242             printf("error! can't open log file !\n");
243         }
244         return ;
245     }
246 #endif
247 #ifdef ANDROID_OS
248     switch(level) {
249     case XCORE_LOG_LEVEL_ERR:
250         ALOGE("[%s]:%s", g_xcore_log_infos[module].module_name, buffer);
251         break;
252     case XCORE_LOG_LEVEL_WARNING:
253         ALOGW("[%s]:%s", g_xcore_log_infos[module].module_name, buffer);
254         break;
255     case XCORE_LOG_LEVEL_INFO:
256         ALOGI("[%s]:%s", g_xcore_log_infos[module].module_name, buffer);
257         break;
258     case XCORE_LOG_LEVEL_VERBOSE:
259         ALOGV("[%s]:%s", g_xcore_log_infos[module].module_name, buffer);
260         break;
261     case XCORE_LOG_LEVEL_DEBUG:
262     default:
263         ALOGD("[%s]:%s", g_xcore_log_infos[module].module_name, buffer);
264         break;
265     }
266 #else
267     printf ("%s:%s", g_xcore_log_infos[module].module_name, buffer);
268 #endif
269 }
270 
xcam_set_log(const char * file_name)271 void xcam_set_log (const char* file_name) {
272     if (NULL != file_name) {
273         memset (log_file_name, 0, LOG_FILE_NAME_MAX_LENGTH);
274         strncpy (log_file_name, file_name, LOG_FILE_NAME_MAX_LENGTH - 1);
275         log_file_name[LOG_FILE_NAME_MAX_LENGTH - 1] = '\0';
276     }
277 }
xcam_get_awb_log_level(unsigned char * log_level,unsigned char * sub_modules)278 void xcam_get_awb_log_level(unsigned char *log_level, unsigned char *sub_modules)
279 {
280     //xcam_get_log_level();
281     *log_level = g_xcore_log_infos[XCORE_LOG_MODULE_AWB].log_level;
282     *sub_modules = g_xcore_log_infos[XCORE_LOG_MODULE_AWB].sub_modules;
283 }
284