00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _CJNP_LOGGABLEBASE_H_
00021 #define _CJNP_LOGGABLEBASE_H_ 1
00022
00023 #include <string>
00024 #include <fstream>
00025 #include <list>
00026 #include <memory>
00027 #include "CjNLTopology.hh"
00028 #include "CjMatrix.hh"
00029 #include "CjNetworkProcess.hh"
00030 #include "Cj_options_helper.hh"
00031 #include <boost/shared_ptr.hpp>
00032
00033 namespace jmitie {
00034
00035 class CjNP_LoggableBase : public CjNetworkProcess {
00036
00037 protected:
00038 std::string m_label;
00039 bool m_append_epoch;
00040 boost::shared_ptr<std::fstream> m_fout;
00041 std::string m_header;
00042
00043 public:
00044 CjNP_LoggableBase( const std::string & args, const ctor_va_t & var_arg ):CjNetworkProcess(args, var_arg), m_append_epoch(false) {}
00045 ~CjNP_LoggableBase() { }
00046
00047 static std::string getProperty_static(const std::string & opt) {
00048 if(opt == "usage") { return std::string("[append_epoch]\tAppends the epoch number after the label of each line.\n" \
00049 "[overwrite]\tspecifies whether to overwrite the file in case of output to file.\n" \
00050 "[label]\tThe label to print at the start of each output line.\n" \
00051 "[header]\tThis is printed above any information dumped.\n" \
00052 "[fn=filename]\tThe filename of the file to secnd output to. If not specified output will be sent to std out.\n");
00053 }
00054 throw std::invalid_argument("CjNP_LoggableBase::getProperty_static(" + opt + "): Unknown property requested.");
00055 }
00056
00057 void chompLogOptions( std::list< optval_t > & opts ) {
00058 std::list< optval_t > opts_ (opts);
00059 bool overwrite_found = false;
00060 bool append_epoch = false;
00061 std::string fname;
00062 std::string label;
00063 std::string header;
00064 std::auto_ptr<std::fstream> fout;
00065
00066 try {
00067 extractStringOption( "fn", opts_, fname );
00068 append_epoch = extractValuelessOption( "append_epoch", opts_ );
00069 overwrite_found = extractValuelessOption( "overwrite", opts_ );
00070 extractStringOption( "label", opts_, label );
00071 extractStringOption( "header", opts_, header );
00072 } catch (const Cj_OptionParseFail & e) { throw Cj_OptionParseFail(getName() + ": " + e.what()); }
00073
00074 if(overwrite_found && !fname.empty()) throw Cj_OptionParseFail("Cannot specify overwrite without filename in process " + getName());
00075 if(!fname.empty()) {
00076 std::ios_base::openmode mode = std::ios_base::out;
00077 if(!overwrite_found) mode |= std::ios_base::app;
00078 fout.reset( new std::fstream( fname.c_str(), mode ) );
00079 if(!fout->good()) throw Cj_OptionParseFail("In process " + getName() + ": Couldn't open file " + fname + " for " + (overwrite_found?"overwriting.":"appending."));
00080 }
00081
00082 m_fout.reset(fout.release());
00083 opts.swap( opts_ );
00084 std::swap( m_append_epoch, append_epoch);
00085 std::swap( m_label, label );
00086 std::swap( m_header, header );
00087 }
00088
00089 static std::string getLogOpts() { return "[fn=filename] [append_epoch] [label=text] [overwrite] [header=value]\n Where:\n\tfn=\tthe filename to output to, if not present output is cout\n\tlabel=\tthe text to prepend each output line\n\tappend_epoch\tappend the epoch number to the label\n\theader=\tText to print before any output"; }
00090 };
00091
00092 }
00093
00094 #endif // _CJNP_LOGGABLEBASE_H_