lib/base/init.cpp

00001 
00002 /* Copyright (c) 2008-2010, Stefan Eilemann <eile@equalizergraphics.com>
00003  *                    2010, Cedric Stalder <cedric.stalder@gmail.com>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License version 2.1 as published
00007  * by the Free Software Foundation.
00008  *  
00009  * This library is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00012  * details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this library; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #include "init.h"
00020 
00021 #include "global.h"
00022 #include "log.h"
00023 #include "pluginRegistry.h"
00024 #include "rng.h"
00025 #include "thread.h"
00026 
00027 #include <fstream>
00028 
00029 namespace eq
00030 {
00031 namespace base
00032 {
00033 
00034 namespace
00035 {
00036     static std::ofstream* _logFile = 0;
00037 }
00038 
00039 EQ_EXPORT bool init( const int argc, char** argv )
00040 {
00041     for( int i=1; i<argc; ++i )
00042     {
00043         if( strcmp( "--eq-logfile", argv[i] ) == 0 )
00044         {
00045             ++i;
00046             if( i<argc )
00047             {
00048                 EQASSERT( !_logFile );
00049                 _logFile = new std::ofstream( argv[i] );
00050                 if( _logFile->is_open( ))
00051                     Log::setOutput( *_logFile );
00052                 else
00053                 {
00054                     EQWARN << "Can't open log file " << argv[i] << ": "
00055                            << base::sysError << std::endl;
00056                     delete _logFile;
00057                     _logFile = 0;
00058                     return false;
00059                 }
00060             }
00061         }
00062     }
00063 
00064     if( !RNG::_init( ))
00065     {
00066         EQERROR << "Failed to initialize random number generator" << std::endl;
00067         return false;
00068     }
00069 
00070     // init all available plugins
00071     PluginRegistry& pluginRegistry = Global::getPluginRegistry();
00072     pluginRegistry.init(); 
00073     Thread::pinCurrentThread();
00074     return true;
00075 }
00076 
00077 EQ_EXPORT bool exit()
00078 {
00079     // de-initialize registered plugins
00080     PluginRegistry& pluginRegistry = Global::getPluginRegistry();
00081     pluginRegistry.exit(); 
00082     eq::base::Thread::removeAllListeners();
00083     eq::base::Log::exit();
00084 
00085     const bool ret = RNG::_exit();
00086     if( _logFile )
00087     {
00088 #ifdef NDEBUG
00089         Log::setOutput( std::cout );
00090 #else
00091         Log::setOutput( std::cerr );
00092 #endif
00093         _logFile->close();
00094         delete _logFile;
00095         _logFile = 0;
00096     }
00097     return ret;
00098 }
00099 
00100 }
00101 }
00102 
Generated on Sun Aug 29 2010 13:53:19 for Equalizer 0.9.1 by  doxygen 1.7.1