271 lines
6.5 KiB
C
271 lines
6.5 KiB
C
/** @file log.c
|
|
@brief 系统日志接口文件
|
|
@version 1.0.0
|
|
*/
|
|
#include <sys/time.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#include <uthash/utlist.h>
|
|
#include <uv.h>
|
|
|
|
#include "log.h"
|
|
#include "smart_sound.h"
|
|
#include "inet_api.h"
|
|
#include "libuv_dbus.h"
|
|
#include "server_addr.h"
|
|
|
|
|
|
#ifdef PLATFORM_CPU
|
|
#define LOG_FILE_BASEDIR (".")
|
|
#else
|
|
#define LOG_FILE_BASEDIR ("/tmp")
|
|
#endif
|
|
|
|
#define SYS_POINT_UPLOAD_TIME (600)
|
|
|
|
typedef struct
|
|
{
|
|
char* pDeviceId;
|
|
char savePath[MAX_PATH];
|
|
int iMaxCacheSize;
|
|
int iEveryUploadTime;
|
|
time_t lastPostTime;
|
|
time_t lastMarkTime;
|
|
FILE* pMarkFile;
|
|
uv_rwlock_t uvRwLock;
|
|
uv_thread_t uvIOThread;
|
|
} SYSPOINT_INFO, *PSYSPOINT_INFO;
|
|
|
|
typedef struct SYSPOINT_ITEM
|
|
{
|
|
char* pContent;
|
|
struct SYSPOINT_ITEM *next, *prev;
|
|
} *PSYSPOINT_ITEM;
|
|
|
|
|
|
static SYSPOINT_INFO g_SysPonitInfo;
|
|
static PSYSPOINT_ITEM g_pSysPointArray = NULL;
|
|
static PHTTP_POST_ATTACH g_pLogParamsArray = NULL;
|
|
|
|
static void __uvSysPointIOProc(void *pParams)
|
|
{
|
|
while(TRUE)
|
|
{
|
|
PSYSPOINT_ITEM pItem = NULL, pTmp = NULL;
|
|
int iFileSize;
|
|
time_t tmNow = time(NULL);
|
|
|
|
if(g_SysPonitInfo.pMarkFile)
|
|
{
|
|
uv_rwlock_wrlock(&g_SysPonitInfo.uvRwLock);
|
|
LL_FOREACH_SAFE(g_pSysPointArray, pItem, pTmp)
|
|
{
|
|
fwrite(pItem->pContent, 1, strlen(pItem->pContent), g_SysPonitInfo.pMarkFile);
|
|
free(pItem->pContent);
|
|
LL_DELETE(g_pSysPointArray, pItem);
|
|
free(pItem);
|
|
}
|
|
uv_rwlock_wrunlock(&g_SysPonitInfo.uvRwLock);
|
|
}
|
|
|
|
GET_FILE_SIZE(g_SysPonitInfo.savePath, iFileSize);
|
|
|
|
if(g_SysPonitInfo.lastPostTime == 0 || tmNow < g_SysPonitInfo.lastPostTime)
|
|
{
|
|
g_SysPonitInfo.lastPostTime = g_SysPonitInfo.lastMarkTime = tmNow;
|
|
tmNow = 0;
|
|
}
|
|
#if 0
|
|
LOG_EX(LOG_Debug, "iFile = %d, MaxSize = %d, upTime = %d\n",
|
|
iFileSize, g_SysPonitInfo.iMaxCacheSize, g_SysPonitInfo.iEveryUploadTime);
|
|
|
|
LOG_EX(LOG_Debug, "tm = %d, last = %d\n", tmNow, g_SysPonitInfo.lastPostTime);
|
|
#endif
|
|
if(iFileSize > g_SysPonitInfo.iMaxCacheSize
|
|
|| tmNow - g_SysPonitInfo.lastPostTime > g_SysPonitInfo.iEveryUploadTime)
|
|
{
|
|
int fileSize = 0;
|
|
|
|
GET_FILE_SIZE(g_SysPonitInfo.savePath, fileSize);
|
|
|
|
if(fileSize > 0)
|
|
{
|
|
SysPointMarkUpload();
|
|
g_SysPonitInfo.lastPostTime = tmNow;
|
|
}
|
|
|
|
if(tmNow - g_SysPonitInfo.lastPostTime > g_SysPonitInfo.iEveryUploadTime)
|
|
{
|
|
SysPointMarkUpload();
|
|
g_SysPonitInfo.lastPostTime = tmNow;
|
|
}
|
|
}
|
|
|
|
if(g_SysPonitInfo.pMarkFile)
|
|
{
|
|
fflush(g_SysPonitInfo.pMarkFile);
|
|
}
|
|
|
|
sleep(1);
|
|
}
|
|
|
|
pthread_detach(pthread_self());
|
|
}
|
|
|
|
int SysPointMarkInit(char* pDevId, int iMaxSize, int iPostTime)
|
|
{
|
|
LOG_EX(LOG_Debug, "pDevId = %s, iMaxSize = %d, iPostTime = %d\n", pDevId, iMaxSize, iPostTime);
|
|
|
|
if(g_SysPonitInfo.pDeviceId != NULL)
|
|
{
|
|
if(pDevId && strlen(pDevId) > 0)
|
|
{
|
|
free(g_SysPonitInfo.pDeviceId);
|
|
g_SysPonitInfo.pDeviceId = strdup(pDevId);
|
|
}
|
|
|
|
if(iMaxSize > 0)
|
|
{
|
|
g_SysPonitInfo.iMaxCacheSize = iMaxSize;
|
|
}
|
|
else
|
|
{
|
|
g_SysPonitInfo.iMaxCacheSize = 1024 * 10;
|
|
}
|
|
|
|
if(iPostTime > 0)
|
|
{
|
|
g_SysPonitInfo.iEveryUploadTime = iPostTime;
|
|
}
|
|
else
|
|
{
|
|
g_SysPonitInfo.iEveryUploadTime = SYS_POINT_UPLOAD_TIME;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
memset(&g_SysPonitInfo, 0, sizeof(SYSPOINT_INFO));
|
|
|
|
if(pDevId && strlen(pDevId) > 0)
|
|
{
|
|
g_SysPonitInfo.pDeviceId = strdup(pDevId);
|
|
}
|
|
|
|
if(iMaxSize > 0)
|
|
{
|
|
g_SysPonitInfo.iMaxCacheSize = iMaxSize;
|
|
}
|
|
else
|
|
{
|
|
g_SysPonitInfo.iMaxCacheSize = 1024 * 1024;
|
|
}
|
|
|
|
if(iPostTime > 0)
|
|
{
|
|
g_SysPonitInfo.iEveryUploadTime = iPostTime;
|
|
}
|
|
else
|
|
{
|
|
g_SysPonitInfo.iEveryUploadTime = SYS_POINT_UPLOAD_TIME;
|
|
}
|
|
|
|
sprintf(g_SysPonitInfo.savePath, "%s/sys_point_mark.log", LOG_FILE_BASEDIR);
|
|
|
|
g_SysPonitInfo.pMarkFile = fopen(g_SysPonitInfo.savePath, "w+");
|
|
|
|
if(g_SysPonitInfo.pMarkFile == NULL)
|
|
{
|
|
LOG_EX(LOG_Error, "Create File: %s Error\n", g_SysPonitInfo.savePath);
|
|
return -ERR_OPEN_FILE;
|
|
}
|
|
|
|
uv_rwlock_init(&g_SysPonitInfo.uvRwLock);
|
|
|
|
uv_thread_create(&g_SysPonitInfo.uvIOThread, __uvSysPointIOProc, NULL);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int SysPointMark(char* pMarkInfo)
|
|
{
|
|
PSYSPOINT_ITEM pItem;
|
|
|
|
if(pMarkInfo == NULL || strlen(pMarkInfo) == 0)
|
|
{
|
|
return -ERR_INPUT_PARAMS;
|
|
}
|
|
|
|
if(g_SysPonitInfo.iMaxCacheSize <= 0)
|
|
{
|
|
return -ERR_UNSUPPORT;
|
|
}
|
|
|
|
pItem = (PSYSPOINT_ITEM)malloc(sizeof(struct SYSPOINT_ITEM));
|
|
|
|
if(pItem == NULL)
|
|
{
|
|
return -ERR_MALLOC_MEMORY;
|
|
}
|
|
|
|
pItem->pContent = strdup(pMarkInfo);
|
|
g_SysPonitInfo.lastMarkTime = time(NULL);
|
|
|
|
uv_rwlock_wrlock(&g_SysPonitInfo.uvRwLock);
|
|
LL_APPEND(g_pSysPointArray, pItem);
|
|
uv_rwlock_wrunlock(&g_SysPonitInfo.uvRwLock);
|
|
|
|
//LOG_EX(LOG_Debug, "SysMark: %s\n", pItem->pContent);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int SysPointMarkUpload(void)
|
|
{
|
|
int ret, size = 0;
|
|
char path[MAX_PATH];
|
|
|
|
memset(path, 0, MAX_PATH);
|
|
sprintf(path, "%s.txt", g_SysPonitInfo.savePath);
|
|
|
|
if(access(path, F_OK) == 0)
|
|
{
|
|
unlink(path);
|
|
}
|
|
|
|
GET_FILE_SIZE(g_SysPonitInfo.savePath, size);
|
|
|
|
if(size <= 0)
|
|
{
|
|
LOG_EX(LOG_Debug, "Upload System Mark Data [%s] Is Empty, Skip......\n", g_SysPonitInfo.savePath);
|
|
return 0;
|
|
}
|
|
|
|
rename(g_SysPonitInfo.savePath, path);
|
|
|
|
fclose(g_SysPonitInfo.pMarkFile);
|
|
g_SysPonitInfo.pMarkFile = fopen(g_SysPonitInfo.savePath, "w+");
|
|
|
|
size = 0;
|
|
|
|
do
|
|
{
|
|
ret = InetHttpUploadFileSync(GetCurServerAddr(MARK_POINT_MODULE), path, NULL);
|
|
sleep(1);
|
|
} while(ret != 0 && ++size < 3);
|
|
|
|
LOG_EX(LOG_Debug, "Upload System Mark Data [%s] To Server [%s]\n", path, GetCurServerAddr(MARK_POINT_MODULE));
|
|
|
|
unlink(path);
|
|
|
|
return 0;
|
|
}
|
|
|