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