eZ Publish  [4.0]
ezdebugsetting.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDebugSetting class
00004 //
00005 // Created on: <16-Jan-2003 16:23:58 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezdebugsetting.php
00032 */
00033 
00034 /*!
00035   \class eZDebugSetting ezdebugsetting.php
00036   \brief Conditional debug output
00037 
00038   This works as a wrapper for the eZDebug class by checking some
00039   conditions defined in site.ini before writing the message.
00040   The condition must be true for the message to be written.
00041 
00042   It will check the debug.ini file and first see if conditions are
00043   enabled globally by reading DebugSettings/ConditionDebug.
00044   If true if will then see if the condition exists in the GeneralCondition
00045   group, if so it will use it for condition check.
00046   If it doesn't exists generally it will check it specifically according
00047   to the message type for instance ErrorCondition, DebugCondition etc.
00048 
00049 Example of debug.ini:
00050 \code
00051 [DebugSettings]
00052 ConditionDebug=enabled
00053 
00054 [GeneralCondition]
00055 my-flag=enabled
00056 other-flag=disabled
00057 
00058 [ErrorCondition]
00059 bad-name-flag=disabled
00060 
00061 \endcode
00062 
00063 */
00064 
00065 require_once( 'lib/ezutils/classes/ezdebug.php' );
00066 //include_once( 'lib/ezutils/classes/ezini.php' );
00067 
00068 class eZDebugSetting
00069 {
00070     /*!
00071      Constructor
00072     */
00073     function eZDebugSetting()
00074     {
00075     }
00076 
00077     /*!
00078       \static
00079       \return true if the condition \a $conditionName is considered enabled.
00080     */
00081     static function isConditionTrue( $conditionName, $messageType )
00082     {
00083         global $eZDebugSettingINIObject;
00084 
00085         $ini = $eZDebugSettingINIObject;
00086 
00087         if ( isset( $eZDebugSettingINIObject ) and $ini instanceof eZINI )
00088         {
00089             if ( $ini->variable( 'DebugSettings', 'ConditionDebug' ) != 'enabled' )
00090                 return false;
00091             $generalSetting = 'GeneralCondition';
00092             $debug = eZDebug::instance();
00093             $debugName = $debug->messageName( $messageType );
00094             $specificSetting = $debugName . 'Condition';
00095             if ( $ini->hasVariable( $generalSetting, $conditionName ) )
00096                 return $ini->variable( $generalSetting, $conditionName ) == 'enabled';
00097             if ( $ini->hasVariable( $specificSetting, $conditionName ) )
00098                 return $ini->variable( $specificSetting, $conditionName ) == 'enabled';
00099         }
00100         return false;
00101     }
00102 
00103     /*!
00104      \static
00105      Creates a new debug label from the original and the condition and returns it.
00106     */
00107     static function changeLabel( $conditionName, $label )
00108     {
00109         if ( $label == "" )
00110             return '<' . $conditionName . '>';
00111         else
00112             return $label . ' <' . $conditionName . '>';
00113     }
00114 
00115     /*!
00116       \static
00117       Writes a debug notice if the condition \a $conditionName is enabled.
00118     */
00119     static function writeNotice( $conditionName, $string, $label = "" )
00120     {
00121         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_NOTICE ) )
00122             return false;
00123         eZDebug::writeNotice( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00124     }
00125 
00126     /*!
00127       \static
00128       Writes a debug warning if the condition \a $conditionName is enabled.
00129     */
00130     static function writeWarning( $conditionName, $string, $label = "" )
00131     {
00132         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_WARNING ) )
00133             return false;
00134         eZDebug::writeWarning( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00135     }
00136 
00137     /*!
00138       \static
00139       Writes a debug error if the condition \a $conditionName is enabled.
00140     */
00141     static function writeError( $conditionName, $string, $label = "" )
00142     {
00143         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_ERROR ) )
00144             return false;
00145         eZDebug::writeError( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00146     }
00147 
00148     /*!
00149       \static
00150       Writes a debug message if the condition \a $conditionName is enabled.
00151     */
00152     static function writeDebug( $conditionName, $string, $label = "" )
00153     {
00154         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_DEBUG ) )
00155             return false;
00156         eZDebug::writeDebug( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00157     }
00158 
00159     /*!
00160       \static
00161       Adds the timing point if the condition \a $conditionName is enabled.
00162     */
00163     static function addTimingPoint( $conditionName, $label = "" )
00164     {
00165         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_TIMING_POINT ) )
00166             return false;
00167         eZDebug::addTimingPoint( eZDebugSetting::changeLabel( $conditionName, $label ) );
00168     }
00169 
00170     /*!
00171      \static
00172      Sets the INI object
00173     */
00174     static function setDebugINI( $ini )
00175     {
00176         global $eZDebugSettingINIObject;
00177         $eZDebugSettingINIObject = $ini;
00178     }
00179 
00180 }
00181 
00182 ?>