overview
This should be a log module that did not write files in the previous C language module. In order not to change the original module compilation method, I added a simple method of writing log files.
Support windows and linux, support x86 and ARM.
full code
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
# ifdef WIN32
# include <windows.h>
# else
# include <unistd.h>
# endif
# define FILE_MAX_SIZE ( 1024 * 1024 )
void get_local_time ( char * buffer)
{
time_t rawtime;
struct tm * timeinfo;
time ( & rawtime) ;
timeinfo = localtime ( & rawtime) ;
sprintf ( buffer, "%04d-%02d-%02d %02d:%02d:%02d" ,
( timeinfo-> tm_year+ 1900 ) , timeinfo-> tm_mon, timeinfo-> tm_mday,
timeinfo-> tm_hour, timeinfo-> tm_min, timeinfo-> tm_sec) ;
}
long get_file_size ( char * filename)
{
long length = 0 ;
FILE * fp = NULL ;
fp = fopen ( filename, "rb" ) ;
if ( fp != NULL )
{
fseek ( fp, 0 , SEEK_END ) ;
length = ftell ( fp) ;
}
if ( fp != NULL )
{
fclose ( fp) ;
fp = NULL ;
}
return length;
}
void write_log_file ( char * filename, long max_size, char * buffer, unsigned buf_size)
{
if ( filename != NULL && buffer != NULL )
{
long length = get_file_size ( filename) ;
if ( length > max_size)
{
unlink ( filename) ;
}
{
FILE * fp;
fp = fopen ( filename, "at+" ) ;
if ( fp != NULL )
{
char now[ 32 ] ;
memset ( now, 0 , sizeof ( now) ) ;
get_local_time ( now) ;
fwrite ( now, strlen ( now) + 1 , 1 , fp) ;
fwrite ( buffer, buf_size, 1 , fp) ;
fclose ( fp) ;
fp = NULL ;
}
}
}
}
test code
int main ( int argc, char * * argv)
{
int i;
for ( i= 0 ; i< 10 ; ++ i)
{
char buffer[ 32 ] ;
memset ( buffer, 0 , sizeof ( buffer) ) ;
sprintf ( buffer, "====> %d\n" , i) ;
write_log_file ( "log.txt" , FILE_MAX_SIZE, buffer, strlen ( buffer) ) ;
# ifdef WIN32
Sleep ( 100 ) ;
# else
sleep ( 1 ) ;
# endif
}
return 0 ;
}