#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
const char *pName = "readlink"; //程序名
void LogMsg(const char *strMsg);
int main(int argc, char ** argv)
{
int reValue;
const char *pStrMsg = "程序成功";
LogMsg(pStrMsg);
return reValue;
}
void LogMsg(const char *strMsg)
{
int i = 0;
char tRecode[15]; //获得时间
char timeLog[20]; //获得日志文件时间
char path[100];
char pathBuf[200];
char strFileBuf[2000];
time_t tm;
FILE* fileLog = NULL;
memset(path, 0, 100);
memset(pathBuf, 0, 200);
memset(strFileBuf, 0, 2000);
i = readlink("/proc/self/exe", path, 100);
i = i - strlen(pName);
memcpy(pathBuf, path, i);
tm = time(NULL);
memset(tRecode, 0, 15);
memset(timeLog, 0, 20);
strftime(tRecode, sizeof(tRecode), "%T", localtime(&tm));
strftime(timeLog, sizeof(timeLog), "%Y%m%d", localtime(&tm));
sprintf(pathBuf, "%sLogs", pathBuf);
mkdir(pathBuf, 0766); //创建日志存放目录
sprintf(pathBuf, "%s/%sLogs.txt", pathBuf,timeLog);
fileLog = fopen(pathBuf, "a+");
if (fileLog != NULL)
{
memset(strFileBuf, 0, sizeof(strFileBuf));
sprintf(strFileBuf, "%s %s \n", tRecode, strMsg);
fwrite(strFileBuf, strlen(strFileBuf), 1, fileLog);
fclose(fileLog);
}
}
说明:在程序运行中记录程序的日志
转载于:https://blog.51cto.com/wuyouyao/1437081