fixed loging crash caused by printf
[cacao.git] / toolbox / loging.c
index e4b9f237057dbf1992b6836bd65d4eafbe310ca1..40946acb2a2069256924bcfcfe390ec9e238e7b0 100644 (file)
-/************************* toolbox/loging.c ************************************
+/* toolbox/loging.c - contains loging functions
 
-       Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+   Institut f. Computersprachen, TU Wien
+   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
+   S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
+   J. Wenninger
 
-       See file COPYRIGHT for information on usage and disclaimer of warranties
+   This file is part of CACAO.
 
-       Not documented, see loging.h.
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
 
-       Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
 
-       Last Change: 1996/10/03
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.
+
+   Contact: cacao@complang.tuwien.ac.at
+
+   Authors: Reinhard Grafl
+
+   $Id: loging.c 730 2003-12-11 21:23:31Z edwin $
+
+*/
 
-*******************************************************************************/
 
 #include <stdio.h>
+#include <stdarg.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 
+#include "global.h"
 #include "loging.h"
 
+
 /***************************************************************************
-                        LOGFILE - BEHANDLUNG 
+                        LOG FILE HANDLING 
 ***************************************************************************/
 
-char logtext[MAXLOGTEXT];   /* Musz mit dem gewuenschten Text vor */
-                            /* Aufruf von dolog() beschrieben werden */
-
-
 FILE *logfile = NULL;
 
 
 
-
 void log_init(char *fname)
 {
        if (fname) {
                if (fname[0]) {
                        logfile = fopen(fname, "w");
-                       }
                }
+       }
 }
 
 
+/*********************** Function: dolog ************************************
+
+Writes logtext to the protocol file (if opened) or to stdout.
+
+**************************************************************************/
+
+void dolog(char *txt, ...)
+{
+       char logtext[MAXLOGTEXT];
+       va_list ap;
+
+       va_start(ap, txt);
+       vsprintf(logtext, txt, ap);
+       va_end(ap);
+
+       if (logfile) {
+               fprintf(logfile, "%s\n",logtext);
+               fflush(logfile);
+
+       } else {
+               fprintf(stdout,"LOG: %s\n",logtext);
+               fflush(stdout);
+       }
+}
+
 
-/*********************** Funktion: dolog ************************************
+/******************** Function: dolog_plain *******************************
 
-Gibt den in logtext stehenden Text auf die Protokollierungsdatei
-aus (wenn sie offen ist) und auszerdem auf stdout. 
+Writes logtext to the protocol file (if opened) or to stdout.
 
 **************************************************************************/
 
-void dolog()
+void dolog_plain(char *txt, ...)
 {
+       char logtext[MAXLOGTEXT];
+       va_list ap;
+
+       va_start(ap, txt);
+       vsprintf(logtext, txt, ap);
+       va_end(ap);
+
        if (logfile) {
-               fprintf (logfile, "%s\n",logtext);
-               fflush (logfile);
-               }
-       else {
-               printf ("LOG: %s\n",logtext);
-               fflush (stdout);
-               }
+               fprintf(logfile, "%s",logtext);
+               fflush(logfile);
+
+       else {
+               fprintf(stdout,"%s",logtext);
+               fflush(stdout);
+       }
 }
 
-/********************* Funktion: log_text ********************************/
 
-void log_text (char *text)
+/********************* Function: log_text ********************************/
+
+void log_text(char *text)
+{
+       dolog("%s", text);
+}
+
+
+/******************** Function: log_plain *******************************/
+
+void log_plain(char *text)
 {
-       sprintf (logtext, "%s",text);
-       dolog();
+       dolog_plain("%s", text);
 }
 
 
-/********************* Funktion: log_cputime ****************************/
+/****************** Function: get_logfile *******************************/
 
-void log_cputime ()
+FILE *get_logfile()
+{
+       return (logfile) ? logfile : stdout;
+}
+
+/****************** Function: log_flush *********************************/
+
+void log_flush()
+{
+       fflush(get_logfile());
+}
+
+/********************* Function: log_nl *********************************/
+
+void log_nl()
+{
+       log_plain("\n");
+       fflush(get_logfile());
+}
+
+/********************* Function: log_cputime ****************************/
+
+void log_cputime()
 {
    long int t;
-   int sec,usec;
+   int sec, usec;
+   char logtext[MAXLOGTEXT];
 
    t = getcputime();
-   sec = t/1000000;
-   usec = t%1000000;
+   sec = t / 1000000;
+   usec = t % 1000000;
 
-   sprintf (logtext, "Total CPU usage: %d seconds and %d milliseconds",
-            sec,usec/1000);
-   dolog();
+   sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
+                  sec, usec / 1000);
+   log_text(logtext);
 }
 
 
+/************************** Function: error *******************************
 
-/************************** Funktion: error *******************************
-
-Wie dolog(), aber das Programm wird auszerdem sofort terminiert.
+Like dolog(), but terminates the program immediately.
 
 **************************************************************************/
 
-void error()
+void error(char *txt, ...)
 {
+       char logtext[MAXLOGTEXT];
+       va_list ap;
+
+       va_start(ap, txt);
+       vsprintf(logtext, txt, ap);
+       va_end(ap);
+
        if (logfile) {
-               fprintf (logfile, "ERROR: %s\n", logtext);
-               }   
-       printf ("ERROR: %s\n",logtext);
+               fprintf(logfile, "ERROR: %s\n", logtext);
+       }
+
+       fprintf(stderr, "ERROR: %s\n", logtext);
        exit(10);
 }
 
 
-/************************ Funktion: panic (txt) ****************************
+/************************ Function: panic (txt) ****************************
 
-  Wie error(), jedoch wird der auszugebende Text als Argument uebergeben
+  Like error(), takes the text to output as an argument
 
 ***************************************************************************/
 
 void panic(char *txt)
 {
-       sprintf (logtext, "%s", txt);
-       error();
+       error("%s", txt);
 }
 
 
-/********************** Funktion: getcputime ********************************
+/********************** Function: getcputime ********************************
 
-       liefert die verbrauchte CPU-Zeit im Mikrosekunden
+       Returns the used CPU time in microseconds
        
 ****************************************************************************/
 
 long int getcputime()
 {
-   struct rusage ru;
-   int sec,usec;
+       struct rusage ru;
+       int sec, usec;
 
-   getrusage (RUSAGE_SELF, &ru);
-   sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
-   usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
-   return sec*1000000 + usec;
+       getrusage(RUSAGE_SELF, &ru);
+       sec = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
+       usec = ru.ru_utime.tv_usec + ru.ru_stime.tv_usec;
+       return sec * 1000000 + usec;
 }
 
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */