Back to index

im-sdk  12.3.91
kolelog.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2003 Sun Microsystems Inc.
00003  *
00004  * This is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  *
00019  * Authors: Karl Park <karl.park@sun.com>
00020  */
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <stdarg.h>
00025 #include <stdlib.h>
00026 #include "kolelog.h"
00027 
00028 
00029 static FILE *fp = NULL;
00030 
00031 void
00032 KOLE_LOG (LOGDEST dest, char *fmt, ...)
00033 {
00034   char *fname;
00035   int strdup_was_called = 0;
00036   
00037   va_list va;
00038   char buffer[1024];
00039   va_start (va, fmt);
00040   vsnprintf (buffer, sizeof (buffer), fmt, va);
00041   va_end (va);
00042 
00043   switch (dest){
00044   case LOGDEST_STDOUT:
00045     fprintf (stdout, "%s\n", buffer);
00046     break;
00047   case LOGDEST_STDERR:
00048     fprintf (stderr, "%s\n", buffer);
00049     break;
00050   case LOGDEST_FILE:
00051     fname = (char *)getenv ("LOGFILE");
00052     if (!fname){
00053       fname = (char *) strdup (LOGFILE);
00054       strdup_was_called = 1;
00055     }
00056     fp = fopen (fname, "a");
00057     
00058     if (!fp){
00059       fprintf (stderr, "error writing log to file %s\n", fname);
00060       fprintf (stderr, "the error msg was\n");
00061       fprintf (stderr, "%s\n", buffer);
00062       break;
00063     }
00064     fprintf (fp, "%s\n", buffer);
00065     fclose (fp);
00066     if (strdup_was_called)
00067       free (fname);
00068     break;
00069   default:
00070     break;
00071   }
00072 }
00073 
00074 #ifdef TEST_RUN
00075 
00076 int
00077 main (int argc, char **argv)
00078 {
00079   int age = 5;
00080   char *name = "jaehee";
00081   
00082   KOLE_LOG (LOGDEST_FILE, "My daughter, %s is %d years old", name, age);
00083   return 0;
00084 }
00085 
00086 #endif
00087