Variable Argumets

From: Chul Min Kim (cmkim_at_shinbiro.com)
Date: 03/30/05


Date: 29 Mar 2005 19:33:49 -0800

Hi,

I'm writing logging functions.
I want to use them within my demon program.
I'm working on Solaris 7, Sun Sparc Machine.

Here are source code.
------------------------------------------------------------
// log.c
// 2005.03
// Kim Chul Min <cmkim@shinbiro.com>

// header files
#include <stdio.h> // freopen()
#include <unistd.h> // access()
#include <string.h> // memset()
#include <strings.h> // strcpy()
#include <varargs.h> // va_*
#include <errno.h>

#include "common.h"

// global
char logfile[MAX_PATH_SZ];
int loglevel = DBGLVL_WARNING;
char *logmode = "a";
char tmbuf[40];

// extern
extern int errno;

// function declarations

int make_logfile(char *path, char *mode)
{
        if ( freopen(path, mode, stderr) == NULL ) {
                // PRINT ERROR
                printf("freopen fail=%d(%s)\n", errno, strerror(errno));
                return JOB_FAIL;
        }

        return JOB_OK;
}

void logmsg(va_alist)
va_dcl
{
        int mylevel;
        va_list args;
        char *fmt;

        va_start(args);

        mylevel = va_arg(args, int);
        if (mylevel < loglevel) {
                return;
        }
        if ( access(logfile, F_OK) ) {
                make_logfile(logfile, logmode);
        }
        fmt = va_arg(args, char *);
        vfprintf(stderr, fmt, args);
        fflush(stderr);
        va_end(ap);
        return;
}

char *timestamp(void)
{
        time_t timeval;
        struct tm *tm;

        timeval = time(NULL);
        tm = (struct tm *)localtime(&timeval);
        strftime(tmbuf, 35, "%Y %b %d %X", tm);

        return tmbuf;
}

#ifdef _TEST_
int main(int ac, char **av)
{

        if (ac < 2) {
                puts("***************************************");
                puts("** [USAGE] **");
                puts("** $ PGM LOG_FILENAME **");
                puts("***************************************");
                return 1;
        }

        memset(logfile, NULL, MAX_PATH_SZ);
        strcpy(logfile, av[1]);

        printf("logfile = [%s]\n", logfile);

        if ( make_logfile(logfile, "a") != JOB_OK ) {
                printf("make_logfile() fail.\n");
                return 1;
        }

        logmsg(DBGLVL_WARNING, "[%s] warning logmsg...\n", timestamp());
        logmsg(DBGLVL_ERROR, "[%s] error logmsg...\n", timestamp());
        logmsg(DBGLVL_FATAL, "[%s] fatal logmsg...\n", timestamp());
        logmsg(DBGLVL_ABSOLUTE, "[%s] absolute logmsg...\n", timestamp());
        
        return 0;
}
#endif
------------------------------------------------------------

Here are compile messages.
------------------------------------------------------------
VIS_DEV[yfirst]:/home/yfirst/bear/src/lib> cc -xCC -v -Xa -D_TEST_ -I../h -c log.c
"log.c", line 98: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 99: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 100: warning: argument mismatch: 3 args passed, 1 expected
"log.c", line 101: warning: argument mismatch: 3 args passed, 1 expected
------------------------------------------------------------

My questions are (1) why I got a argument mismatch warning messages
during compile and how can I fix it, (2) I found that above source
code is old K&R style. I want to write them with ANSI C Style
if it is possible on Solaris 7. I cannot find "stdargs.h" file in /usr/include

I mean ANSI C Style variable arguments coding like:

#include <stdargs.h>
void Logmsg(char *Format, ...)
{

}

Thank you for your valuable time.
Kim
Seoul, Korea



Relevant Pages

  • Re: Another computer algebra system : smib
    ... I do not know if you ever see it, but there was a very good book written by Andrew Tanenbau about Unix, which contained the full source code of a small unix operating system called Minix. ... I have not read any Sage python source code. ... I have offered you access to Sun hardware running Solaris if you want to test on Solaris. ... 'Experts Exchange' take questions posted on the web and try to find ...
    (sci.math.symbolic)
  • Re: How to find out whether byte swapping is needed or not?
    ... > I have a huge set of C source code, written, compiled, built and ... > I know when we port from Solaris to Linux we need to take care of Byte ... unsigned int result; ... Enterprise Technology Solutions, TD Bank Financial Group ...
    (comp.os.linux.misc)
  • Re: Very high load average - nothing appears to use much CPU time
    ... which is screwing it up on Solaris. ... Sage was calling 'top' to find the ... My attempts to drum up some interest from AIX and HP-UX ... would take several days to compile the source code. ...
    (comp.unix.solaris)
  • Re: OpenSolaris Release is NEXT WEEK!
    ... the same way Solaris went. ... You can download the OS from Sun for free. ... What is the difference between the OpenSolaris source code and the ...
    (comp.unix.solaris)
  • Re: OpenSolaris Release is NEXT WEEK!
    ... the same way Solaris went. ... You can download the OS from Sun for free. ... What is the difference between the OpenSolaris source code and the ...
    (comp.unix.aix)