eZ Publish  [trunk]
ezdebugsetting.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZDebugSetting class
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /**
00012  * Provides conditional debug output
00013  *
00014  * This works as a wrapper for the eZDebug class by checking some
00015  * conditions defined in site.ini before writing the message.
00016  * The condition must be true for the message to be written.
00017  *
00018  * It will check the debug.ini file and first see if conditions are
00019  * enabled globally by reading DebugSettings/ConditionDebug.
00020  * If true if will then see if the condition exists in the GeneralCondition
00021  * group, if so it will use it for condition check.
00022  * If it doesn't exists generally it will check it specifically according
00023  * to the message type for instance ErrorCondition, DebugCondition etc.
00024  *
00025  * Example of debug.ini:
00026  * <code>
00027  * [DebugSettings]
00028  * ConditionDebug=enabled
00029  *
00030  * [GeneralCondition]
00031  * my-flag=enabled
00032  * other-flag=disabled
00033  *
00034  * [ErrorCondition]
00035  * bad-name-flag=disabled
00036  * </code>
00037  */
00038 class eZDebugSetting
00039 {
00040     /**
00041      * Returns true if the condition $conditionName is considered enabled.
00042      *
00043      * @param string $conditionName Name of the condition
00044      *
00045      * @return bool
00046      */
00047     static function isConditionTrue( $conditionName, $messageType )
00048     {
00049         $ini = eZINI::instance( 'debug.ini' );
00050 
00051         if ( $ini->variable( 'DebugSettings', 'ConditionDebug' ) != 'enabled' )
00052             return false;
00053 
00054         $generalSetting = 'GeneralCondition';
00055         if ( $ini->hasVariable( $generalSetting, $conditionName ) )
00056             return $ini->variable( $generalSetting, $conditionName ) == 'enabled';
00057 
00058         $specificSetting = eZDebug::instance()->messageName( $messageType ) . 'Condition';
00059         if ( $ini->hasVariable( $specificSetting, $conditionName ) )
00060             return $ini->variable( $specificSetting, $conditionName ) == 'enabled';
00061     }
00062 
00063     /**
00064      * Creates a new debug label from the original and the condition and returns it.
00065      *
00066      * @param string $conditionName Name of the condition
00067      * @param string $label Optional label
00068      * @return string $label . '<' . $conditionName . '>'
00069      */
00070     static function changeLabel( $conditionName, $label = '' )
00071     {
00072         if ( $label == '' )
00073             return '<' . $conditionName . '>';
00074         else
00075             return $label . ' <' . $conditionName . '>';
00076     }
00077 
00078     /**
00079      * Writes a debug notice if the condition $conditionName is enabled.
00080      *
00081      * @param string $conditionName Name of the condition
00082      * @param string $string Text to write
00083      * @param string $label Optional label
00084      */
00085     static function writeNotice( $conditionName, $string, $label = "" )
00086     {
00087         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_NOTICE ) )
00088             return false;
00089         eZDebug::writeNotice( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00090     }
00091 
00092     /**
00093      * Writes a debug warning if the condition $conditionName is enabled.
00094      *
00095      * @param string $conditionName Name of the condition
00096      * @param string $string Text to write
00097      * @param string $label Optional label
00098      */
00099     static function writeWarning( $conditionName, $string, $label = "" )
00100     {
00101         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_WARNING ) )
00102             return false;
00103         eZDebug::writeWarning( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00104     }
00105 
00106     /**
00107      * Writes a debug error if the condition $conditionName is enabled.
00108      *
00109      * @param string $conditionName Name of the condition
00110      * @param string $string Text to write
00111      * @param string $label Optional label
00112      */
00113     static function writeError( $conditionName, $string, $label = "" )
00114     {
00115         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_ERROR ) )
00116             return false;
00117         eZDebug::writeError( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00118     }
00119 
00120     /**
00121      * Writes a debug message if the condition $conditionName is enabled.
00122      *
00123      * @param string $conditionName Name of the condition
00124      * @param string $string Text to write
00125      * @param string $label Optional label
00126      */
00127     static function writeDebug( $conditionName, $string, $label = "" )
00128     {
00129         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_DEBUG ) )
00130             return false;
00131         eZDebug::writeDebug( $string, eZDebugSetting::changeLabel( $conditionName, $label ) );
00132     }
00133 
00134     /**
00135      * Adds the timing point if the condition $conditionName is enabled.
00136      *
00137      * @param string $conditionName Name of the condition
00138      * @param string $label Optional label
00139      */
00140     static function addTimingPoint( $conditionName, $label = "" )
00141     {
00142         if ( !eZDebugSetting::isConditionTrue( $conditionName, eZDebug::LEVEL_TIMING_POINT ) )
00143             return false;
00144         eZDebug::addTimingPoint( eZDebugSetting::changeLabel( $conditionName, $label ) );
00145     }
00146 
00147     /**
00148      * Sets the INI object
00149      *
00150      * @param eZINI $ini The eZINI object to set.
00151      *
00152      * @deprecated Since 4.5
00153      */
00154     static function setDebugINI( $ini )
00155     {
00156         eZDebug::writeStrict( __METHOD__ . ' is deprecated as of 4.5.', __METHOD__ );
00157     }
00158 
00159 }
00160 
00161 ?>