Macro definition Log file name
#define PROCESSNAME "Log_filename"
When the log file is larger than 5M, the file is deleted and the interface uses the method reference printf
voidWritelog (Const Char*FM, ...) { intBuflen =5120; CharBuf[buflen]; inti =0; memset (BUF,0, Buflen); Va_list args; Va_start (args, FM); vsnprintf (buf, Buflen, Fm,args); Va_end (args); printf ("%s\n", BUF); FILE* logfile=NULL; Charlogpath[ -] = {0}; snprintf (LogPath,sizeof(LogPath),"/var/log/"); if(Access (LogPath,0)!=0) { Charcmdstr[ the] = {0}; sprintf (Cmdstr,"mkdir-p%s", LogPath); System (CMDSTR); } Charfname[ the]; Charlongtime[ $]; time_t T; memset (fname,0,sizeof(fname)); Time (&t); structTM Local = {0}; Localtime_r (&t, &local); sprintf (longtime,"%04d-%02d-%02d%02d:%02d:%02d", local.tm_year+1900, Local.tm_mon+1, Local.tm_mday, Local.tm_hour, Local.tm_min, local.tm_sec); sprintf (fname,"%s/%s", LogPath, PROCESSNAME); for(i=0; I <3; i++) {logfile= fopen (fname,"AB"); if(logfile) Break; Sleep (1); } if(logfile) {//Determine file size if(Ftell (logfile) <5*1024x768*1024x768)//5M { //Date Time Const Char* Ptemp =longtime; Fwrite (Ptemp,1, strlen (ptemp), logfile); Fwrite (" ",1,1, logfile); //contentFwrite (BUF,1, strlen (BUF), logfile); Fwrite ("\ r \ n",1,3, logfile); Fclose (logfile); } Else{fclose (logfile); Remove (fname); } }}
Linux/C + + log printing function