QSapecNG
 All Classes Functions Enumerations Properties
logger.h
00001 /*
00002     SapecNG - Next Generation Symbolic Analysis Program for Electric Circuit
00003     Copyright (C) 2009, Michele Caini
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef LOGGER_H
00021 #define LOGGER_H
00022 
00023 
00024 #include "logger/logpolicy.h"
00025 
00026 #include <streambuf>
00027 #include <ostream>
00028 
00029 #include <sstream>
00030 
00031 
00032 namespace sapecng
00033 {
00034 
00035 
00036 /*
00037  * begin: google docet
00038  */
00039 
00040 template <class cT, class traits = std::char_traits<cT> >
00041 class basic_nullbuf: public std::basic_streambuf<cT, traits>
00042 {
00043 
00044   typename traits::int_type overflow(typename traits::int_type c)
00045   {
00046     return traits::not_eof(c);
00047   }
00048 
00049 };
00050 
00051 template <class cT, class traits = std::char_traits<cT> >
00052 class basic_onullstream: public std::basic_ostream<cT, traits>
00053 {
00054 
00055 public:
00056   basic_onullstream():
00057     std::basic_ios<cT, traits>(&m_sbuf),
00058     std::basic_ostream<cT, traits>(&m_sbuf)
00059   {
00060     init(&m_sbuf);
00061   }
00062 
00063 private:
00064   basic_nullbuf<cT, traits> m_sbuf;
00065 
00066 };
00067 
00068 typedef basic_onullstream<char> onullstream;
00069 typedef basic_onullstream<wchar_t> wonullstream; 
00070 
00071 /*
00072  * end: google docet
00073  */
00074 
00075 
00076 class LogPolicy;
00077 
00078 
00079 class Logger
00080 {
00081 
00082 public:
00083   Logger() { }
00084   virtual ~Logger();
00085 
00086   enum LogLevel {
00087     DEBUG = 0,
00088     INFO,
00089     WARNING,
00090     ERROR,
00091     FATAL
00092   };
00093   
00094   virtual std::ostream& get(LogLevel level = INFO);
00095 
00096   static LogLevel level() { return level_; }
00097   static void setLevel(LogLevel level) { level_ = level; }
00098   static LogPolicy* policy() { return policy_; }
00099   static void setPolicy(LogPolicy* policy) { policy_ = policy; }
00100 
00101 private:
00102   Logger(const Logger& logger);
00103   Logger& operator=(const Logger& logger);
00104 
00105   std::ostringstream os_;
00106   onullstream null_;
00107 
00108   static LogLevel level_;
00109   static LogPolicy* policy_;
00110 
00111 };
00112 
00113 
00114 }
00115 
00116 
00117 #endif // LOGGER_H
 All Classes Functions Enumerations Properties