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 | |
| 33 | #define MAX_TAG_LENGTH 64 |
| 34 | #define MAX_FILENAME_LENGTH 128 |
| 35 | #define MAX_LOG_BUFFER 1024 |
| 36 | |
| 37 | static long long getCurrentTimeMillis(void); |
| 38 | |
| 39 | static int g_activeLevel= 2; |
| 40 | static FILE * g_fd = stderr; |
| 41 | static char g_fileName[MAX_FILENAME_LENGTH] = {0}; |
| 42 | static std::mutex g_mutext; |
| 43 | |
| 44 | void Logger_set_level(int setLevel) |
| 45 | { |
| 46 | if (setLevel <=0) { |
| 47 | g_activeLevel = 0; |
| 48 | } else if (setLevel > 6) { |
| 49 | g_activeLevel = 6; |
| 50 | } else { |
| 51 | g_activeLevel = setLevel; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int Logger_get_level() |
| 56 | { |
| 57 | return g_activeLevel; |
| 58 | } |
| 59 | |
| 60 | void Logger_set_file(char *filepath) |
| 61 | { |
| 62 | FILE * logFd; |
| 63 | if (!filepath) { |
| 64 | if (g_fd != stderr) { |
| 65 | fclose(g_fd); |
| 66 | g_fd = stderr; |
| 67 | memset(g_fileName, 0 , MAX_FILENAME_LENGTH); |
| 68 | } |
| 69 | return; |
| 70 | } else { //close pre logfile |
| 71 | //had set filepath |
| 72 | if (strcmp(g_fileName, filepath) == 0) { |
| 73 | return; |
| 74 | } |
| 75 | if (g_fd != stderr && strlen(g_fileName) > 0) { |
| 76 | printf("render_client log file:%s \n",g_fileName); |
| 77 | return; |
| 78 | } |
| 79 | memset(g_fileName, 0 , MAX_FILENAME_LENGTH); |
| 80 | strcpy(g_fileName, filepath); |
| 81 | if (g_fd != stderr) { |
| 82 | fclose(g_fd); |
| 83 | g_fd = stderr; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | logFd = fopen(filepath, "w"); |
| 88 | if (logFd == NULL) { |
| 89 | return; |
| 90 | } |
| 91 | g_fd = logFd; |
| 92 | } |
| 93 | |
| 94 | char *logLevelToString(int level) { |
| 95 | if (level == LOG_LEVEL_ERROR) { |
| 96 | return (char *)" E "; |
| 97 | } else if (level == LOG_LEVEL_WARNING) { |
| 98 | return (char *)" W "; |
| 99 | } else if (level == LOG_LEVEL_INFO) { |
| 100 | return (char *)" I "; |
| 101 | } else if (level == LOG_LEVEL_DEBUG) { |
| 102 | return (char *)" D "; |
| 103 | } else if (level == LOG_LEVEL_TRACE) { |
| 104 | return (char *)" V "; |
| 105 | } |
| 106 | return (char *) " U "; |
| 107 | } |
| 108 | |
| 109 | void logPrint(int level, const char *fmt, ... ) |
| 110 | { |
| 111 | if ( level <= g_activeLevel ) |
| 112 | { |
| 113 | if (g_fd == stderr) { //default output log to logcat |
| 114 | va_list argptr; |
| 115 | char buf[MAX_LOG_BUFFER]; |
| 116 | int len = 0; |
| 117 | |
| 118 | len = sprintf(buf, "%lld ",getCurrentTimeMillis()); |
| 119 | int tlen = len > 0? len:0; |
| 120 | tlen = sprintf( buf+tlen, "%d ", getpid()); |
| 121 | if (tlen >= 0) { |
| 122 | len += tlen; |
| 123 | } |
| 124 | va_start( argptr, fmt ); |
| 125 | if (len > 0) { |
| 126 | vsnprintf(buf+len, MAX_LOG_BUFFER-len, fmt, argptr); |
| 127 | } else { |
| 128 | vsnprintf(buf, MAX_LOG_BUFFER, fmt, argptr); |
| 129 | } |
| 130 | va_end( argptr ); |
| 131 | ALOGI("%s", buf); |
| 132 | } else if (g_fd != stderr) { //set output log to file |
| 133 | va_list argptr; |
| 134 | fprintf( g_fd, "%lld ", getCurrentTimeMillis()); |
| 135 | fprintf( g_fd, "%d ", getpid()); |
| 136 | //print log level tag |
| 137 | fprintf( g_fd, "%s ",logLevelToString(level)); |
| 138 | va_start( argptr, fmt ); |
| 139 | vfprintf( g_fd, fmt, argptr ); |
| 140 | va_end( argptr ); |
| 141 | fflush(g_fd); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static long long getCurrentTimeMillis(void) |
| 147 | { |
| 148 | static const clockid_t clocks[] = { |
| 149 | CLOCK_REALTIME, |
| 150 | CLOCK_MONOTONIC, |
| 151 | CLOCK_PROCESS_CPUTIME_ID, |
| 152 | CLOCK_THREAD_CPUTIME_ID, |
| 153 | CLOCK_BOOTTIME |
| 154 | }; |
| 155 | struct timespec t; |
| 156 | t.tv_sec = t.tv_nsec = 0; |
| 157 | clock_gettime(clocks[1], &t); |
| 158 | int64_t mono_ns = int64_t(t.tv_sec)*1000000000LL + t.tv_nsec; |
| 159 | return mono_ns/1000LL; |
| 160 | } |