PHP Classes

File: mainexample.php

Recommend this page to a friend!
  Classes of Alessandro Quintiliani   Log Delta Time   mainexample.php   Download  
File: mainexample.php
Role: Example script
Content type: text/plain
Description: Example on usage of LogDeltaTime class and all of its methods to debug a PHP script and generating a log file with different kinds of information. This script is an alternative to index.php and contains a miscellaneous of example1.php, example2.php, example3.php, example4.php
Class: Log Delta Time
Log the time difference between PHP statements
Author: By
Last change: adjusted copyright symbol
Date: 7 years ago
Size: 11,103 bytes
 

Contents

Class file image Download
<?php /* * *************************************************************************************************** * * File name: mainexample.php * * Copyright 2015 Alessandro Quintiliani * * This file is part of LogDeltaTime. * * LogDeltaTime is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * LogDeltaTime is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with LogDeltaTime. If not, see <http://www.gnu.org/licenses/>. * * *************************************************************************************************** */ $thisPHPscriptName = basename($_SERVER['PHP_SELF']); // this instruction simply generates mainexample.php $scriptPHPtoDebug = ""; include("Class.LogDeltaTime.php"); #### CLASS TO DEBUG THIS PHP SCRIPT ### if ($_POST['btn_mainexample'] == $thisPHPscriptName) { $scriptPHPtoDebug = $thisPHPscriptName; define(MYVAR_VALUE, 10); // set a constant MYVAR_VALUE only as an example define(MAX_FACTORIAL, 14); // set a constant MAX_FACTORIAL only as an example define(DELTA_MIN_TO_TRACK, 2); // set minimum time difference in seconds (constant named i.e. DELTA_MIN_TO_TRACK) between two consecutive log messages to be tracked to the log file define(MIN_EXECUTION_DELAY, 2); // set min delay execution script, in seconds (constant named i.e.MIN_EXECUTION_DELAY) define(MAX_EXECUTION_DELAY, 4); // set max delay execution script, in seconds (constant named i.e.MAX_EXECUTION_DELAY) $LOGDIR = "log"; # directory containing the log generated by this PHP script when instantiating LogDeltaTime class. It's created only if not exists at the same level of this PHP script $LOGFILE = "logmainexample.txt"; # log file created, if not exists, inside $LOGDIR # instance of LogDeltaTime class. This instance also sets the start microtime execution of this PHP script $log = new LogDeltaTime($LOGDIR, $LOGFILE, 1); // replace the value of the third parameter with 2 to append the statements to logmainexample.txt after each run of mainexample.php $log->wlog ( "first statement" ); $log->wlog ( "initializing myvar and c variables" ); $myvar = rand ( 7, MYVAR_VALUE ); $c = 0; $log->wlog ( "check if myvar=" . MYVAR_VALUE . ": if so, a block code with a sum instruction is executed" ); $log->wlog ( "begin 'if' block having myvar as a control variable and executed only if myvar=" . MYVAR_VALUE ); $log->setDeltaLog(false); // from this point on, the log statement format doesn't have delta time $log->wlog ( "value of myvar: $myvar" ); if ($myvar == MYVAR_VALUE) { $log->wlog ( "entered the block code having myvar=$myvar" ); $log->wlog ( "first log message in the 'if' block having myvar=$myvar" ); $log->wlog ( "set values of a and b" ); $a = rand ( -5, 9 ); $b = rand ( -5, 9 ); $log->wlog ( "executing simple sum between a=$a and b=$b and the result placed in c" ); $c = $a + $b; $log->wlog ( " c (= a + b) equals $c" ); $log->wlog ( "last log message in the 'if' block having myvar=$myvar as a control variable" ); } $log->wlog ( "end 'if' block having myvar as a control variable and executed only if myvar=" . MYVAR_VALUE ); $log->setDeltaLog(true); // from this point on, the log statement format is switched back to that having delta time $log->wlog ( "begin new if block having c as a control variable used as input to calculate its factorial number (allowed values of c: [0-" . MAX_FACTORIAL . "])" ); $log->wlog ( "value of c: $c" ); if ($c < 0) { $log->wlog ( "first log message in the 'if' block having c as a control variable" ); $log->wlog ( " c=$c: cannot calculate the factorial of a negative number" ); $factorial = "undefined"; } elseif ($c > MAX_FACTORIAL) { $log->wlog ( "first log message in the if block having c>" . MAX_FACTORIAL ); $log->wlog ( " c=$c: " . MAX_FACTORIAL . " is the maximum allowed value for c" ); } else { $log->wlog ( "first log message in the if block having c>=0 and c<=" . MAX_FACTORIAL ); $log->wlog ( " c=$c (c > 1 and c <= " . MAX_FACTORIAL . "): its factorial value can be calculated" ); if ($c <= 1) { $log->wlog ( "c=$c --> c!=1 " ); $factorial = 1; } else { $factorial = $c; $log->wlog ( "begin for loop to calculate $c! in an iterative way" ); for($i = $c; $i > 2; $i --) { $stepfact = $c - $i + 1; $log->wlog ( "execution factorial step $stepfact" ); $factorial *= ($i - 1); $log->wlog ( "intermediate factorial value: $factorial" ); } $log->wlog ( "end for loop with factorial=$factorial" ); } } $log->wlog ( "end new if block code having c as a control variable used as input to calculate its factorial number (allowed values of c: [0-" . MAX_FACTORIAL . "])" ); $log->wlog ( "at this point, c=$c, $c!=$factorial" ); $log->wlog("start instructions which could potentially delay this PHP script execution"); $simulated_random_delay = rand(MIN_EXECUTION_DELAY, MAX_EXECUTION_DELAY); $log->setDeltaMin ( DELTA_MIN_TO_TRACK ); // from this point on, only those statements having at least DELTA_MIN_TO_TRACK secs elapsed between each other will be written to the log file sleep ( 1 ); // this instruction simulates any block code delaying the execution of 1 second $log->wlog ( "this log message won't be written to the log file because the time difference since setDeltaMin was invoked is less than " . DELTA_MIN_TO_TRACK . " seconds" ); $log->wlog ( "log message placed before the sleep(x) instruction where x is varying randomly between 1 and " . MAX_EXECUTION_DELAY . " seconds" ); sleep ( $simulated_random_delay ); // this instruction simulates any block code delaying the execution to a random value between MIN_EXECUTION_DELAY and MAX_EXECUTION_DELAY seconds $log->wlog ( "log message placed after the sleep(x) instruction where x is varying randomly between " . MIN_EXECUTION_DELAY . " and " . MAX_EXECUTION_DELAY . " seconds: will this message be written to the log file?" ); $log->wlog("end instructions which could potentially delay this PHP script execution"); $log->setDeltaMin ( 0 ); // from this point on, all log statements will be written to the log file $log->wlog("start block code having 'onevar' as a control variable"); $log->wlog("setting 'onevar' as a variable with its current value to be prepended at the log statements"); $log->setCtrlVar('onevar'); $ARRAY_BLOCKVALUES = array('VAL1', 'VAL2', 'VAL3', 'VAL4', 'VAL5'); // possible values assumed by $onevar $MAX_INDEX_BLOCKVALUES = count($ARRAY_BLOCKVALUES) - 1; $indexCtrlVal = rand(0,$MAX_INDEX_BLOCKVALUES); $onevar = $ARRAY_BLOCKVALUES[$indexCtrlVal]; // value of $onevar randomly assigned between 'VAL1' and 'VAL5' $log->wlog("start block code"); // this message will have the value of $onevar prepended when written to the log file if ($onevar == 'VAL2') { $x = 5; $y = 6; $log->wlog("x=$x y=$y"); // this message will have the value of $onevar (only 'VAL2') prepended when written to the log file } elseif ($onevar=='VAL4') { $x = 7; $y = 8; $log->wlog("x=$x y=$y"); // this message will have the value of $onevar (only 'VAL4') prepended when written to the log file } else { $x = 11; $y = 12; $log->wlog("x=$x y=$y"); // this message will have the value of $onevar (one among 'VAL1', 'VAL3', 'VAL5') prepended when written to the log file } $log->wlog("end block code"); $log->end(); unset($log); } // if ($_POST['btn_mainexample'] == 'mainexample.php') ?> <!DOCTYPE HTML> <html> <head> <title>LOGDELTATIME - LOG DEBUGGING EXAMPLE</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function showHelp(id_button) { var title_button = "This PHP script generates logmainexample.txt with the \nlog " + "statements written in the first format (delta time between consecutive log statements)." + "\n- Then the method setDeltaLog(false) is invoked and all the statements, since the point where setDeltaLog(false)" + "\nis encountered, are written with NO delta time between consecutive log statements." + "\n- Then the log format is switched back to that having delta time by invoking setDeltaLog(true)." + "\n- Then a random delay between 2 and 4 seconds is generated by sleep function and the method setDeltaMin(2) is invoked, and" + "\n only statements with time elapsed since their predecessors are tracked to logmainexample.txt." + "\n- Last, 3 instruction sets with three different values of a control variable are written to logmainexample.txt"; var be = document.getElementById(id_button); var a = be.setAttribute("title", title_button); be.appendChild(a); } </script> </head> <body> <h1 style="font-size:200%; text-align: center;">EXAMPLE OF DEBUGGING PHP FILE</h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" target="_self"> <div> <table style="margin-left: auto; margin-right: auto; text-align: center;"> <tr> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;<button id="btn_mainexample" name="btn_mainexample" value="mainexample.php" type="submit" onmouseover="javascript:showHelp('btn_mainexample');">RUN MAINEXAMPLE.PHP</button>&nbsp;</td> </tr> </table> </div> </form> <div style="text-align:center">&nbsp; <?php if ($scriptPHPtoDebug) { echo "<br>Script " . $scriptPHPtoDebug . " executed. See the log file <a href=\"" . $LOGDIR."/".$LOGFILE . "\" target=framename style=\"text-decoration:none;\">" . $LOGFILE . "</a>"; } ?> </div> </body> </html>