diff --git a/crlcore/etc/tools.configuration.xml b/crlcore/etc/tools.configuration.xml index 5c319d0a..be86f6ca 100644 --- a/crlcore/etc/tools.configuration.xml +++ b/crlcore/etc/tools.configuration.xml @@ -1,5 +1,12 @@ + + + + + + + @@ -74,6 +81,16 @@ + + + + + + + + + + diff --git a/crlcore/src/ccore/Utilities.cpp b/crlcore/src/ccore/Utilities.cpp index 0025b8bd..26f8ee19 100644 --- a/crlcore/src/ccore/Utilities.cpp +++ b/crlcore/src/ccore/Utilities.cpp @@ -29,9 +29,72 @@ #include #include +#include +namespace boptions = boost::program_options; + +#include "hurricane/Warning.h" +#include "vlsisapd/configuration/Configuration.h" #include "crlcore/Utilities.h" +namespace { + + using namespace Hurricane; + using namespace CRL; + + + void verboseLevel1Changed ( Cfg::Parameter* p ) + { + if ( p->asBool() ) mstream::enable ( mstream::Verbose1 ); + else mstream::disable ( mstream::Verbose1 ); + + //cerr << "Verbose Level 1: " << boolalpha << p->asBool() << endl; + } + + + void verboseLevel2Changed ( Cfg::Parameter* p ) + { + if ( p->asBool() ) mstream::enable ( mstream::Verbose2 ); + else mstream::disable ( mstream::Verbose2 ); + } + + + void infoChanged ( Cfg::Parameter* p ) + { + if ( p->asBool() ) mstream::enable ( mstream::Info ); + else mstream::disable ( mstream::Info ); + } + + + void catchCoreChanged ( Cfg::Parameter* p ) + { + System::setCatchCore ( p->asBool() ); + } + + + void logModeChanged ( Cfg::Parameter* p ) + { + if ( p->asBool() ) tty::enable (); + else tty::disable (); + } + + + void traceLevelChanged ( Cfg::Parameter* p ) + { + ltracelevel ( p->asInt() ); + } + + + std::string environmentMapper ( std::string environmentName ) + { + if ( environmentName == "CORIOLIS_TOP" ) return "coriolis_top"; + return ""; + } + + +} // End of anonymous namespace. + + int tty::_width = 80; bool tty::_enabled = true; unsigned int mstream::_activeMask = mstream::Verbose0; @@ -132,41 +195,113 @@ namespace CRL { const char* BadColorValue = "%s() :\n\n" " Invalid color value for color \"%s\" : \"%s\".\n"; - System* System::_singleton = System::create (); - - // ------------------------------------------------------------------- // Class : "CRL::System". - System *System::create () - { - if ( _singleton != NULL ) { - cerr << "[WARNING] Attempt to re-create System() singleton." << endl; - return _singleton; - } + System* System::_singleton = System::get(); - _singleton = new System (); + + System::System () + : _catchCore(true) + { + // Immediate setup to avoid some tiresome looping... + _singleton = this; // Set the trap function for the SIGINT signal (CTRL-C). //if ( signal(SIGINT,System::TrapSig) == SIG_ERR ) // System::TrapSig ( SIGTFLT ); // Set the trap function for SIGFPE, SIGBUS, SIGABRT and SIGSEGV signals. - if ( ( signal(SIGFPE , System::trapSig) == SIG_ERR ) - || ( signal(SIGBUS , System::trapSig) == SIG_ERR ) - || ( signal(SIGABRT, System::trapSig) == SIG_ERR ) - || ( signal(SIGPIPE, System::trapSig) == SIG_ERR ) - || ( signal(SIGSEGV, System::trapSig) == SIG_ERR ) ) - System::trapSig ( SIGTFLT ); + if ( ( signal(SIGFPE , System::_trapSig) == SIG_ERR ) + || ( signal(SIGBUS , System::_trapSig) == SIG_ERR ) + || ( signal(SIGABRT, System::_trapSig) == SIG_ERR ) + || ( signal(SIGPIPE, System::_trapSig) == SIG_ERR ) + || ( signal(SIGSEGV, System::_trapSig) == SIG_ERR ) ) + System::_trapSig ( SIGTFLT ); + + // Environment variables reading. + boptions::options_description options ("Environment Variables"); + options.add_options() + ( "coriolis_top", boptions::value() + , "The root directory of the Coriolis installation tree." ); + + boptions::variables_map arguments; + boptions::store ( boptions::parse_environment(options,environmentMapper), arguments ); + boptions::notify ( arguments ); + + bfs::path::default_name_check ( bfs::portable_posix_name ); + + const string strSysConfDir = SYS_CONF_DIR; + bfs::path sysConfDir ( strSysConfDir ); + if ( not sysConfDir.has_root_path() ) { + if ( arguments.count("coriolis_top") ) { + sysConfDir = arguments["coriolis_top"].as() / sysConfDir; + } else { + cerr << Error("Environment variable CORIOLIS_TOP not set," + " may be unable to read configuration...") << endl; + } + } + sysConfDir /= "coriolis2"; + _pathes.insert ( make_pair("etc",sysConfDir) ); + + // Default configuration loading. + Cfg::Configuration* conf = Cfg::Configuration::get (); + + Cfg::getParamBool("misc.catchCore" ,true )->registerCb ( catchCoreChanged ); + Cfg::getParamBool("misc.verboseLevel1",true )->registerCb ( verboseLevel1Changed ); + Cfg::getParamBool("misc.verboseLevel2",true )->registerCb ( verboseLevel2Changed ); + Cfg::getParamBool("misc.info" ,false)->registerCb ( infoChanged ); + Cfg::getParamBool("misc.logMode" ,true )->registerCb ( logModeChanged ); + Cfg::getParamInt ("misc.traceLevel" ,1000 )->registerCb ( traceLevelChanged ); + + // Immediate update from the configuration. + catchCoreChanged ( Cfg::getParamBool("misc.catchCore" ) ); + verboseLevel1Changed ( Cfg::getParamBool("misc.verboseLevel1") ); + verboseLevel2Changed ( Cfg::getParamBool("misc.verboseLevel2") ); + infoChanged ( Cfg::getParamBool("misc.info" ) ); + logModeChanged ( Cfg::getParamBool("misc.logMode" ) ); + traceLevelChanged ( Cfg::getParamInt ("misc.traceLevel" ) ); + + bool systemConfFound = false; + bfs::path systemConfFile = sysConfDir / "tools.configuration.xml"; + if ( bfs::exists(systemConfFile) ) { + systemConfFound = true; + conf->readFromFile ( systemConfFile.string() ); + } else { + cerr << Warning("System configuration file:\n <%s> not found." + ,systemConfFile.string().c_str()) << endl; + } + + bool dotConfFound = false; + bfs::path dotConfFile = "./.coriolis2.configuration.xml"; + if ( bfs::exists(dotConfFile) ) { + dotConfFound = true; + conf->readFromFile ( dotConfFile.string() ); + } + + // Delayed printing, as we known only now whether VerboseLevel1 is requested. + if ( cmess1.enabled() ) { + cmess1 << " o Reading Configuration. " << endl; + if (systemConfFound) cmess1 << " - <" << systemConfFile.string() << ">." << endl; + if (dotConfFound) cmess1 << " - <" << dotConfFile.string() << ">." << endl; + } + } + + + System *System::get () + { + if ( _singleton == NULL ) { + _singleton = new System (); + } return _singleton; } - void System::trapSig ( int sig ) + void System::_trapSig ( int sig ) { - cerr << "\n\n[CRL ERROR] System::trapSig():\n" << endl; + cerr << "\n\n[CRL ERROR] System::_trapSig():\n" << endl; switch ( sig ) { case SIGINT: @@ -193,7 +328,7 @@ namespace CRL { cerr << "\n Please e-mail to .\n" << "\n program terminated "; - if ( getSystem()->getCatchCore() ) { + if ( getCatchCore() ) { cerr << "(core not dumped).\n"; exit ( 1 ); } else { @@ -221,7 +356,20 @@ namespace CRL { } - System *System::getSystem () { return _singleton; } + const bfs::path& System::_getPath ( const string& key ) + { + static bfs::path nullPath ("no_path"); + + cerr << "Looking up: " << key << endl; + + map::const_iterator ipath = _pathes.find ( key ); + if ( ipath == _pathes.end() ) return nullPath; + + cerr << "Successfull lookup: "; cerr.flush(); + cerr << (*ipath).second.string() << endl; + + return (*ipath).second; + } // ------------------------------------------------------------------- @@ -299,5 +447,5 @@ namespace CRL { // Class : "mstream". - void mstream::enable ( unsigned int mask ) { _activeMask |= mask; } - void mstream::disable ( unsigned int mask ) { _activeMask &= ~mask; } +void mstream::enable ( unsigned int mask ) { _activeMask |= mask; } +void mstream::disable ( unsigned int mask ) { _activeMask &= ~mask; } diff --git a/crlcore/src/ccore/crlcore/Utilities.h b/crlcore/src/ccore/crlcore/Utilities.h index 88262f29..c7072ef1 100644 --- a/crlcore/src/ccore/crlcore/Utilities.h +++ b/crlcore/src/ccore/crlcore/Utilities.h @@ -33,6 +33,9 @@ #include #include +#include +namespace bfs = boost::filesystem; + #include "hurricane/Commons.h" #include "hurricane/Error.h" #include "hurricane/Slot.h" @@ -55,26 +58,30 @@ namespace CRL { class System { public: - // Constructor & Destructor. - static System* create (); - // Methods. - static System* getSystem (); - static void trapSig ( int sig ); - inline bool getCatchCore (); - inline bool setCatchCore ( bool catchCore ); - + static System* get (); + static inline bool getCatchCore (); + static inline const bfs::path& getPath ( const std::string& ); + static inline bool setCatchCore ( bool catchCore ); private: - // Internal: Attributes. - static System* _singleton; - bool _catchCore; - - // Constructors & Destructors. - inline System (); - System ( const System &other ); - System& operator= ( const System &other ); + static System* _singleton; + bool _catchCore; + std::map _pathes; + private: + System (); + System ( const System &other ); + System& operator= ( const System &other ); + static void _trapSig ( int sig ); + inline bool _getCatchCore (); + const bfs::path& _getPath ( const std::string& ); + inline bool _setCatchCore ( bool catchCore ); }; + inline bool System::getCatchCore () { return get()->_getCatchCore(); } + inline const bfs::path& System::getPath ( const std::string& key ) { return get()->_getPath(key); } + inline bool System::setCatchCore ( bool catchCore ) { return get()->_setCatchCore(catchCore); } + + // ------------------------------------------------------------------- // Class : "CRL::IoFile ()". // @@ -123,21 +130,20 @@ namespace CRL { // Inline Methods. - inline System::System (): _catchCore(true) { } - inline bool System::getCatchCore () { return _catchCore; } - inline bool System::setCatchCore ( bool catchCore ) { return _catchCore = catchCore; } + inline bool System::_getCatchCore () { return _catchCore; } + inline bool System::_setCatchCore ( bool catchCore ) { return _catchCore = catchCore; } - inline IoFile::IoFile ( string path ): _file(NULL) - , _path(path) - , _mode("") - , _lineNumber(0) - , _eof(false) {} - inline bool IoFile::isOpen () const { return _file!=NULL; } - inline bool IoFile::eof () const { return _eof; } - inline FILE* IoFile::getFile () { return _file; } - inline size_t IoFile::getLineNumber () const { return _lineNumber; } - inline void IoFile::rewind () { if (_file) std::rewind(_file); _lineNumber=0; } - inline string IoFile::_getTypeName () const { return _TName("IoFile"); } + inline IoFile::IoFile ( string path ): _file(NULL) + , _path(path) + , _mode("") + , _lineNumber(0) + , _eof(false) {} + inline bool IoFile::isOpen () const { return _file!=NULL; } + inline bool IoFile::eof () const { return _eof; } + inline FILE* IoFile::getFile () { return _file; } + inline size_t IoFile::getLineNumber () const { return _lineNumber; } + inline void IoFile::rewind () { if (_file) std::rewind(_file); _lineNumber=0; } + inline string IoFile::_getTypeName () const { return _TName("IoFile"); } // ------------------------------------------------------------------- @@ -269,11 +275,12 @@ inline std::string tty::bgcolor ( unsigned int mask ) , VerboseLevel2 = Verbose0|Verbose1|Verbose2 }; public: - static void enable ( unsigned int mask ); - static void disable ( unsigned int mask ); - inline mstream ( unsigned int mask, std::ostream &s ); - inline unsigned int getStreamMask() const; - inline bool enabled () const; + static void enable ( unsigned int mask ); + static void disable ( unsigned int mask ); + inline mstream ( unsigned int mask, std::ostream &s ); + inline unsigned int getStreamMask() const; + static inline unsigned int getActiveMask(); + inline bool enabled () const; // Overload for formatted outputs. template inline mstream& operator<< ( T& t ); template inline mstream& operator<< ( T* t ); @@ -292,6 +299,7 @@ inline std::string tty::bgcolor ( unsigned int mask ) inline mstream::mstream ( unsigned int mask, std::ostream& s ): std::ostream(s.rdbuf()) , _streamMask(mask) {} inline unsigned int mstream::getStreamMask() const { return _streamMask; } + inline unsigned int mstream::getActiveMask() { return _activeMask; } inline bool mstream::enabled () const { return (_streamMask & _activeMask); } inline mstream& mstream::flush () { if (enabled()) static_cast(this)->flush(); return *this; } inline mstream& mstream::operator<< ( std::ostream& (*pf)(std::ostream&) ) { if (enabled()) (*pf)(*this); return *this; } @@ -377,13 +385,13 @@ class Dots { class linefill : public std::ostream { public: - inline linefill ( const std::string& header, std::ostream &s ); + inline linefill ( const std::string& header, mstream &s ); // Overload for formatted outputs. template inline linefill& operator<< ( T& t ); template inline linefill& operator<< ( T* t ); template inline linefill& operator<< ( const T& t ); template inline linefill& operator<< ( const T* t ); - inline std::ostream& base (); + inline mstream& base (); inline void _print ( const std::string& field ); inline linefill& flush (); inline linefill& reset (); @@ -392,17 +400,19 @@ class linefill : public std::ostream { // Internal: Attributes. private: - std::string _header; - size_t _width; - size_t _lines; + mstream& _base; + std::string _header; + size_t _width; + size_t _lines; }; -inline linefill::linefill ( const std::string& header, std::ostream& s ): std::ostream(s.rdbuf()) , _header(header), _width(0), _lines(0) {} -inline std::ostream& linefill::base () { return (*static_cast(this)); } -inline linefill& linefill::reset () { (*this) << std::endl; _width=0; return *this; } -inline linefill& linefill::flush () { static_cast(this)->flush(); return *this; } -inline linefill& linefill::operator<< ( std::ostream& (*pf)(std::ostream&) ) { (*pf)(*this); return *this; } +inline linefill::linefill ( const std::string& header, mstream& s ): std::ostream(s.rdbuf()), _base(s), _header(header), _width(0), _lines(0) {} +//inline std::ostream& linefill::base () { return (*static_cast(this)); } +inline mstream& linefill::base () { return _base; } +inline linefill& linefill::reset () { _base << std::endl; _width=0; return *this; } +inline linefill& linefill::flush () { static_cast(_base).flush(); return *this; } +inline linefill& linefill::operator<< ( std::ostream& (*pf)(std::ostream&) ) { (*pf)(static_cast(_base)); return *this; } inline void linefill::_print ( const std::string& field ) { size_t fieldWidth = field.length(); diff --git a/crlcore/src/cyclop/CyclopMain.cpp b/crlcore/src/cyclop/CyclopMain.cpp index f1ab1528..5511e48e 100644 --- a/crlcore/src/cyclop/CyclopMain.cpp +++ b/crlcore/src/cyclop/CyclopMain.cpp @@ -128,7 +128,7 @@ int main ( int argc, char *argv[] ) exit ( 0 ); } - System::getSystem()->setCatchCore ( not coreDump ); + System::get()->setCatchCore ( not coreDump ); if ( verbose1 ) mstream::enable ( mstream::VerboseLevel1 ); if ( verbose2 ) mstream::enable ( mstream::VerboseLevel2 ); diff --git a/crlcore/src/x2y/x2y.cpp b/crlcore/src/x2y/x2y.cpp index 652bc927..394ea7dc 100644 --- a/crlcore/src/x2y/x2y.cpp +++ b/crlcore/src/x2y/x2y.cpp @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) { exit ( 0 ); } - System::getSystem()->setCatchCore ( not coreDump ); + System::get()->setCatchCore ( not coreDump ); if ( verbose1 ) mstream::enable ( mstream::VerboseLevel1 ); if ( verbose2 ) mstream::enable ( mstream::VerboseLevel2 );