* Removed all Id tags.
[cacao.git] / src / toolbox / logging.c
index 590af4d2cb73ff445a88a1a55f645c070ed2a7c7..de04b1e573aee75d7623e6fe8254701a5b1c4cb8 100644 (file)
@@ -1,9 +1,9 @@
-/* toolbox/logging.c - contains logging functions
+/* src/toolbox/logging.c - contains logging functions
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    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: logging.c 1925 2005-02-10 10:46:33Z twisti $
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
 */
 
 
+#include "config.h"
+
 #include <stdio.h>
-#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "vm/types.h"
+
 #include "mm/memory.h"
 #include "toolbox/logging.h"
+#include "toolbox/util.h"
 #include "vm/global.h"
-#include "vm/tables.h"
-#include "vm/statistics.h"
+
+#if defined(ENABLE_STATISTICS)
+# include "vmcore/statistics.h"
+#endif
 
 
 /***************************************************************************
@@ -50,7 +50,6 @@
 FILE *logfile = NULL;
 
 
-
 void log_init(const char *fname)
 {
        if (fname) {
@@ -61,166 +60,145 @@ void log_init(const char *fname)
 }
 
 
-/*********************** Function: dolog ************************************
-
-Writes logtext to the protocol file (if opened) or to stdout.
-
-**************************************************************************/
-
-void dolog(const char *txt, ...)
-{
-       char logtext[MAXLOGTEXT];
-       va_list ap;
-
-       va_start(ap, txt);
-       vsprintf(logtext, txt, ap);
-       va_end(ap);
+/* log_start *******************************************************************
 
-       if (logfile) {
-               fprintf(logfile, "%s\n",logtext);
-               fflush(logfile);
+   Writes the preleading LOG: text to the protocol file (if opened) or
+   to stdout.
 
-       } else {
-               fprintf(stdout,"LOG: %s\n",logtext);
-               fflush(stdout);
-       }
-}
+*******************************************************************************/
 
+/* ATTENTION: Don't include threads-common.h, because we can't
+   bootstrap this file in that case (missing java_lang_Thread.h).
+   Instead we declare threads_get_current_threadobject differently: */
 
-/******************** Function: dolog_plain *******************************
+/* #if defined(ENABLE_THREADS) */
+/* # include "threads/threads-common.h" */
+/* #endif */
 
-Writes logtext to the protocol file (if opened) or to stdout.
+extern ptrint threads_get_current_tid(void);
 
-**************************************************************************/
 
-void dolog_plain(const char *txt, ...)
+void log_start(void)
 {
-       char logtext[MAXLOGTEXT];
-       va_list ap;
+#if defined(ENABLE_THREADS)
+       ptrint tid;
 
-       va_start(ap, txt);
-       vsprintf(logtext, txt, ap);
-       va_end(ap);
+       tid = threads_get_current_tid();
+#endif
 
        if (logfile) {
-               fprintf(logfile, "%s", logtext);
-               fflush(logfile);
-
-       } else {
-               fprintf(stdout,"%s", logtext);
-               fflush(stdout);
+#if defined(ENABLE_THREADS)
+# if SIZEOF_VOID_P == 8
+               fprintf(logfile, "[0x%016lx] ", tid );
+# else
+               fprintf(logfile, "[0x%08x] ", tid);
+# endif
+#endif
+       }
+       else {
+#if defined(ENABLE_THREADS)
+# if SIZEOF_VOID_P == 8
+               fprintf(stdout, "LOG: [0x%016lx] ", tid);
+# else
+               fprintf(stdout, "LOG: [0x%08x] ", tid);
+# endif
+#else
+               fputs("LOG: ", stdout);
+#endif
        }
 }
 
 
-/********************* Function: log_text ********************************/
-
-void log_text(const char *text)
-{
-       dolog("%s", text);
-}
+/* log_vprint ******************************************************************
 
+   Writes logtext to the protocol file (if opened) or to stdout.
 
-/******************** Function: log_plain *******************************/
+*******************************************************************************/
 
-void log_plain(const char *text)
+void log_vprint(const char *text, va_list ap)
 {
-       dolog_plain("%s", text);
+       if (logfile)
+               vfprintf(logfile, text, ap);
+       else
+               vfprintf(stdout, text, ap);
 }
 
 
-/****************** Function: get_logfile *******************************/
-
-FILE *get_logfile(void)
-{
-       return (logfile) ? logfile : stdout;
-}
+/* log_print *******************************************************************
 
+   Writes logtext to the protocol file (if opened) or to stdout.
 
-/****************** Function: log_flush *********************************/
+*******************************************************************************/
 
-void log_flush(void)
+void log_print(const char *text, ...)
 {
-       fflush(get_logfile());
-}
+       va_list ap;
 
+       va_start(ap, text);
+       log_vprint(text, ap);
+       va_end(ap);
+}
 
-/********************* Function: log_nl *********************************/
 
-void log_nl(void)
-{
-       log_plain("\n");
-       fflush(get_logfile());
-}
+/* log_println *****************************************************************
 
+   Writes logtext to the protocol file (if opened) or to stdout with a
+   trailing newline.
 
-/********************* Function: log_cputime ****************************/
+*******************************************************************************/
 
-void log_cputime(void)
+void log_println(const char *text, ...)
 {
-       s8 t;
-       int sec, usec;
-       char logtext[MAXLOGTEXT];
+       va_list ap;
 
-       t = getcputime();
-       sec = t / 1000000;
-       usec = t % 1000000;
+       log_start();
 
-       sprintf(logtext, "Total CPU usage: %d seconds and %d milliseconds",
-                       sec, usec / 1000);
-       log_text(logtext);
+       va_start(ap, text);
+       log_vprint(text, ap);
+       va_end(ap);
+
+       log_finish();
 }
 
 
-/* log_message_method **********************************************************
+/* log_finish ******************************************************************
 
-   outputs log text like this:
-
-   LOG: Loading class: java.lang.Object
+   Finishes a logtext line with trailing newline and a fflush.
 
 *******************************************************************************/
 
-void log_message_class(const char *msg, classinfo *c)
+void log_finish(void)
 {
-       char *buf;
-       s4    len;
-
-       len = strlen(msg) + utf_strlen(c->name) + 1;
-
-       buf = MNEW(char, len);
-
-       sprintf(buf, msg);
-       utf_sprint_classname(buf + strlen(buf), c->name);
-
-       log_text(buf);
-
-       MFREE(buf, char, len);
+       if (logfile) {
+               fputs("\n", logfile);
+               fflush(logfile);
+       }
+       else {
+               fputs("\n", stdout);
+               fflush(stdout);
+       }
 }
 
 
-/* log_message_method **********************************************************
+/* log_message_utf *************************************************************
 
-   outputs log text like this:
+   Outputs log text like this:
 
-   LOG: Compiling: java.lang.Object.clone()Ljava.lang.Object;
+   LOG: Creating class: java/lang/Object
 
 *******************************************************************************/
 
-void log_message_method(const char *msg, methodinfo *m)
+void log_message_utf(const char *msg, utf *u)
 {
        char *buf;
        s4    len;
 
-       len = strlen(msg) + utf_strlen(m->class->name) + 1 +
-               utf_strlen(m->name) + utf_strlen(m->descriptor) + 1;
+       len = strlen(msg) + utf_bytes(u) + strlen("0");
 
        buf = MNEW(char, len);
 
-       sprintf(buf, msg);
-       utf_sprint_classname(buf + strlen(buf), m->class->name);
-       strcpy(buf + strlen(buf), ".");
-       utf_sprint(buf + strlen(buf), m->name);
-       utf_sprint_classname(buf + strlen(buf), m->descriptor);
+       strcpy(buf, msg);
+       utf_cat(buf, u);
 
        log_text(buf);
 
@@ -228,68 +206,78 @@ void log_message_method(const char *msg, methodinfo *m)
 }
 
 
-/* error ***********************************************************************
+/* log_message_class ***********************************************************
+
+   Outputs log text like this:
 
-   Like dolog(), but terminates the program immediately.
+   LOG: Loading class: java/lang/Object
 
 *******************************************************************************/
 
-void error(const char *txt, ...)
+void log_message_class(const char *msg, classinfo *c)
 {
-       char logtext[MAXLOGTEXT];
-       va_list ap;
+       log_message_utf(msg, c->name);
+}
 
-       va_start(ap, txt);
-       vsprintf(logtext, txt, ap);
-       va_end(ap);
 
-       if (logfile) {
-               fprintf(logfile, "ERROR: %s\n", logtext);
-       }
+/* log_message_class_message_class *********************************************
 
-       fprintf(stderr, "ERROR: %s\n", logtext);
+   Outputs log text like this:
 
-       exit(1);
-}
+   LOG: Initialize super class java/lang/Object from java/lang/VMThread
 
+*******************************************************************************/
 
-/* panic ***********************************************************************
+void log_message_class_message_class(const char *msg1, classinfo *c1,
+                                                                        const char *msg2, classinfo *c2)
+{
+       char *buf;
+       s4    len;
 
-   Like error(), takes the text to output as an argument.
+       len =
+               strlen(msg1) + utf_bytes(c1->name) +
+               strlen(msg2) + utf_bytes(c2->name) + strlen("0");
 
-*******************************************************************************/
+       buf = MNEW(char, len);
 
-void panic(const char *txt)
-{
-       error("%s", txt);
+       strcpy(buf, msg1);
+       utf_cat_classname(buf, c1->name);
+       strcat(buf, msg2);
+       utf_cat_classname(buf, c2->name);
+
+       log_text(buf);
+
+       MFREE(buf, char, len);
 }
 
 
-/* log_utf *********************************************************************
+/* log_message_method **********************************************************
+
+   Outputs log text like this:
 
-   Log utf symbol.
+   LOG: Compiling: java.lang.Object.clone()Ljava/lang/Object;
 
 *******************************************************************************/
 
-void log_utf(utf *u)
+void log_message_method(const char *msg, methodinfo *m)
 {
-       char buf[MAXLOGTEXT];
-       utf_sprint(buf, u);
-       dolog("%s", buf);
-}
+       char *buf;
+       s4    len;
 
+       len = strlen(msg) + utf_bytes(m->class->name) + strlen(".") +
+               utf_bytes(m->name) + utf_bytes(m->descriptor) + strlen("0");
 
-/* log_plain_utf ***************************************************************
+       buf = MNEW(char, len);
 
-   Log utf symbol (without printing "LOG: " and newline).
+       strcpy(buf, msg);
+       utf_cat_classname(buf, m->class->name);
+       strcat(buf, ".");
+       utf_cat(buf, m->name);
+       utf_cat(buf, m->descriptor);
 
-*******************************************************************************/
+       log_text(buf);
 
-void log_plain_utf(utf *u)
-{
-       char buf[MAXLOGTEXT];
-       utf_sprint(buf, u);
-       dolog_plain("%s", buf);
+       MFREE(buf, char, len);
 }
 
 
@@ -304,4 +292,5 @@ void log_plain_utf(utf *u)
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */