Files
2026-08-24 10:03:24 +08:00

188 lines
5.4 KiB
C

#include "log_print_.h"
#include <unistd.h>
#include <sys/syscall.h>
#if 0
#if defined(OS_QT)
#include <QDebug>
#endif
#if defined(OS_Linux)
#else
#include <android/log.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
// #include <sys/time.h>
#define LOG_TAG "amlsource"
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
//#define LEVEL_SETING_PATH "media.amplayer.loglevel"
#include <libavutil/log.h>
#endif
#define CLEAR_COLOR "\033[0m"
#define UNDERLINE "\033[4m"
//lan
//lv
//qing
//huang
//hong
static const char *COLOR[6][2] = {
{"\033[44;37m", "\033[34m"},
{"\033[44;37m", "\033[34m"},
{"\033[42;37m", "\033[32m"},
{"\033[46;37m", "\033[36m"},
{"\033[43;37m", "\033[33m"},
{"\033[41;37m", "\033[31m"}};
//#endif
int64_t get_cur_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t time = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
return time;
}
void pfDebug(int enumlevel, int line, const char *cppname, const char *fun, const char *pszFormat, ...)
{
if (enumlevel > LOG_LEVEL)
return;
struct timeval current;
gettimeofday(&current, NULL);
struct tm stime;
localtime_r(&current.tv_sec, &stime);
va_list argList;
va_start(argList, pszFormat);
char ptstrContent[2048] = {0};
vsnprintf(ptstrContent, 2048 * sizeof(char), pszFormat, argList);
va_end(argList);
unsigned int pid = (unsigned int)pthread_self();
//pid_t pid = getpid();
int tid = (int)syscall(__NR_gettid);
int colorlevel = 0;
switch (enumlevel)
{
case LTrace:
colorlevel = 0;
break;
case LDebug:
colorlevel = 1;
break;
case LPrint:
colorlevel = 2;
break;
case LInfo:
colorlevel = 3;
break;
case LWarn:
colorlevel = 4;
break;
case LError:
colorlevel = 5;
break;
case LNolog:
colorlevel = 0;
break;
}
#if defined(OS_QT)
qDebug("[%02d:%02d:%02d.%03d][Level:%d][tid:%d][%-20s][%-10s][%-4d]%s\n",
stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, enumlevel, tid, cppname, fun, line, ptstrContent);
#elif defined(OS_Linux)
char LOGContent[4096] = {0};
if (strlen(cppname) < 3)
{
sprintf(LOGContent, "%s[%02d:%02d:%02d.%03d][Level:%d][tid:%d][%-20s][%-10s][%-4d]%s%s",
COLOR[colorlevel][1], stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, enumlevel, tid, cppname, fun, line, ptstrContent, CLEAR_COLOR);
}
else
{
sprintf(LOGContent, "%s[%02d:%02d:%02d.%03d][Level:%d][tid:%d][%-20s][%-10s][%-4d]%s%s\n",
COLOR[colorlevel][1], stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, enumlevel, tid, cppname, fun, line, ptstrContent, CLEAR_COLOR);
}
printf("%s", LOGContent);
#else
char LOGContent[4096] = {0};
switch (LOG_SYTLE_DEFAULT)
{
case 0:
sprintf(LOGContent, "%s", ptstrContent);
break;
case 1:
sprintf(LOGContent, "[tid:%d]%s", tid, ptstrContent);
break;
case 2:
if (line == 0)
{
sprintf(LOGContent, "[tid:%d][%-20s]%s",
tid, cppname, ptstrContent);
}
else
{
sprintf(LOGContent, "[tid:%d][%-20s][%-10s][%-4d]%s\n",
tid, cppname, fun, line, ptstrContent);
}
break;
case 3:
if (line == 0)
{
sprintf(LOGContent, "[%02d:%02d:%02d.%03d][tid:%d][%-20s]%s",
stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, tid, cppname, ptstrContent);
}
else
{
sprintf(LOGContent, "[%02d:%02d:%02d.%03d][tid:%d][%-20s][%-10s][%-4d]%s\n",
stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, tid, cppname, fun, line, ptstrContent);
}
break;
case 4:
default:
if (line == 0)
{
sprintf(LOGContent, "%s[%02d:%02d:%02d.%03d][tid:%d][%-20s]%s%s",
COLOR[colorlevel][1], stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, tid, cppname, ptstrContent, CLEAR_COLOR);
}
else
{
sprintf(LOGContent, "%s[%02d:%02d:%02d.%03d][tid:%d][%-20s][%-10s][%-4d]%s%s\n",
COLOR[colorlevel][1], stime.tm_hour, stime.tm_min, stime.tm_sec, (int)current.tv_usec / 1000, tid, cppname, fun, line, ptstrContent, CLEAR_COLOR);
}
}
switch (enumlevel)
{
case LTrace:
LOGV("%s", LOGContent);
break;
case LDebug:
LOGD("%s", LOGContent);
break;
case LPrint:
case LInfo:
LOGI("%s", LOGContent);
break;
case LWarn:
LOGW("%s", LOGContent);
break;
case LError:
LOGE("%s", LOGContent);
break;
}
#endif
}
#endif