Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Examples

logfile.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 #ifndef LOGFILE_H 00003 #define LOGFILE_H 00004 00005 #include <cstdio> 00006 #include <unistd.h> 00007 #include <map> 00008 #include <list> 00009 #include <string> 00010 #include <ctime> 00011 #include <iostream> 00012 #include <sstream> 00013 00015 00022 template <class T> class LogFile{ 00026 protected: 00027 FILE* f; 00028 typedef std::map<T, double> data_map; 00029 typedef std::list<T> names_list; 00030 data_map data; 00031 names_list names; 00032 std::string filename; 00033 std::string info; 00034 std::string separator; 00035 int line_counter; 00036 bool _print_names_list; 00037 public: 00038 LogFile(const std::string& filename="",const std::string& info="",const std::string& separator="\t") 00039 :filename(filename),info(info),separator(separator),line_counter(0),_print_names_list(false){ 00040 f=NULL; 00041 }; 00042 ~LogFile(){ 00043 if(f)fclose(f); 00044 }; 00045 void setInfo(const std::string& info){ 00046 this->info=info; 00047 }; 00048 void addChannel(const T& name){ 00049 names.push_back(name); 00050 }; 00051 void putData(const T& channel, double value){ 00052 data[channel]=value; 00053 }; 00057 void comment(const std::string& text){ 00058 init(); 00059 fprintf(f,"#%d: %s\n",line_counter,text.data()); 00060 fflush(f); 00061 }; 00062 00065 void print_names_list(){ 00066 _print_names_list=true; 00067 }; 00072 void print(){ 00073 if(names.empty())return; 00074 init(); 00075 bool first=true; 00076 for(typename names_list::iterator i=names.begin();i!=names.end();++i){ 00077 if(first) first=false; 00078 else fprintf(f,"%s",separator.data()); 00079 fprintf(f,"%f", data[(*i)]); 00080 } 00081 fprintf(f,"\n"); 00082 fflush(f); 00083 line_counter++; 00084 }; 00085 protected: 00086 void init(){ 00087 if(f!=NULL)return; 00088 00089 time_t t=time(NULL); 00090 if(filename!="") 00091 f=fopen(filename.data(),"w"); 00092 else { 00093 char* time_str=ctime(&t); 00094 if(time_str[strlen(time_str)-1]='\n') 00095 time_str[strlen(time_str)-1]='\0'; 00096 f=fopen(time_str,"w"); 00097 } 00098 00099 bool first=true; 00100 // print info 00101 fprintf(f,"# created at %s",ctime(&t)); 00102 fprintf(f,"#\n"); 00103 fprintf(f,"#"); // insert comment char 00104 for(typename std::string::iterator s=info.begin();s!=info.end();++s){ 00105 fprintf(f,"%c",*s); 00106 if(*s == '\n') // line ending found 00107 fprintf(f,"#"); // insert comment char 00108 } 00109 fprintf(f,"#\n"); 00110 00111 // collect names 00112 std::ostringstream headline_str; 00113 std::ostringstream names_str; 00114 int counter=1; 00115 names_str << "# list of channels\n#\n"; 00116 for(typename names_list::iterator i=names.begin();i!=names.end();++i,counter++){ 00117 if(first) { 00118 first=false; 00119 headline_str << "#"; 00120 } else 00121 headline_str << separator; 00122 00123 headline_str << (*i); 00124 names_str << "# " << counter << ": " << (*i) << std::endl; 00125 } 00126 names_str << "#\n" << std::ends; 00127 headline_str << std::endl << std::ends; 00128 // print names list 00129 if(_print_names_list) 00130 fprintf(f,"%s",names_str.str().data()); 00131 // print column headers 00132 fprintf(f,"%s",headline_str.str().data()); 00133 } 00134 00135 }; 00136 00137 #endif 00138

Generated on Wed Apr 6 10:22:15 2005 for Gnuplot by doxygen 1.3.8