fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 Amlogic 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 | #define LOG_NDEBUG 0 |
| 17 | #define LOG_TAG "videorender" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <limits.h> |
| 24 | #include <time.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | #include <pthread.h> |
| 28 | #include <thread> |
| 29 | #include <mutex> |
| 30 | #include <cutils/log.h> |
| 31 | #include "Logger.h" |
| 32 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 33 | #define MAX_USER_TAG 8 |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 34 | #define MAX_TAG_LENGTH 64 |
| 35 | #define MAX_FILENAME_LENGTH 128 |
| 36 | #define MAX_LOG_BUFFER 1024 |
| 37 | |
| 38 | static long long getCurrentTimeMillis(void); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 39 | typedef struct { |
| 40 | char tag[MAX_TAG_LENGTH]; //user print tag |
| 41 | bool active; |
| 42 | } UserTag; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 43 | |
| 44 | static int g_activeLevel= 2; |
| 45 | static FILE * g_fd = stderr; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 46 | static char g_fileName[MAX_FILENAME_LENGTH]; |
| 47 | static UserTag g_userTag[MAX_USER_TAG]; |
| 48 | static int g_activeUserTag = 0; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 49 | static std::mutex g_mutext; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 50 | static int g_init = 0; |
| 51 | |
| 52 | int Logger_init(int id) |
| 53 | { |
| 54 | int category = NO_CAT; |
| 55 | |
| 56 | g_mutext.lock(); |
| 57 | if (g_init == 0) { |
| 58 | g_init = 1; |
| 59 | memset(g_fileName, 0 , MAX_FILENAME_LENGTH); |
| 60 | for (int i = 0; i < MAX_USER_TAG; i++) { |
| 61 | g_userTag[i].active = false; |
| 62 | } |
| 63 | } |
| 64 | for (int i = 0; i < MAX_USER_TAG; i++) { |
| 65 | if (g_userTag[i].active == false) { |
| 66 | g_userTag[i].active = true; |
| 67 | category = i; |
| 68 | memset(g_userTag[i].tag, 0, MAX_TAG_LENGTH); |
| 69 | sprintf(g_userTag[i].tag,"%s-%d","rlib",id); |
| 70 | ++g_activeUserTag; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | g_mutext.unlock(); |
| 75 | return category; |
| 76 | } |
| 77 | |
| 78 | void Logger_exit(int category) |
| 79 | { |
| 80 | g_mutext.lock(); |
| 81 | if (category >= 0 && category < MAX_USER_TAG) { |
| 82 | g_userTag[category].active = false; |
| 83 | --g_activeUserTag; |
| 84 | } |
| 85 | g_mutext.unlock(); |
| 86 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 87 | |
| 88 | void Logger_set_level(int setLevel) |
| 89 | { |
| 90 | if (setLevel <=0) { |
| 91 | g_activeLevel = 0; |
| 92 | } else if (setLevel > 6) { |
| 93 | g_activeLevel = 6; |
| 94 | } else { |
| 95 | g_activeLevel = setLevel; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int Logger_get_level() |
| 100 | { |
| 101 | return g_activeLevel; |
| 102 | } |
| 103 | |
| 104 | void Logger_set_file(char *filepath) |
| 105 | { |
| 106 | FILE * logFd; |
| 107 | if (!filepath) { |
| 108 | if (g_fd != stderr) { |
| 109 | fclose(g_fd); |
| 110 | g_fd = stderr; |
| 111 | memset(g_fileName, 0 , MAX_FILENAME_LENGTH); |
| 112 | } |
| 113 | return; |
| 114 | } else { //close pre logfile |
| 115 | //had set filepath |
| 116 | if (strcmp(g_fileName, filepath) == 0) { |
| 117 | return; |
| 118 | } |
| 119 | if (g_fd != stderr && strlen(g_fileName) > 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 120 | fprintf(stderr, "libvideorender log file:%s \n",g_fileName); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 121 | return; |
| 122 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 123 | fprintf(stderr, "libvideorender log file:%s \n",g_fileName); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 124 | memset(g_fileName, 0 , MAX_FILENAME_LENGTH); |
| 125 | strcpy(g_fileName, filepath); |
| 126 | if (g_fd != stderr) { |
| 127 | fclose(g_fd); |
| 128 | g_fd = stderr; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | logFd = fopen(filepath, "w"); |
| 133 | if (logFd == NULL) { |
| 134 | return; |
| 135 | } |
| 136 | g_fd = logFd; |
| 137 | } |
| 138 | |
| 139 | char *logLevelToString(int level) { |
| 140 | if (level == LOG_LEVEL_ERROR) { |
| 141 | return (char *)" E "; |
| 142 | } else if (level == LOG_LEVEL_WARNING) { |
| 143 | return (char *)" W "; |
| 144 | } else if (level == LOG_LEVEL_INFO) { |
| 145 | return (char *)" I "; |
| 146 | } else if (level == LOG_LEVEL_DEBUG) { |
| 147 | return (char *)" D "; |
| 148 | } else if (level == LOG_LEVEL_TRACE) { |
| 149 | return (char *)" V "; |
| 150 | } |
| 151 | return (char *) " U "; |
| 152 | } |
| 153 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 154 | void logPrint(int category ,int level, const char *fmt, ... ) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 155 | { |
| 156 | if ( level <= g_activeLevel ) |
| 157 | { |
| 158 | if (g_fd == stderr) { //default output log to logcat |
| 159 | va_list argptr; |
| 160 | char buf[MAX_LOG_BUFFER]; |
| 161 | int len = 0; |
| 162 | |
| 163 | len = sprintf(buf, "%lld ",getCurrentTimeMillis()); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 164 | if (g_activeUserTag && category >= 0 && category < MAX_USER_TAG && g_userTag[category].active) { |
| 165 | int tlen = len > 0? len:0; |
| 166 | tlen = sprintf( buf+tlen, "%s ", g_userTag[category].tag); |
| 167 | if (tlen >= 0) { |
| 168 | len += tlen; |
| 169 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 170 | } |
| 171 | va_start( argptr, fmt ); |
| 172 | if (len > 0) { |
| 173 | vsnprintf(buf+len, MAX_LOG_BUFFER-len, fmt, argptr); |
| 174 | } else { |
| 175 | vsnprintf(buf, MAX_LOG_BUFFER, fmt, argptr); |
| 176 | } |
| 177 | va_end( argptr ); |
| 178 | ALOGI("%s", buf); |
| 179 | } else if (g_fd != stderr) { //set output log to file |
| 180 | va_list argptr; |
| 181 | fprintf( g_fd, "%lld ", getCurrentTimeMillis()); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 182 | if (g_activeUserTag && category >= 0 && category < MAX_USER_TAG && g_userTag[category].active) { |
| 183 | fprintf( g_fd, "%s ", g_userTag[category].tag); |
| 184 | } else { |
| 185 | fprintf( g_fd, "%d:%lu ", getpid(),pthread_self()); |
| 186 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 187 | //print log level tag |
| 188 | fprintf( g_fd, "%s ",logLevelToString(level)); |
| 189 | va_start( argptr, fmt ); |
| 190 | vfprintf( g_fd, fmt, argptr ); |
| 191 | va_end( argptr ); |
| 192 | fflush(g_fd); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static long long getCurrentTimeMillis(void) |
| 198 | { |
| 199 | static const clockid_t clocks[] = { |
| 200 | CLOCK_REALTIME, |
| 201 | CLOCK_MONOTONIC, |
| 202 | CLOCK_PROCESS_CPUTIME_ID, |
| 203 | CLOCK_THREAD_CPUTIME_ID, |
| 204 | CLOCK_BOOTTIME |
| 205 | }; |
| 206 | struct timespec t; |
| 207 | t.tv_sec = t.tv_nsec = 0; |
| 208 | clock_gettime(clocks[1], &t); |
| 209 | int64_t mono_ns = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec; |
| 210 | return mono_ns/1000LL; |
| 211 | } |