00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan 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 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanLogHolder.h" 00028 00029 // All plugs needed, to be able to pick one of them: 00030 #include "CeylanLogPlugConsole.h" // for console plug 00031 #include "CeylanLogPlugClassical.h" // for classical plug 00032 #include "CeylanLogPlugHTML.h" // for HTML plug 00033 #include "CeylanLogPlugNull.h" // for null plug 00034 00035 00036 #include "CeylanOperators.h" // for string operators 00037 #include "CeylanLogLight.h" // for CEYLAN_LOG 00038 #include "CeylanUtils.h" // for ExitFailure, emergencyShutdown 00039 #include "CeylanTextDisplayable.h" // for SetOutputFormat 00040 00041 00042 #include <iostream> // for cerr, if abnormal situation occurs 00043 00044 00045 #ifdef CEYLAN_USES_CONFIG_H 00046 #include "CeylanConfig.h" // for configure-time feature settings 00047 #endif // CEYLAN_USES_CONFIG_H 00048 00049 00050 using std::string ; 00051 00052 using namespace Ceylan::Log ; 00053 00054 00055 const string LogHolder::ConsolePlugOption = "--consolePlug" ; 00056 const string LogHolder::ClassicalPlugOption = "--classicalPlug" ; 00057 const string LogHolder::HTMLPlugOption = "--HTMLPlug" ; 00058 const string LogHolder::NullPlugOption = "--nullPlug" ; 00059 00060 00061 00062 #if CEYLAN_ARCH_NINTENDO_DS 00063 00064 KnownPlugs LogHolder::DefaultPlug = consolePlug ; 00065 00066 #else // CEYLAN_ARCH_NINTENDO_DS 00067 00068 KnownPlugs LogHolder::DefaultPlug = classicalPlug ; 00069 00070 #endif // CEYLAN_ARCH_NINTENDO_DS 00071 00072 00073 00074 LogHolder::LogHolder( Ceylan::Uint16 argCount, 00075 const char * const arguments[], bool immediateWrite ) : 00076 _chosenPlug( DefaultPlug ) 00077 { 00078 00079 for ( Ceylan::Uint16 i = 0; i < argCount; i++ ) 00080 { 00081 00082 if ( arguments[ i ] == ConsolePlugOption ) 00083 { 00084 TextDisplayable::SetOutputFormat( 00085 Ceylan::TextDisplayable::rawText ) ; 00086 _chosenPlug = consolePlug ; 00087 break ; 00088 } 00089 00090 if ( arguments[ i ] == ClassicalPlugOption ) 00091 { 00092 TextDisplayable::SetOutputFormat( 00093 Ceylan::TextDisplayable::rawText ) ; 00094 _chosenPlug = classicalPlug ; 00095 break ; 00096 } 00097 00098 if ( arguments[ i ] == HTMLPlugOption ) 00099 { 00100 TextDisplayable::SetOutputFormat( Ceylan::TextDisplayable::html ) ; 00101 _chosenPlug = HTMLPlug ; 00102 break ; 00103 } 00104 00105 if ( arguments[ i ] == NullPlugOption ) 00106 { 00107 _chosenPlug = nullPlug ; 00108 break ; 00109 } 00110 00111 // Do not touch argument if not recognized. 00112 00113 } 00114 00115 // Records that path as some libraries (ex: PhysicsFS) need it: 00116 LogPlug::SetFullExecutablePath( arguments[0] ) ; 00117 00118 string speakerName = LogPlug::GetSpeakerNameFrom( arguments[0] ) ; 00119 00120 00121 /* 00122 * LogHolder is usually out of a try/catch pair, avoid propagating 00123 * exception: 00124 * 00125 */ 00126 00127 try 00128 { 00129 00130 switch( _chosenPlug ) 00131 { 00132 00133 case consolePlug: 00134 CEYLAN_LOG( "LogHolder: using the console plug." ) ; 00135 LogPlugConsole::StartService( speakerName, immediateWrite ) ; 00136 break ; 00137 00138 case classicalPlug: 00139 CEYLAN_LOG( "LogHolder: using the classical plug." ) ; 00140 LogPlugClassical::StartService( speakerName, immediateWrite ) ; 00141 break ; 00142 00143 case HTMLPlug: 00144 CEYLAN_LOG( "LogHolder: using the HTML plug." ) ; 00145 // No immediateWrite for LogPlugHTML: 00146 LogPlugHTML::StartService( speakerName ) ; 00147 break ; 00148 00149 case nullPlug: 00150 CEYLAN_LOG( "LogHolder: using the null plug." ) ; 00151 LogPlugNull::StartService() ; 00152 break ; 00153 00154 00155 default: 00156 CEYLAN_LOG( "Warning: LogHolder: " 00157 "no known plug specified, defaulting to classical." ) ; 00158 LogPlugClassical::StartService( speakerName, immediateWrite ) ; 00159 break ; 00160 00161 } 00162 00163 } 00164 catch( const Ceylan::Exception & e ) 00165 { 00166 Ceylan::emergencyShutdown( "LogHolder constructor failed: " 00167 + e.toString() ) ; 00168 } 00169 00170 } 00171 00172 00173 00174 LogHolder::~LogHolder() throw() 00175 { 00176 00177 switch( _chosenPlug ) 00178 { 00179 00180 case consolePlug: 00181 CEYLAN_LOG( "LogHolder: stopping the console plug." ) ; 00182 LogPlugConsole::StopService() ; 00183 break ; 00184 00185 case classicalPlug: 00186 CEYLAN_LOG( "LogHolder: stopping the classical plug." ) ; 00187 LogPlugClassical::StopService() ; 00188 break ; 00189 00190 case HTMLPlug: 00191 CEYLAN_LOG( "LogHolder: stopping the HTML plug." ) ; 00192 LogPlugHTML::StopService() ; 00193 break ; 00194 00195 case nullPlug: 00196 CEYLAN_LOG( "LogHolder: stopping the null plug." ) ; 00197 LogPlugNull::StopService() ; 00198 break ; 00199 00200 default: 00201 std::cerr << "LogHolder destructor: " 00202 "no valid plug available! Aborting." << std::endl ; 00203 ::exit( Ceylan::ExitFailure ) ; 00204 break ; 00205 00206 } 00207 00208 } 00209 00210 00211 00212 const string LogHolder::toString( Ceylan::VerbosityLevels level ) const 00213 { 00214 00215 switch( _chosenPlug ) 00216 { 00217 00218 case consolePlug: 00219 return "LogHolder uses the console plug" ; 00220 // break ; 00221 00222 case classicalPlug: 00223 return "LogHolder uses the classical plug" ; 00224 // break ; 00225 00226 case HTMLPlug: 00227 return "LogHolder uses the HTML plug" ; 00228 // break ; 00229 00230 case nullPlug: 00231 return "LogHolder uses the null plug" ; 00232 // break ; 00233 00234 default: 00235 return "LogHolder uses an unknown plug! (abnormal)" ; 00236 // break ; 00237 00238 } 00239 00240 } 00241 00242 00243 00244 bool LogHolder::IsAKnownPlugOption( const std::string & option ) 00245 { 00246 00247 if ( option == ConsolePlugOption ) 00248 return true ; 00249 00250 if ( option == ClassicalPlugOption ) 00251 return true ; 00252 00253 if ( option == HTMLPlugOption ) 00254 return true ; 00255 00256 if ( option == NullPlugOption ) 00257 return true ; 00258 00259 return false ; 00260 00261 } 00262