-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugObject.php
executable file
·145 lines (117 loc) · 4.05 KB
/
debugObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
File: debugObject.php
Created: 06/03/2008
Author: Nathan Davies
Purpose: To write text file output to a user specified file using the PHP file functions.
This is really designed for trackiong variable information when the code isn't functioning correctly but 'errors' are not happening.
*/
class debugObject
{
var $lnFileHandle ;
var $lcOutputFrom ;
var $ltTimeInstantiated ;
var $lcLineBreak ;
var $lcLineDivision ;
var $lcOutputAuthor ;
var $lcOutPutFileName ;
var $lcWriteType ;
public function debugObject($pcFromFile, $pcDivision, $pcOutputAuthor)
{
$this->ltTimeInstantiated = date('Y-m-d H:i:s') ;
$this->lcOutputFrom = !empty($pcFromFile)?$pcFromFile:'Source not specified' ;
$this->lcLineBreak = "\r\n" ;
$this->lcLineDivision = !empty($pcDivision)?$pcDivision:'-----' ; // set a default if not user defined.
$this->lcOutputAuthor = !empty($pcOutputAuthor)?$pcOutputAuthor:"Unknown User" ; // enables a log history to be more accurately kept
} // end of dataObject instantiation function
function debugOutput($pcOutputFilePath, $pcWriteType, $pcInfoToWrite, $plNewHeader)
{
$llContinue = $this->createHandle($pcOutputFilePath, $pcWriteType) ;
if($llContinue)
{
$lvWritten = $this->writeTofile($pcInfoToWrite, $plNewHeader) ;
if($lvWritten === FALSE)
{
// not possible to write to the file
$lnReturn = -2 ;
}
else
{
$lnReturn = $lvWritten ;
}
}
else
{
// not possible to create the desired file
$lnReturn = -1 ;
}
return $lnReturn ;
} // end of debugOutput
function createHandle($pcOutputFilePath, $pcWriteType)
{
$this->lcOutPutFileName = !empty($pcOutputFilePath)?$pcOutputFilePath:"../debugOutput.txt" ; // default file in the directory above this
$this->lcWriteType = !empty($pcWriteType)?$pcWriteType:"a+" ; // default to append to the file
$this->lnFileHandle = fopen($this->lcOutPutFileName, $this->lcWriteType) ;
$llReturn = $this->lnFileHandle === false?false:true ; // make the return variable for easier testing in the calling code
return $llReturn ;
} // end of createHandle
function ensureFileHandle()
{
if(!$this->lnFileHandle)
{
$this->createHandle($this->lcOutPutFileName, $this->lcWriteType) ;
}
} // end of ensureFileHandle
function writeToFile($pcInfoToWrite, $plNewHeader)
{
// $pcInfoToWrite is a '||' delimited string - '||' denotes a new line is required
$this->ensureFileHandle() ;
// check for file contentst
$llContainsData = $this->checkFileContents() ;
if($llContainsData)
{
$lcOutputString = $this->lcLineBreak . $this->lcLineDivision . $this->lcLineBreak ;
}
if($plNewHeader)
{
$lcOutputString .= "Object Started: " . $this->ltTimeInstantiated . $this->lcLineBreak ;
$lcOutputString .= "Started By: " . $this->lcOutputAuthor . $this->lcLineBreak ;
$lcOutputString .= "Started In: " . $this->lcOutputFrom . $this->lcLineBreak ;
}
if(!empty($pcInfoToWrite))
{
$laInfo = explode("||", $pcInfoToWrite) ;
$lnArrayCount = count($laInfo) - 1 ; // make it useful in the loop below
for($lnLoopCount = 0 ; $lnLoopCount <= $lnArrayCount; $lnLoopCount++)
{
$lcOutputString .= $laInfo[$lnLoopCount] . $this->lcLineBreak ;
}
}
else
{ $lcOutputString .= 'No output information specified' . $this->lcLineBreak ;
}
$lvReturn = fwrite($this->lnFileHandle, $lcOutputString) ;
return $lvReturn ;
} // end of writeToFile
function closeFile()
{
fclose($this->lnFileHandle) ;
} // end of closeFile
function checkFileContents()
{
$llReturn = false ;
if(strtolower(substr($this->lcWriteType, 0,1)) == "a") // need to append, anything else needs to start the file again
{
if(filesize($this->lcOutputFileName > 0))
{
$lvContents = fread($this->lnFileHandle, filesize($this->lcOutputFileName)) ;
if($lvContents)
{
$llReturn = true ;
}
}
}
return $llReturn ;
} // end of checkFileContents
} // end of class definition
?>