lprintf.cpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.24 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 # include "incls/_precompiled.incl"
00025 # include "incls/_lprintf.cpp.incl"
00026 #include <string.h>
00027 
00028 static FILE* logFile = NULL;
00029 static char fname[80];
00030 
00031 // dont't use include files for the thing below because this would include
00032 // the conflicting defs of lprintf et al.
00033 extern "C" bool PrintVMMessages;
00034 extern "C" bool LogVMMessages;
00035 extern "C" bool AlwaysFlushVMMessages;
00036 extern "C" {
00037   void breakpoint();
00038   void error_breakpoint();
00039 }
00040 
00041 void lprintf_exit() {
00042   if (logFile) {
00043     fclose(logFile);
00044     logFile = NULL;
00045     unlink(fname);
00046   }
00047 }
00048 
00049 static void check_log_file() {
00050   if (LogVMMessages && !logFile) {
00051     os::move_file(SYSTEM_NAME ".log", SYSTEM_NAME ".log.old");
00052     logFile = fopen(SYSTEM_NAME ".log", "w");
00053   }
00054 }
00055 
00056 
00057 extern "C" void lprintf(char* m, ...) {
00058   char buf[1024];
00059   va_list ap;
00060   va_start(ap, m);
00061   vsprintf(buf, m, ap);
00062   va_end(ap);
00063 
00064   check_log_file();
00065   if (LogVMMessages) {
00066     fputs(buf, logFile);
00067     if (AlwaysFlushVMMessages) fflush(logFile);
00068   }
00069   if (PrintVMMessages) {
00070     fputs(buf, stdout);
00071     if (AlwaysFlushVMMessages) fflush(stdout);
00072   }
00073 }
00074 
00075 extern "C" void lputc(char c) {
00076   check_log_file();
00077   if (LogVMMessages) {
00078     fputc(c, logFile);
00079     if (AlwaysFlushVMMessages) fflush(logFile);
00080   }
00081   if (PrintVMMessages) {
00082     fputc(c, stdout);
00083     if (AlwaysFlushVMMessages) fflush(stdout);
00084   }
00085 }
00086 
00087 extern "C" void lputs(char* str) {
00088   check_log_file();
00089   if (LogVMMessages) {
00090     fputs(str, logFile);
00091     if (AlwaysFlushVMMessages) fflush(logFile);
00092   }
00093   if (PrintVMMessages) {
00094     fputs(str, stdout);
00095     if (AlwaysFlushVMMessages) fflush(stdout);
00096   }
00097 }
00098 
00099 extern "C" void error(char* format, ...) {
00100   std->print_cr("VM Error:");
00101   va_list ap;
00102   va_start(ap, format);
00103   std->vprint_cr(format, ap);
00104   va_end(ap);
00105   DEBUG_EXCEPTION
00106 }
00107 
00108 extern "C" void warning(char* format, ...) {
00109   std->print_cr("VM Warning:");
00110   va_list ap;
00111   va_start(ap, format);
00112   std->vprint_cr(format, ap);
00113   va_end(ap);
00114   if (BreakAtWarning) DEBUG_EXCEPTION;
00115 }
00116 
00117 extern "C" void compiler_warning(char* format, ...) {
00118   if (PrintCompilerWarnings) {
00119     std->print_cr("Compiler Warning:");
00120     va_list ap;
00121     va_start(ap, format);
00122     std->vprint_cr(format, ap);
00123     va_end(ap);
00124     if (BreakAtWarning) DEBUG_EXCEPTION;
00125   }
00126 }
00127 
00128 void flush_logFile() { if (logFile) fflush(logFile); }
00129 
00130 extern "C" void my_sprintf(char*& buf, const char* format, ...){
00131   // like sprintf, but updates the buf pointer so that subsequent
00132   // sprintfs append to the string
00133   va_list ap;
00134   va_start(ap, format);
00135   vsprintf(buf, format, ap);
00136   va_end(ap);
00137   buf += strlen(buf);
00138 }
00139 
00140 extern "C" void my_sprintf_len(char*& buf, const int len, const char* format, ...){
00141   char* oldbuf = buf;
00142   va_list ap;
00143   va_start(ap, format);
00144   vsprintf(buf, format, ap);
00145   va_end(ap);
00146   buf += strlen(buf);
00147   for ( ; buf < oldbuf + len; *buf++ = ' ') ;
00148   *buf = '\0';
00149 }
00150 

Generated on Mon Oct 9 13:37:14 2006 for Strongtalk VM by  doxygen 1.4.7