向log檔案列印date

2021-08-20 03:10:49 字數 1261 閱讀 1440

向乙個log檔案列印行號和date在專案中經常用到,所以本篇部落格將程式貼在此處,以後如果在專案中應用到,可以直接拿來應用。

例程:程序向log檔案每隔1秒寫入一行序號和date,如果程序中斷,下次再啟動該程序時,能夠接續上次的序號。

程式如下:

@author: wanghao

@created time : wed 16 may 2018 04:10:25 ampdt

@file name: test3.c

@description:

#include

#include

#include

#include

#include

#define n 128

int main(int argc,char **argv)

file *fp;

int   line=0;

char buf[n];

if((fp= fopen("/home/wanghao/log","a+")) == null) {

fprintf(stderr,"fopen failed:%s\n", strerror(errno));

return-1;

while(fgets(buf, n, fp)!= null) {

line++;

while(1)

time_t t;

/*getthe  time as  the  number of seconds since the epoch,

1970-01-01 00:00:00 +0000 (utc).*/

time(&t);

struct tm *t1;

/*getthe current local time*/

t1=localtime(&t);

if(sprintf(buf, "%-2d %d/%d/%d %d:%d:%d\n",

++line,

t1->tm_year+1900,

t1->tm_mon+1,

t1->tm_mday,

t1->tm_hour,

t1->tm_min,

t1->tm_sec)< 0)

perror("fprintferror");

break;

printf("%s",buf);

fputs(buf,fp);

fflush(fp);

sleep(1);

fclose(fp);

return0;

Qt列印Log日誌到檔案

寫軟體時有時候需要記錄日誌到檔案,方便檢視軟體執行資訊和排查問題,qt有自己的日誌列印功能,實現qt日誌功能需要用到下面的函式,其中qt4和qt5的函式有區別 上面的函式是用來列印除錯資訊,警報資訊,危險資訊和致命資訊的。當qt有內部錯誤產生時,qt除錯庫會列印幾百種警報資訊 通常是異常的函式引數 ...

Log列印完整引數,生成Json檔案

當字元數量足夠多的的時候,log控制台便會自動擷取,不能在控制台上完整輸出,可以利用如下方法,這種方式會在測試機或者虛擬機器的根目錄生成乙個new.json檔案,裡面是完整的log資訊 public static void printstringtofile string str else writ...

LK 列印log控制

我們知道,lk裡面的log只能通過uart列印出來,看一下除了uart的初始化以及相關操作,這邊主要來看一下log的列印是如何控制列印級別的 首先看該檔案 1.project msm8610 mk top level project rules for the msm8610 project loc...