优化:README

This commit is contained in:
2025-08-04 17:04:56 +08:00
commit e6670a19bd
8 changed files with 598 additions and 0 deletions

245
program/printlog.c Normal file
View File

@ -0,0 +1,245 @@
/*
* @Author: Lucius
* @Date: 2023-03-25 14:05:20
* @LastEditTime: 2023-03-27 17:00:03
* @LastEditors: Lucius
* @Description:
* @FilePath: /23Mar25_PrintLOG/program/printlog.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "printlog.h"
typedef struct _threadLogInfo
{
pthread_t owner;
char threadName[24];
} TThreadLogInfo, *PThreadLogInfo;
static char gLogType = 0 /*LOG_TYPE_ERROR | LOG_TYPE_WARNING | LOG_TYPE_INFO | LOG_TYPE_DEBUG*/;
static char gFileName[24] = {0};
static int gRunMode = PRINT_PROCESS_MODE;
static int gMaxThreadNumber = 0;
static TThreadLogInfo *gpThreadLogInfo = ((void *)0);
static int gThreadCount = 0;
static pthread_mutex_t gThreadLock;
static int gThreadFlag = 0;
static int gErrLogSaveSize = 1020 * 1024;
static unsigned int gErrLogDelSize = 4 * 1024;
static unsigned int gFileLogLevel = 0x00;
#define PRINT_LOG_TAG
#define PRINT_LOGUNKNOW(...) printf(__VA_ARGS__)
#define PRINT_LOGD(...) printf(__VA_ARGS__)
#define PRINT_LOGI(...) printf(__VA_ARGS__)
#define PRINT_LOGW(...) printf(__VA_ARGS__)
#define PRINT_LOGE(...) printf(__VA_ARGS__)
#define VPRINT_LOGD(fmt, args)
unsigned int pDebugFlag[(dDebugFlag_Max + 7) / 8] = {0};
void SetDebugFlag(unsigned int tInputDebugFlag)
{
pDebugFlag[tInputDebugFlag / 8] |= (1 << (tInputDebugFlag % 8));
}
int HaveDebugFlag(unsigned int tInputDebugFlag)
{
if (pDebugFlag[tInputDebugFlag / 8] & (1 << (tInputDebugFlag % 8)))
{
return 1;
}
return 0;
}
int SetLogType(char logType, int enable)
{
if (1 == enable)
{
gLogType = gLogType | logType;
}
else if (0 == enable)
{
gLogType = gLogType & (~logType);
}
else
{
return ERROR_LOG_SET_TYPE;
}
return ERROR_LOG_OK;
}
void SetLogTypesByParams(char *pParams)
{
if (((void *)0) != strchr(pParams, 'E'))
{
SetLogType(LOG_TYPE_ERROR, 1);
}
if (((void *)0) != strchr(pParams, 'W'))
{
SetLogType(LOG_TYPE_WARNING, 1);
}
if (((void *)0) != strchr(pParams, 'I'))
{
SetLogType(LOG_TYPE_INFO, 1);
}
if (((void *)0) != strchr(pParams, 'D'))
{
SetLogType(LOG_TYPE_DEBUG, 1);
}
}
int SetprocessName(const char *pFileName)
{
int ret = 0;
snprintf(gFileName, sizeof(gFileName), "%s", pFileName);
return ret;
}
int SetPrintParameter(int parameterCode, int *parameterValue, int maxValueLen)
{
int ret = 0;
switch (parameterCode)
{
case PRINT_RUN_MODE:
{
int runMode = *parameterValue;
if ((runMode != PRINT_PROCESS_MODE) && (runMode != PRINT_THREAD_MODE))
{
break;
}
gRunMode = *parameterValue;
break;
}
default:
{
break;
}
}
return ret;
}
static int GetCurPrintDateTime(char *pOutBuf, int maxOutBufLen)
{
char tmpYear[5];
struct timeval tv;
struct timezone tz;
struct tm *pCurLocalDateTime = ((void *)0);
if (((void *)0) == pOutBuf)
{
return -1;
}
memset(tmpYear, 0, sizeof(tmpYear));
memset(pOutBuf, 0, maxOutBufLen);
gettimeofday(&tv, &tz);
pCurLocalDateTime = localtime(&tv.tv_sec);
snprintf(tmpYear, sizeof(tmpYear), "%04d", pCurLocalDateTime->tm_year + 1900);
snprintf(pOutBuf, maxOutBufLen, "%s-%02d-%02dT%02d:%02d:%02d.%03ld",
tmpYear, pCurLocalDateTime->tm_mon + 1, pCurLocalDateTime->tm_mday,
pCurLocalDateTime->tm_hour, pCurLocalDateTime->tm_min, pCurLocalDateTime->tm_sec, tv.tv_usec / 1000);
return 0;
}
int GetLogType(void)
{
return gLogType;
}
void PrintLog(enum __PRINT_LOG_DEBUGFLAG debugflag, enum __PRINT_LOG_TYPE logType, const char *pFile, const char *pFunction,
int line, const char *pFormat, ...)
{
if (!HaveDebugFlag(debugflag))
{
return;
}
va_list args;
char filebuf[1024] = {0};
int cnt = 0;
const char *pPreFix = ((void *)0);
if (0 == (logType & gLogType) && 0 == (logType & gFileLogLevel))
return;
switch (logType)
{
case LOG_TYPE_INFO:
{
pPreFix = "\033[1;32;40mInfo\033[0m";
break;
}
case LOG_TYPE_WARNING:
{
pPreFix = "\033[1;33;40mWarning\033[0m";
break;
}
case LOG_TYPE_ERROR:
{
pPreFix = "\033[1;31;40mError\033[0m";
break;
}
case LOG_TYPE_DEBUG:
{
pPreFix = "\033[1;34;40mDebug\033[0m";
break;
}
default:
{
pPreFix = "Unknown";
break;
}
}
char curTime[1024];
GetCurPrintDateTime(curTime, sizeof(curTime));
cnt = sprintf(filebuf, "[%s:%s] %s [%s %s %d]: ",
gFileName, curTime, pPreFix, pFile, pFunction, line);
if (0 != (logType & gLogType))
{
printf("%s", filebuf);
va_start(args, pFormat);
vprintf(pFormat, args);
va_end(args);
printf("\n");
}
va_start(args, pFormat);
if (cnt < 512)
{
vsnprintf(filebuf + cnt, 512, pFormat, args);
}
va_end(args);
fflush(stdout);
return;
}