00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00034 #if defined(VMDTKCON)
00035
00036 Inform msgInfo("Info) ", VMDCON_INFO);
00037 Inform msgWarn("Warning) ", VMDCON_WARN);
00038 Inform msgErr("ERROR) ", VMDCON_ERROR);
00039 #else
00040
00041 Inform msgInfo("Info) ");
00042 Inform msgWarn("Warning) ");
00043 Inform msgErr("ERROR) ");
00044 #endif
00045
00046 Inform& sendmsg(Inform& inform) {
00047 Inform& rc = inform.send();
00048
00049 #if defined(VMDTKCON)
00050 vmdcon_purge();
00051 #else
00052 fflush(stdout);
00053
00054
00055 #endif
00056 return rc;
00057 }
00058
00059 Inform& ends(Inform& inform) { return inform; }
00060
00061 #if defined(VMDTKCON)
00062 Inform::Inform(const char *myname, int lvl) {
00063 name = strdup(myname);
00064 loglvl=lvl;
00065 muted=0;
00066 reset();
00067 }
00068 #else
00069 Inform::Inform(const char *myname) {
00070 name = strdup(myname);
00071 muted=0;
00072 reset();
00073 }
00074 #endif
00075
00076 Inform::~Inform() {
00077 free(name);
00078 }
00079
00080 Inform& Inform::send() {
00081 char *nlptr, *bufptr;
00082
00083 if (!muted) {
00084 bufptr = buf;
00085 if (!strchr(buf, '\n'))
00086 strcat(buf, "\n");
00087
00088 while ((nlptr = strchr(bufptr, '\n'))) {
00089 *nlptr = '\0';
00090 #if defined(VMDTKCON)
00091 vmdcon_append(loglvl, name, -1);
00092 vmdcon_append(loglvl, bufptr, -1);
00093 vmdcon_append(loglvl, "\n", 1);
00094 #else
00095 printf("%s%s\n", name, bufptr);
00096 #endif
00097 bufptr = nlptr + 1;
00098 }
00099 }
00100
00101 buf[0] = '\0';
00102 return *this;
00103 }
00104
00105 Inform& Inform::reset() {
00106 memset(buf, 0, sizeof(buf));
00107 memset(tmpbuf, 0, sizeof(tmpbuf));
00108 return *this;
00109 }
00110
00111 Inform& Inform::operator<<(const char *s) {
00112 strncat(buf, s, MAX_MSG_SIZE - strlen(buf));
00113 return *this;
00114 }
00115
00116 Inform& Inform::operator<<(char c) {
00117 tmpbuf[0] = c;
00118 tmpbuf[1] = '\0';
00119 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00120 return *this;
00121 }
00122
00123 Inform& Inform::operator<<(int i) {
00124 sprintf(tmpbuf, "%d", i);
00125 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00126 return *this;
00127 }
00128
00129 Inform& Inform::operator<<(long i) {
00130 sprintf(tmpbuf, "%ld", i);
00131 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00132 return *this;
00133 }
00134
00135 Inform& Inform::operator<<(unsigned long u) {
00136 sprintf(tmpbuf, "%ld", u);
00137 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00138 return *this;
00139 }
00140
00141 Inform& Inform::operator<<(double d) {
00142 sprintf(tmpbuf, "%f", d);
00143 strncat(buf, tmpbuf, MAX_MSG_SIZE - strlen(buf));
00144 return *this;
00145 }
00146
00147 Inform& Inform::operator<<(Inform& (*f)(Inform &)) {
00148 return f(*this);
00149 }
00150
00151 #ifdef TEST_INFORM
00152
00153 int main() {
00154 msgInfo << "1\n";
00155 msgInfo << "12\n";
00156 msgInfo << "123\n";
00157 msgInfo << sendmsg;
00158 msgInfo << "6789";
00159 msgInfo << sendmsg;
00160 return 0;
00161 }
00162
00163 #endif
00164