EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 /* 00004 00005 (c) Thales group, 2010 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; 00010 version 2 of the license. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: http://eodev.sourceforge.net 00022 00023 Authors: 00024 Johann Dréo <johann.dreo@thalesgroup.com> 00025 Caner Candan <caner.candan@thalesgroup.com> 00026 00027 */ 00028 00029 #ifdef _WIN32 00030 #include <io.h> 00031 #else // _WIN32 00032 #include <unistd.h> 00033 #endif // ! _WIN32 00034 00035 #include <fcntl.h> 00036 #include <cstdlib> 00037 #include <cstdio> // used to define EOF 00038 00039 #include <iostream> 00040 00041 #include "eoLogger.h" 00042 00043 void eoLogger::_init() 00044 { 00045 _standard_io_streams[&std::cout] = 1; 00046 _standard_io_streams[&std::clog] = 2; 00047 _standard_io_streams[&std::cerr] = 2; 00048 00049 // /!\ If you want to add a level dont forget to add it at the header file in the enumerator Levels 00050 00051 addLevel("quiet", eo::quiet); 00052 addLevel("errors", eo::errors); 00053 addLevel("warnings", eo::warnings); 00054 addLevel("progress", eo::progress); 00055 addLevel("logging", eo::logging); 00056 addLevel("debug", eo::debug); 00057 addLevel("xdebug", eo::xdebug); 00058 } 00059 00060 eoLogger::eoLogger() : 00061 std::ostream(&_obuf), 00062 00063 _verbose("quiet", "verbose", "Set the verbose level", 'v'), 00064 _printVerboseLevels(false, "print-verbose-levels", "Print verbose levels", 'l'), 00065 _output("", "output", "Redirect a standard output to a file", 'o'), 00066 00067 _selectedLevel(eo::progress), 00068 _contextLevel(eo::quiet), 00069 _fd(2), 00070 _obuf(_fd, _contextLevel, _selectedLevel) 00071 { 00072 _init(); 00073 } 00074 00075 eoLogger::eoLogger(eo::file file) : 00076 std::ostream(&_obuf), 00077 00078 _verbose("quiet", "verbose", "Set the verbose level", 'v'), 00079 _printVerboseLevels(false, "print-verbose-levels", "Print verbose levels", 'l'), 00080 _output("", "output", "Redirect a standard output to a file", 'o'), 00081 00082 _selectedLevel(eo::progress), 00083 _contextLevel(eo::quiet), 00084 _fd(2), 00085 _obuf(_fd, _contextLevel, _selectedLevel) 00086 { 00087 _init(); 00088 *this << file; 00089 } 00090 00091 eoLogger::~eoLogger() 00092 { 00093 if (_fd > 2) { ::close(_fd); } 00094 } 00095 00096 void eoLogger::_createParameters( eoParser& parser ) 00097 { 00098 //------------------------------------------------------------------ 00099 // we are saying to eoParser to create the parameters created above. 00100 //------------------------------------------------------------------ 00101 00102 std::string section("Logger"); 00103 parser.processParam(_verbose, section); 00104 parser.processParam(_printVerboseLevels, section); 00105 parser.processParam(_output, section); 00106 00107 //------------------------------------------------------------------ 00108 00109 00110 //------------------------------------------------------------------ 00111 // we're gonna redirect the log to the given filename if -o is used. 00112 //------------------------------------------------------------------ 00113 00114 if ( ! _output.value().empty() ) 00115 { 00116 eo::log << eo::file( _output.value() ); 00117 } 00118 00119 //------------------------------------------------------------------ 00120 00121 00122 //------------------------------------------------------------------ 00123 // we're gonna print the list of levels if -l parameter is used. 00124 //------------------------------------------------------------------ 00125 00126 if ( _printVerboseLevels.value() ) 00127 { 00128 eo::log.printLevels(); 00129 } 00130 00131 //------------------------------------------------------------------ 00132 } 00133 00134 std::string eoLogger::className() const 00135 { 00136 return ("eoLogger"); 00137 } 00138 00139 void eoLogger::addLevel(std::string name, eo::Levels level) 00140 { 00141 _levels[name] = level; 00142 _sortedLevels.push_back(name); 00143 } 00144 00145 void eoLogger::printLevels() const 00146 { 00147 std::cout << "Available verbose levels:" << std::endl; 00148 00149 for (std::vector<std::string>::const_iterator it = _sortedLevels.begin(), end = _sortedLevels.end(); 00150 it != end; ++it) 00151 { 00152 std::cout << "\t" << *it << std::endl; 00153 } 00154 00155 ::exit(0); 00156 } 00157 00158 eoLogger& operator<<(eoLogger& l, const eo::Levels lvl) 00159 { 00160 l._contextLevel = lvl; 00161 return l; 00162 } 00163 00164 eoLogger& operator<<(eoLogger& l, eo::file f) 00165 { 00166 l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); 00167 return l; 00168 } 00169 00170 eoLogger& operator<<(eoLogger& l, eo::setlevel v) 00171 { 00172 l._selectedLevel = (v._lvl < 0 ? l._levels[v._v] : v._lvl); 00173 return l; 00174 } 00175 00176 eoLogger& operator<<(eoLogger& l, std::ostream& os) 00177 { 00178 if (l._standard_io_streams.find(&os) != l._standard_io_streams.end()) 00179 { 00180 l._fd = l._standard_io_streams[&os]; 00181 } 00182 return l; 00183 } 00184 00185 eoLogger::outbuf::outbuf(const int& fd, 00186 const eo::Levels& contexlvl, 00187 const eo::Levels& selectedlvl) 00188 : _fd(fd), _contextLevel(contexlvl), _selectedLevel(selectedlvl) 00189 {} 00190 00191 int eoLogger::outbuf::overflow(int_type c) 00192 { 00193 if (_selectedLevel >= _contextLevel) 00194 { 00195 if (_fd >= 0 && c != EOF) 00196 { 00197 ::write(_fd, &c, 1); 00198 } 00199 } 00200 return c; 00201 } 00202 00203 namespace eo 00204 { 00205 file::file(const std::string f) 00206 : _f(f) 00207 {} 00208 00209 setlevel::setlevel(const std::string v) 00210 : _v(v), _lvl((Levels)-1) 00211 {} 00212 00213 setlevel::setlevel(const Levels lvl) 00214 : _v(std::string("")), _lvl(lvl) 00215 {} 00216 } 00217 00218 void make_verbose(eoParser& parser) 00219 { 00220 eo::log._createParameters( parser ); 00221 00222 eo::log << eo::setlevel(eo::log._verbose.value()); 00223 } 00224 00225 eoLogger eo::log;