fopen - many times per page
Posted: Wed Aug 01, 2012 11:26 am
I've put together a debug function, that is set to run at various point in my site. But as I add more instances of this function I am starting to get more and more errors in the form of:
[text]Warning: fopen(.\DebugData\PageListing_20120801_17.txt): failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\debug.function.php on line 96 Can't Open File[/text]
In some cases it is accessed up to 25 times per page, to record every page that is loaded, every Database that is accessed, and every query that is run etc.
Does it matter that I open and close the debug file each time I write to it, or is there a better option?
My function is:
and is called as:
[text]Warning: fopen(.\DebugData\PageListing_20120801_17.txt): failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\debug.function.php on line 96 Can't Open File[/text]
In some cases it is accessed up to 25 times per page, to record every page that is loaded, every Database that is accessed, and every query that is run etc.
Does it matter that I open and close the debug file each time I write to it, or is there a better option?
My function is:
Code: Select all
function debugf($P,$S,$D,$Ds)
{
$debug = "On";
if ($debug == "On") {
$timezone = "Europe/London";
date_default_timezone_set ($timezone);
$DebugDate = date('Ymd_H');
$ResetTime = date('Y-m-d H:i:s');
$tab = (chr(9));
$page = $P;
$Section = explode(",",$S);
$DB = $D;
$DBPoint = $Ds;
//print_r($Section);
//$a=0;
foreach($Section AS $Sc){
switch($Sc){
case 1:
//echo "COUNTER";
$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 1;};
$fh = fopen($myFile, 'a') or die("Can't Open File");
flock($fh, LOCK_EX);
fwrite($fh, "--PAGE COUNT: ".$Counter."--\r\n");
flock($fh, LOCK_UN);
fclose($fh);
print $Counter;
$Counter++;
$_SESSION['debugCounter'] = $Counter;
break;
case 2:
//echo "DBCONNECTIONS";
switch($DBPoint){
case 1:
$DBp = "Start";
break;
case 2:
$DBp = "Connected";
break;
case 3:
$DBp = "End";
break;
default:
$DBp = $Ds;
break;
};
$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
$fh = fopen($myFile, 'a') or die("Can't Open File");
flock($fh, LOCK_EX);
fwrite($fh, $page.$tab.date('H:i:s').$tab.$DB." ".$DBp."\r\n");
flock($fh, LOCK_UN);
fclose($fh);
break;
case 3:
//echo "EMAIL ON RESET";
if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 2;};
if($Counter == 2) {
$email = "email@company.com";
$subj = "Test Page Counter Has Been Reset";
$mes = "Test Page Counter Has Been Reset\n
Time: $ResetTime";
mail($email, $subj, $mes);
};
unset($Counter);
break;
case 4:
//echo "HEADER";
//DBConnections
$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
$fh = fopen($myFile, 'a') or die("Can't Open File");
flock($fh, LOCK_EX);
fwrite($fh, "## START ##".$tab."Time".$tab."Database \r\n");
flock($fh, LOCK_UN);
fclose($fh);
//PageListing//
$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
$fh = fopen($myFile, 'a') or die("Can't Open File");
fwrite($fh, "## ".$page." ##".$tab."Time".$tab."Current".$tab."Peak \r\n");
fclose($fh);
break;
case 5:
//echo "PAGE LISTING";
$mem = memory_get_usage(true); //in bytes
$memPeak = memory_get_peak_usage (true); //in bytes
$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
$fh = fopen($myFile, 'a') or die("Can't Open File");
flock($fh, LOCK_EX);
fwrite($fh, $page.$tab.date('H:i:s').$tab.$mem.$tab.$memPeak."\r\n");
flock($fh, LOCK_UN);
fclose($fh);
break;
};
//$a++;
};
};
};Code: Select all
//DEBUG (Page Listing)
debugf("pageName.php(START)","5",0,0);
//--
//DEBUG (DB Connection)
debugf("pagename.php","2",$Cserver,3);
//--