Hello,
In one of my C codes, I have written a function for logging into a files. When the size of the file exceeds 10 MB, I close that file and open a new file. But at run time my Code is crashing at times during file closing. This behaviour is not at the same point each time. at times the code is crashing, at times it is not.
I am attaching the stack trace for reference:
(gdb) bt full
#0 0x003187a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
No symbol table info available.
#1 0x0035c955 in raise () from /lib/tls/libc.so.6
No symbol table info available.
#2 0x0035e319 in abort () from /lib/tls/libc.so.6
No symbol table info available.
#3 0x0038ff8a in __libc_message () from /lib/tls/libc.so.6
No symbol table info available.
#4 0x0039694c in _int_free () from /lib/tls/libc.so.6
No symbol table info available.
#5 0x00396aca in free () from /lib/tls/libc.so.6
No symbol table info available.
#6 0x0038731a in fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6
No symbol table info available.
#7 0x0805218a in printandlog (index=0, msg_str=0xbfefeb00 "sr Event 2082 292 a77c810 16") at main.c:3239
Time = (struct tm *) 0x45d9e0
Timet = 1205477277
Timeb = {time = 1205477277, millitm = 925, timezone = -330, dstflag = 0}
TimeStr = "[03/14 12:17:57.925 ] ", '\0' <repeats 64 times>
buffer = "[03/14 12:17:57.925 ] sr Event 2082 292 a77c810 16\r\n", '\0' <repeats 934 times>
l_tempbuf = "\000\000"
l_totChar = 65
num = 0
#8 0x0804d213 in Events () at main.c:1221
channel = 16
rc = 0
code = 2082
..........................
The code of function is:
void printandlog(int index,char *msg_str)
{
struct tm *Time;
time_t Timet;
struct timeb Timeb;
char TimeStr[MAX_STRING_SIZE] = {'\0',}; /* will hold the time string when built */
char buffer[MAX_STRING_SIZE] = {'\0',}; /* will have everything but the time */
char l_tempbuf[3] = {'\0',};
int l_totChar = 0;
int num = 0;
//get_time(TimeStr);
time(&Timet);
Time = localtime(&Timet);
ftime(&Timeb); /* needed to get the milliseconds */
sprintf(TimeStr,"[%02d/%02d %02d:%02d:%02d.%03d ] [%d] ",Time->tm_mon+1,Time->tm_mday,Time->tm_hour,Time->tm_min,Time->tm_sec, Timeb.millitm,index);
strcpy(buffer, TimeStr);
strcat(buffer, msg_str);
strcat(buffer,"\n");
if(fileflg == 0)
{
printf("%s,%d",buffer,strlen(buffer));
l_totChar = fprintf(fp,buffer);
g_maxLogFileSize = g_maxLogFileSize + l_totChar;
if(g_maxLogFileSize >= MAXLOGSIZE)
{
//Closing preview file and creating new file
fclose(fp);
sprintf(l_tempbuf,"%c",g_logFile[strlen(g_logFilePath)+7]);
num = atoi(l_tempbuf);
if(num == MAX_LOG_FILES)
num =1;
else
num = num+1;
memset(g_logFile,'\0',sizeof(g_logFile));
sprintf(g_logFile,"%scmrlog_%d.txt",g_logFilePath,num);
printf("[%s]\n",g_logFile);
remove(g_logFile);
if((fp = fopen(g_logFile,"a+|w+")) == NULL)
printf("Cannot open Log File For Writing.\n");
g_maxLogFileSize = 0;
}
fflush(fp);
}
else
printf("%s",buffer);
}
Can any one help me with this? Thanks in advance..
Regards
programmer321 0 Light Poster
jephthah 1,888 Posting Maven
programmer321 0 Light Poster
jephthah 1,888 Posting Maven
programmer321 0 Light Poster
jephthah 1,888 Posting Maven
Petrock6 0 Newbie Poster
jephthah 1,888 Posting Maven
mitrmkar 1,056 Posting Virtuoso
programmer321 0 Light Poster
jephthah 1,888 Posting Maven
programmer321 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.