C
Chul Min Kim
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 <[email protected]>
// 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
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 <[email protected]>
// 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