4

I would like to know if there are any logger libraries for C , that can do circular file logging?

I am currently looking at log4C, But cant find enough docs on it that can say it will do circular logging.

if anyone has done this. kindly let me know.

Thanks

Sam Becker
  • 19,231
  • 14
  • 60
  • 80

3 Answers3

2

here is an example

This is a cut down version. In ours we use vargs and format them before calling log_it.


typedef const char* c_str;
FILE* log_fp = 0;
const int max_log_size = 4 * 1024 * 1024;
const int max_no = 5;
c_str prefix = "logs_";
c_str postfix = ".txt";

void log_it( c_str str )
    {
    char file1[100], file2[100];

    if( ! log_fp )
        {
        sprintf( file1 "%s%d%s", prefix, 0, postfix );
        log_fp = fopen( file1, "a" );
        }

    if( log_fp )
        {
        if( ftell( log_fp ) > max_log_size )
            {
            fclose( log_fp );
            log_fp = 0;

            for( int i = (max_no - 1); i >= 0; i-- )
                {
                sprintf( file1 "%s%d%s", prefix, i, postfix );
                sprintf( file1 "%s%d%s", prefix, i+1, postfix );
                rename( file1, file2 );
                }

            sprintf( file1 "%s%d%s", prefix, 0, postfix );
            log_fp = fopen( file1, "a" );
            }

        fputs( str, log_fp );
        fflush( log_fp );
        }
    }

I hope that helps.

dave

David Allan Finch
  • 1,414
  • 8
  • 20
1

Are you really sure you want circular logging? I think you would be better off with rolling logs.

i.e.

circular logging: log to log.1 then log.2 then log.3 then log.4 then back log.1

rolling logging: have four log files, where log.1 is always the most recent, and log.2,3,4 are older log entries?

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • why not? on running on a development enviroment you can quickly running out of disk-space of you are producing large logs. at my workplace we use rolling logs because of the large number of processes and debug output – hhafez Mar 04 '09 at 09:52
  • 2
    He's asking for circular logging, and I think he probably should want rolling logging. I don't understand why you ask "why not?". – Douglas Leeder Mar 04 '09 at 10:25
0

It seems Log4C is not painfully well documented at this point. They do point at the Log4J page though, which mentioned "rolling" logs, is that perhaps what you want? It could be just a question of terminology confusion.

unwind
  • 391,730
  • 64
  • 469
  • 606