debugging / logging module

akshayxx
akshayxx used Ask the Experts™
on
i need a light weight Source code in C which i can use in my C programs(if i can compile that into my code) to log  the debugging information OR say to produce application logs..
it would be good if it does the rotating of logs by itself .. ( dont wanna use syslog+ logrotate)..
like samba which keeps logs for each client connected separately in separate directory.

i looked log4c .. but thats a bit too heavy for me.. and that requires expat also .. i dont want dependencies
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What kind of logging information do you plan to collect? Are you looking for a peticular file format or will plain text suffice? What do you mean by rotating logs? Do you mean that it writes to different files depending on the date or do you mean that it begins to overwrite the same file once it reaches a certain size?

Author

Commented:
thanks for taking interest..
Plain file will do.. but should be good enough formatted so that it can be comprehended easily for point of failures.. ok that shud have levels of log, timestamp (this is basic requirement :) )

u can say space separated printf style logging

and also i heard some loggers provide u the feature to log out the line number/function_name program name also..

rotating logs means .. date-wise different log files for different instances of program..
( my case my application will have multiple instances continuously running for long times ( say months) )..

also log files older than certain number of days may be deleted ( or may be archived)..

also it would be nice if u have more suggestions..

Author

Commented:
>>>and also i heard some loggers provide u the feature to log out the line number/function_name program name also

thats basically debugging information :)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Hi akshay,
I will send you logger application soon. Can u give me some time.

Author

Commented:
sure arun_vip.. my email id is akshay_geo@yahoo.com
by that time .. i am trying to strip-off the debugging/logging code from samba 's source code ..
Commented:

Hi akshay,
I made the program in linux. Mostly it will solve your problem.
logmsg.c

#include "logmsg.h"

/******************************************************************************
 * FunctionName      : logmsg_open
 * Develped By       : S.Arunachalam
 * Input Arguments   :
 * Return Value      : int
 * Description       : This function makes todays file if it is not available by PATH/ddmmyyy.log opens today file in append m
ode and returns the pointer
 *                     Returns  FilePointer on success
 *                              NULL on failure
*******************************************************************************/

FILE *logmsg_open()
{
        FILE *fplogmsg = NULL;//file pointer for the filename

        time_t now = time(NULL);
        struct tm *nowtm;
        char filename[MAX_LEN] = {0,};

        //if PATH is /home/guest then filename will be /home/guest/271202.log
        nowtm = localtime(&now);
        sprintf(filename, "%s/%0.2d%0.2d%0.2d.log", PATH, nowtm->tm_mday, nowtm->tm_mon + 1, nowtm->tm_year - 100);

        //opening the logmsg file
        fplogmsg = fopen(filename, "a");
        if(fplogmsg == NULL)
                return NULL;

        return fplogmsg;
}//logmsg_open

/******************************************************************************
 * FunctionName      : logmsg_close
 * Develped By       : S.Arunachalam
 * Input Arguments   : FILE *fplogmsg
 * Return Value      : void
 * Description       : This function closes the FILE descriptor
*******************************************************************************/

void logmsg_close(FILE *fplogmsg)
{
        if(fplogmsg != NULL)
        {
            fclose(fplogmsg);
            fplogmsg = NULL;
        }
}

/******************************************************************************
 * FunctionName      : logmsg_write
 * Develped By       : S.Arunachalam
 * Input Arguments   : int log_level, char *buffer, char *file, char *function, int line
 * Return Value      : int
 * Description       : This function writes the log information in today's file if the standard log level is greater than logl
evel.
 *                     Returns LOGMSG_SUCCESS
 *                             LOGMSG_FAILURE
*******************************************************************************/

int logmsg_write (char *file, char *function, int line, int log_level, char *buffer, ...)
{
        int ret = 0;
        FILE *fplogmsg = NULL;


        if(buffer == NULL || file == NULL || function == NULL)
                return LOGMSG_FAILURE;

        fplogmsg = logmsg_open();
        if(fplogmsg == NULL)
                return LOGMSG_FAILURE;

        //if the loglevel is lesser than than the standard log level then writes into the file
        if(LOGMSG_LOGLEVEL > log_level)
        {
                va_list valogmsg ;
                char vabuffer[MAX_LEN]  = {0,};

                va_start (valogmsg , buffer) ;
                vsprintf(vabuffer, buffer, valogmsg);
                fprintf(fplogmsg, "[%s]:[%s]:[%d]:[%s]\n", file, function, line, vabuffer);
                va_end (valogmsg) ;
        }

        logmsg_close(fplogmsg);

        return LOGMSG_SUCCESS;

}


logmsg.h

#ifndef USELOGMSG
#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#define MAX_LEN 2048

#define PATH "/home/arunachalams/Library/LogMsg"

#define LOGMSG_FAILURE -1
#define LOGMSG_SUCCESS 1
#define LOGMSG_LOGLEVEL 5
#define LOGMSG(level, buffer, ...) logmsg_write(__FILE__, __FUNCTION__, __LINE__, level, buffer, ## __VA_ARGS__)

FILE *logmsg_open();
void logmsg_close(FILE *fplogmsg);

#define USELOGMSG 1
#endif

sample.c
#include "logmsg.h"

main()
{

        char s[] = "This is text message";

        LOGMSG(3, "Trial %s %d %s", s, 20, "abc");
        LOGMSG(4, "Trial  %s", "texpla");
        LOGMSG(4, "Trial2");
        LOGMSG(6, "Trial6"); // It won't print

}

This debug information each day makes one debug file ddmmyyyy.log. The path can be changed by PATH macro.
LOGLEVEL can be changed by LOGMSG_LOGLEVEL.
If you face any problem please contact me.







Author

Commented:
hi arun_vip,
i appreciate the effort that u put into get me this simplest of logging functions. but i m afraid to say that this can be used as it is for any commercial project..
any ways thanks a lot.
i'll see , may be this can be used for light weight applications..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial