/*写日志的简易实现方法
* */
* */
# include <stdio.h>
# include <time.h>
# include <string.h>
# include <stdlib.h>
# include <time.h>
# include <string.h>
# include <stdlib.h>
//获取当前时间
void GetTime(char* buf){
time_t curr;
struct tm* timeinfo;
curr = time(&curr);
timeinfo=localtime(&curr);
sprintf(buf,"%d-%d-%d %d:%d:%d", timeinfo->tm_year+1900,timeinfo->tm_mon,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
}
void GetTime(char* buf){
time_t curr;
struct tm* timeinfo;
curr = time(&curr);
timeinfo=localtime(&curr);
sprintf(buf,"%d-%d-%d %d:%d:%d", timeinfo->tm_year+1900,timeinfo->tm_mon,timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
}
//写入日志文件
void Log(char* s)
{
FILE* fp;
char buf1[64];
char buf2[1024];
GetTime(buf1);
strcpy (buf2,s);
fp = fopen ("./log.txt","a+");//此处可设置log路径和名称
fprintf (fp,"%s INFO : %s\n",buf1,buf2);
}
int main(int argc, char* argv[])
{
if (argc < 2){
printf ("must input message\n");
exit (1);
}
Log (argv[1]);
}
void Log(char* s)
{
FILE* fp;
char buf1[64];
char buf2[1024];
GetTime(buf1);
strcpy (buf2,s);
fp = fopen ("./log.txt","a+");//此处可设置log路径和名称
fprintf (fp,"%s INFO : %s\n",buf1,buf2);
}
int main(int argc, char* argv[])
{
if (argc < 2){
printf ("must input message\n");
exit (1);
}
Log (argv[1]);
}