00001 00002 /*************************************************************************** 00003 *cr 00004 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00005 *cr University of Illinois 00006 *cr All Rights Reserved 00007 *cr 00008 ***************************************************************************/ 00009 00010 /*************************************************************************** 00011 * RCS INFORMATION: 00012 * 00013 * $RCSfile: Inform.C,v $ 00014 * $Author: johns $ $Locker: $ $State: Exp $ 00015 * $Revision: 1.46 $ $Date: 2020年10月22日 03:40:41 $ 00016 * 00017 *************************************************************************** 00018 * DESCRIPTION: 00019 * 00020 * Inform - takes messages and displays them to the given ostream. 00021 * 00022 ***************************************************************************/ 00023 00024 #include "Inform.h" 00025 #include <stdlib.h> 00026 #include <string.h> 00027 #include <stdio.h> 00028 00029 #include "config.h" 00030 #if defined(VMDTKCON) 00031 #include "vmdconsole.h" 00032 #endif 00033 #ifdef ANDROID 00034 #include "androidvmdstart.h" 00035 #endif 00036 00037 #if defined(VMDTKCON) 00038 // XXX global instances of the Inform class 00039 Inform msgInfo("Info) ", VMDCON_INFO); 00040 Inform msgWarn("Warning) ", VMDCON_WARN); 00041 Inform msgErr("ERROR) ", VMDCON_ERROR); 00042 #else 00043 // XXX global instances of the Inform class 00044 Inform msgInfo("Info) "); 00045 Inform msgWarn("Warning) "); 00046 Inform msgErr("ERROR) "); 00047 #endif 00048 00049 Inform& sendmsg(Inform& inform) { 00050 Inform& rc = inform.send(); 00051 00052 #if defined(VMDTKCON) 00053 vmdcon_purge(); 00054 #else 00055 fflush(stdout); // force standard output to be flushed here, otherwise output 00056 // from Inform, stdio, Tcl, and Python can be weirdly 00057 // buffered, resulting in jumbled output from batch runs 00058 #endif 00059 return rc; 00060 } 00061 00062 Inform& ends(Inform& inform) { return inform; } 00063 00064 #if defined(VMDTKCON) 00065 Inform::Inform(const char *myname, int lvl) { 00066 name = strdup(myname); 00067 loglvl=lvl; 00068 muted=0; 00069 reset(); 00070 } 00071 #else 00072 Inform::Inform(const char *myname) { 00073 name = strdup(myname); 00074 muted=0; 00075 reset(); 00076 } 00077 #endif 00078 00079 Inform::~Inform() { 00080 free(name); 00081 } 00082 00083 Inform& Inform::send() { 00084 char *nlptr, *bufptr; 00085 00086 if (!muted) { 00087 bufptr = buf; 00088 if (!strchr(buf, '\n')) 00089 strcat(buf, "\n"); 00090 00091 while ((nlptr = strchr(bufptr, '\n'))) { 00092 *nlptr = '0円'; 00093 #if defined(VMDTKCON) 00094 vmdcon_append(loglvl, name, -1); 00095 vmdcon_append(loglvl, bufptr, -1); 00096 vmdcon_append(loglvl, "\n", 1); 00097 #else 00098 #ifdef ANDROID 00099 log_android(name, bufptr); 00100 #else 00101 printf("%s%s\n", name, bufptr); 00102 #endif 00103 #endif 00104 bufptr = nlptr + 1; 00105 } 00106 } 00107 00108 buf[0] = '0円'; 00109 return *this; 00110 } 00111 00112 Inform& Inform::reset() { 00113 memset(buf, 0, sizeof(buf)); 00114 return *this; 00115 } 00116 00117 Inform& Inform::operator<<(const char *s) { 00118 strncat(buf, s, MAX_MSG_SIZE - strlen(buf)); 00119 return *this; 00120 } 00121 00122 Inform& Inform::operator<<(char c) { 00123 char tmpbuf[4] = { 0 }; 00124 tmpbuf[0] = c; 00125 tmpbuf[1] = '0円'; 00126 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00127 return *this; 00128 } 00129 00130 Inform& Inform::operator<<(int i) { 00131 char tmpbuf[32] = { 0 }; 00132 sprintf(tmpbuf, "%d", i); 00133 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00134 return *this; 00135 } 00136 00137 Inform& Inform::operator<<(unsigned int i) { 00138 char tmpbuf[32] = { 0 }; 00139 sprintf(tmpbuf, "%d", i); 00140 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00141 return *this; 00142 } 00143 00144 Inform& Inform::operator<<(long i) { 00145 char tmpbuf[128] = { 0 }; 00146 sprintf(tmpbuf, "%ld", i); 00147 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00148 return *this; 00149 } 00150 00151 Inform& Inform::operator<<(unsigned long u) { 00152 char tmpbuf[128] = { 0 }; 00153 sprintf(tmpbuf, "%ld", u); 00154 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00155 return *this; 00156 } 00157 00158 #if defined(_WIN64) 00159 // LLP64 platforms have to separately support ptrdiff_t and size_t, 00160 // since they are aliased to "long long" or other types rather than "long" 00161 Inform& Inform::operator<<(ptrdiff_t i) { 00162 char tmpbuf[128] = { 0 }; 00163 sprintf(tmpbuf, "%td", i); 00164 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00165 return *this; 00166 } 00167 00168 Inform& Inform::operator<<(size_t u) { 00169 char tmpbuf[128] = { 0 }; 00170 sprintf(tmpbuf, "%td", u); 00171 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00172 return *this; 00173 } 00174 #endif 00175 00176 Inform& Inform::operator<<(double d) { 00177 char tmpbuf[128] = { 0 }; 00178 sprintf(tmpbuf, "%f", d); 00179 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf)); 00180 return *this; 00181 } 00182 00183 Inform& Inform::operator<<(Inform& (*f)(Inform &)) { 00184 return f(*this); 00185 } 00186 00187 #ifdef TEST_INFORM 00188 00189 int main() { 00190 msgInfo << "1\n"; 00191 msgInfo << "12\n"; 00192 msgInfo << "123\n"; 00193 msgInfo << sendmsg; 00194 msgInfo << "6789"; 00195 msgInfo << sendmsg; 00196 return 0; 00197 } 00198 00199 #endif 00200