tags:

views:

98

answers:

3

Hi,

I would like to add logs at the end of a file for each event, and create a new one when its size up to 255 Mo.

For example, the current file could be /var/log/foo.2:

/var/log/foo.0.log (full log file)
/var/log/foo.1.log (full log file)
/var/log/foo.2.log

Have you got an idea of C source to do so?

Thank you

A: 

If you just want to have the functionality, look at log4c.

If you want to know how that works, you can look at its code as well.

If you need specially tailored code for your application...

Basically you need to use fopen with the "a" option, where a stands for append.

To determine size of a file, use ftell or another platform specific function because there is no file size information method in the C standard as far as I can remember. Or you go the long way and just read all the bytes from the file and count them...

Gerd Klima
+1  A: 

When opening the file with

File *fopen(const char *filename, const char *mode);

choose "a" as mode. Which will open or create a text file for writing at the end of file.

Lucas