PHP Classes

File: example3.php

Recommend this page to a friend!
  Classes of Alessandro Quintiliani   Log Delta Time   example3.php   Download  
File: example3.php
Role: Example script
Content type: text/plain
Description: PHP script included in index.php and used as an example to show one type of log file (only statements with at least 2 seconds elapsed since their predecessor statements)
Class: Log Delta Time
Log the time difference between PHP statements
Author: By
Last change: modified "in the log file" with "to the log file"
Date: 8 years ago
Size: 3,605 bytes
 

Contents

Class file image Download
<?php
/*
 * ***************************************************************************************************
 *
 * File name: example3.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/>.
 *
 * ***************************************************************************************************
 */

// DEBUG OF example3.php WITH ONLY LOG STATEMENTS DIFFERING AT LEAST 'N' SECONDS BETWEEN TWO CONSECUTIVE STEPS ARE TRACKED TO THE LOG FILE
// (invoked methods: setDeltaMin, wlog, end)

const MYVAR_VALUE = 10;
const
DELTA_MIN_TO_TRACK = 2; // minimum time difference in seconds between two consecutive log messages
const MAX_EXECUTION_DELAY = 4; // max delay execution script

include ("Class.LogDeltaTime.php");

$LOGDIR = 'log';
$LOGFILE = 'logexample3.txt';

$log = new LogDeltaTime ( $LOGDIR, $LOGFILE, 1 ); // replace the value of the third parameter with 2 to append the statements to logexample3.txt after each run of example3.php

$log->wlog ( "first statement" );

$log->wlog ( "initializing myvar variable" );
$myvar = 24;

$log->wlog ( "value of myvar: $myvar" );

$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 is less than " . DELTA_MIN_TO_TRACK . " seconds" );

sleep ( 2 ); // this instruction simulates any block code delaying the execution of 2 seconds

$log->wlog ( "this log message will be written to the log file because the time difference since the previous statement is at least " . DELTA_MIN_TO_TRACK . " seconds" );

if (
$myvar > MYVAR_VALUE) {
   
$log->wlog ( "first log message in the 'if' block having myvar as a control variable" );
   
   
$log->wlog ( "log message placed before the sleep(x) instruction where x is varying randomly between 1 and " . MAX_EXECUTION_DELAY . " seconds" );
   
sleep ( rand ( 1, MAX_EXECUTION_DELAY ) ); // this instruction simulates any block code delaying the execution to a random value between 1 and MAX_EXECUTION_DELAY seconds
   
$log->wlog ( "log message placed after the sleep(x) instruction where x is varying randomly between 1 and " . MAX_EXECUTION_DELAY . " seconds: will this message be written to the log file?" );
   
   
$log->wlog ( "last message log in the 'if' block having myvar=$myvar" );
}
$log->wlog ( "exit from the 'if' block having myvar as a control variable" );

$log->wlog ( "log message before sleep(2)" );
sleep ( 2 ); // this instruction simulates any block code delaying the execution script of 2 seconds
$log->wlog ( "log message after sleep(2): this will be written to the log file" );

$log->end ();
unset (
$log );
?>