Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating A Log File 1

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
0
0
GB
Hello Chaps,

I'm looking to write a function thats called doLog() or somthing to that effect, which will have two arguments, both or which are char.

I basicly want to create a tab delimited log.txt file that contains those details along with a date and time that event occured, So if i pass in the two arguments "had meeting with" and "bob" then it would log those like in the file like.

2007-01-05 13:42:54 Had meeting with Bob

The file is then collected by another application and parsed into the database. When the collection is made, the file is likely to be destroyed, so it doesnt continue to grow. i need this doLog() function to see the file has been deleted, create a new one and then continue to add log entries into it.

I'm running this on a Linux box, and the file will be parsed by a JAVA application that is sat on a windows box. I'm also looking to keep the overheads on the function as low as possible so they dont slow the progress of the application too much.

Thanks for any help you can offer,

Rob
 
I'd do something like this:
Code:
int LogThis( const char* szFirst, const char* szSecond )
{
   int ret;
   char szTime[20];
   time_t tTime;
   struct tm* pTmTime;
   FILE* logFile = fopen( "log.txt", "a" );

   ret = 1; /* Default to error unless everything passes. */

   if ( logFile != NULL )
   {
      if ( (pTmTime = localtime( time( tTime ) )) != NULL )
      {
         if ( strftime( szTime, 20, "%Y-%m-%d %H:%M:%S", pTmTime ) != 0 )
         {
            if ( fprintf( logFile, "%s\t%s\t%s", szDate, szFirst, szSecond ) >= 0 )
            {
               ret = 0; /* success */
            }
         }
      }

      if ( fclose( logFile ) != 0 )
      {
         ret = 1; /* error */
      }
   }

   return ret;
}
 
Thanks very much for the advice mate, but i'm getting compilation errors with it.

scansend.c: In function `LogThis':
scansend.c:23: warning: assignment makes pointer from integer without a cast
scansend.c:27: error: `szDate' undeclared (first use in this function)
scansend.c:27: error: (Each undeclared identifier is reported only once
scansend.c:27: error: for each function it appears in.)

I'm probably being a complete numpty, any ideas why this might be?

Thanks

Rob
 
Oops. szDate should be szTime.
and this line:
Code:
if ( (pTmTime = localtime( time( tTime ) )) != NULL )
should be changed to this:
Code:
if ( (pTmTime = localtime( time( &tTime ) )) != NULL )

I also forgot to mention, if your Java program could be deleting the log file at any time, you need a way to lock it...

Something like this should work:
Code:
int fHandle;
int ret = 1;

if ( (fHandle = open( "logfile.lock", O_CREAT | O_EXCL )) != -1 )
{
   ret = LogThis( "blah", "blah" );
   close( fHandle );
   unlink( "logfile.lock" );
}
else
{
   /* The log file is in use, try again later or something... */
}
Just be sure to add the same kind of logic to your Java program, so it checks if a lock file already exists, and if it doesn't then it creates a "logfile.lock" file before it uses the log file, then deletes the lock file when it's done...
 
Heya Pal, Thanks for that.

I still get a warming when comiling the code using GCC.

scansend.c:23: warning: assignment makes pointer from integer without a cast

Its no big deal I dont think, but I've only been working with this stuff a couple of says and its got me quite lost :-D

When I run the compiled application, I get a segmentation fault, I'm trying to call the log function like this.

LogThis("Device Found",addr);

Is that right?

Thanks for all your help,

Rob
 
Sorry, you caught me before I had my coffee. ;-)
Change:
Code:
if ( (pTmTime = localtime( time( &tTime ) )) != NULL )
to:
Code:
time( &tTime );
if ( (pTmTime = localtime( &tTime )) != NULL )

Is 'addr' a char* string? If it is, it should work.
 
Great stuff mate, it appears to be working a charm now, writing the log just as I wanted it :-D

I still get that warning in the compilation though, god only knows whats causing that, but the app still runs so no major rush on getting that sorted.

Thanks,

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top