Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Inform.C

Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003  *cr                                                                       
00004  *cr            (C) Copyright 1995-2011 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.38 $       $Date: 2011/11/20 00:49:49 $
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 
00034 #if defined(VMDTKCON)
00035 // XXX global instances of the Inform class
00036 Inform msgInfo("Info) ",    VMDCON_INFO);
00037 Inform msgWarn("Warning) ", VMDCON_WARN);
00038 Inform msgErr("ERROR) ",    VMDCON_ERROR);
00039 #else
00040 // XXX global instances of the Inform class
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); // force standard output to be flushed here, otherwise output
00053                   // from Inform, stdio, Tcl, and Python can be weirdly 
00054                   // buffered, resulting in jumbled output from batch runs
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  

Generated on Sat May 26 01:48:02 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002