slogcxx-nlog.h

Go to the documentation of this file.
00001 // -*- c++ -*- Tell emacs this is c++
00003 // Copyright (c) 2006 Kurt Schwehr
00004 //      Data Visualization Research Lab,
00005 //      Center for Coastal and Ocean Mapping
00006 //      University of New Hampshire.
00007 //      http://ccom.unh.edu
00008 //
00013 
00014 
00015 // WARNING: do not include this file directly... Define NLOG on slogcxx.h instead.
00016 
00018 // MACROS 
00020 
00021 // FIX: Put a NOP in the stream?
00022 //#define WHERE Where(__FILE__,__LINE__,__FUNCTION__)
00023 // Patterned after assert.h.  Will this work?
00024 #undef WHERE
00025 //#define WHERE ((void)0)
00026 #define WHERE (0)
00027 
00029 // Where class
00031 
00032 // class Where {
00033 //  public: 
00034 //     Where(UNUSED const std::string &_file, UNUSED const int &_lineno, UNUSED const std::string &_function) {};
00035 //     inline std::string getFile() const {return std::string("unknown file");};
00036 //     inline int getLineno() const {return -1;};
00037 //     inline std::string getFunction() const {return std::string("unknown function");};
00038 // };
00039 
00040 class Where {
00041  public: 
00042     Where(UNUSED const std::string &_file, UNUSED const int &_lineno, UNUSED const std::string &_function) {}
00043     inline std::string const &getFile() const {
00044         static const std::string unknownFile("unknown file");
00045         return unknownFile;
00046     }
00047     inline int const &getLineno() const {
00048         static const int unknownLine=-1;
00049         return unknownLine;
00050     }
00051     inline std::string const &getFunction() const {
00052         static const std::string unknownFunction("unknown function");
00053         return unknownFunction;
00054     }
00055 };
00056 
00057 
00058 
00060 // The main Slog class
00062 
00063 // FIX: probably needs help optimizing this down
00064 class Slog {
00065 public:
00066     Slog(UNUSED const std::string &filename="", const std::string &indentStr=" ", 
00067          UNUSED const bool append=true, const bool enableXml=true, const bool enableTime=true)
00068         : logLevel(1), msgLevel(1), curStr(""),
00069             xmlEnabled(enableXml), timeEnabled(enableTime)
00070             ,stateIndent(indentStr)
00071     {}
00072     ~Slog() {}
00073 
00074     void setLevel(const int lvl) {logLevel=lvl;} 
00075     int getLevel() {return logLevel;}
00076     int inc() {return ++logLevel;}
00077     int dec() {--logLevel; if (0>logLevel) logLevel=0; return logLevel;}
00078     void enableTime() {timeEnabled=true;};
00079     void disableTime() {timeEnabled=false;};
00080     bool getTimeStatus() {return timeEnabled;};
00081     void enableXml() {xmlEnabled=true;}; 
00082     void disableXml() {xmlEnabled=false;};  
00083     bool getXmlStatus() {return xmlEnabled;};
00084     bool entry(UNUSED const int lvl, UNUSED const std::string str) {return true;}
00085 
00086     bool where(UNUSED const std::string &file, UNUSED const int lineno, UNUSED const std::string &function) {return true;}
00087 
00088     void setMsgLevel(const int lvl) {assert(0<=lvl);msgLevel=lvl;}
00089     int getMsgLevel() {return msgLevel;}
00090     int incMsg() {return ++msgLevel;}
00091     int decMsg() {--msgLevel; if (0>msgLevel) msgLevel=0; return msgLevel;}
00092 
00093     bool partial(UNUSED const int lvl, UNUSED const std::string str) {return true;} 
00094     bool complete() {return true;}
00095 
00096     void setStateIndent(const std::string &str) {stateIndent=str;};
00097     std::string getStateIndent() {return stateIndent;};
00098     std::string indent();
00099     std::string getStateNumberStr();
00100     std::string getCurScope() {if (0==getStateDepth()) return ""; return stateStack[stateStack.size()-1];}
00101 
00102     void pushState(std::string scope, int msgLvl = -1) {
00103         stateStack.push_back(scope);
00104         if (msgLvl != -1) {
00105             msgLvlStack.push_back(getMsgLevel());
00106             setMsgLevel(msgLvl);
00107         }  else { msgLvlStack.push_back(-1); }
00108     }
00109     std::string popState() {
00110         std::string s=stateStack[stateStack.size()-1];
00111         int ml = msgLvlStack[msgLvlStack.size()-1];
00112         if (ml != -1) setMsgLevel(ml);
00113         stateStack.pop_back();
00114         msgLvlStack.pop_back();
00115         return s;
00116     }
00117     void writeState(UNUSED bool flat=true) {}
00118     int getStateDepth() {return stateStack.size();};
00119 
00120     Slog& operator=(UNUSED const Slog& rhs) {
00121         std::cerr << "Slog op=!" << std::endl;
00122         return *this;
00123     }
00124 
00125 private:
00126     int logLevel;
00127     int msgLevel;
00128     std::string curStr;
00129     bool xmlEnabled;
00130     bool timeEnabled;
00131 
00132     std::string stateIndent;
00133     std::vector<std::string> stateStack;
00134     std::vector<int> msgLvlStack;
00135 }; // end Slog class
00136 
00137 
00138 inline Slog& operator<<(Slog&s, UNUSED Slog&(*manip)(Slog&)) {return s;}
00139 
00140 inline Slog& endl(Slog& s) {return s;}
00141 inline Slog& decl(Slog& s) {return s;}
00142 inline Slog& incl(Slog& s) {return s;}
00143 
00144 inline Slog& operator<< (Slog &s, UNUSED const int &r){return s;} 
00145 inline Slog& operator<< (Slog &s, UNUSED const size_t &r){return s;} 
00146 inline Slog& operator<< (Slog &s, UNUSED const char *str){return s;}
00147 inline Slog& operator<< (Slog &s, UNUSED const std::string &str){return s;}
00148 
00149 inline Slog& operator<< (Slog &s, UNUSED const char &c){return s;} 
00150 inline Slog& operator<< (Slog &s, UNUSED const short &sh){return s;}
00151 inline Slog& operator<< (Slog &s, UNUSED const long &l){return s;} 
00152 inline Slog& operator<< (Slog &s, UNUSED const float &f){return s;} 
00153 inline Slog& operator<< (Slog &s, UNUSED const double &d){return s;}
00154 
00155 inline Slog& operator<< (Slog &s, UNUSED const Where &w){return s;}
00156 
00157 
00158 class LogState {
00159 public:
00160     LogState(Slog *logInstance, const std::string &scope, int msgLvl = -1): log(logInstance), popped(false) {log->pushState(scope,msgLvl);}
00161     inline std::string pop() {  if (popped) return "";  popped=true; return log->popState();}
00162     ~LogState() {}
00163 private:
00164     Slog *log;
00165     bool popped;
00166 };

Generated on Tue Aug 15 10:09:38 2006 by  doxygen 1.4.6