- function Dmess($msg, $prefix='') {
- $debugMethod = 1;// 0-> print out stnd; 1-> write log file;
- $traceArr = debug_backtrace();
- $lastTrace = end($traceArr);
- if( strlen($prefix)>0 )
- $line = "[".date("Y-m-d H:i:s")."][".$prefix."] ".$msg;
- else {
- $line = "[".date("Y-m-d H:i:s")."][";
- for($i=count($traceArr);$i>0;$i--){
- $trace = $traceArr[$i-1];
- $line .= basename($trace['file']).":".$trace['line'];
- if( $i==count($traceArr) )
- $line .= "|".$trace['function'];
- $line .= "=>";
- }
- $line = substr($line, 0, -2);
- $line .= "] " . $msg;
- }
- if( $debugMethod==0 ) {
- print "\n<!--<debug>$msg</debug>//-->\n";
- //print "\nDEBUG:: $msg</debug><br/>\n";
- flush();
- ob_flush();
- }
- else {
- $logfile = "./logs/debug.log";
- write2file($line."\n", $logfile);
- }
- }
- function write2file($msg,$fileName)
- {
- $fp = @fopen($fileName, 'a');
- if($fp==false)
- {
- $fp=@fopen($fileName,'w+');
- if($fp==false)
- {
- if($fileName!=DEBUGFILE) {
- echo "ERROR:: file ".$fileName." can not be created for writing";
- exit();
- }
- else
- {
- echo " file ".$fileName." can not be created for writing";
- return 0;
- }
- }
- }
- $lfp = flock($fp,LOCK_EX);
- $i = 0;
- while ($lfp == False)
- {
- $i++;
- $lfp = flock($fp,LOCK_EX);
- if ($i==10){return(0);}
- }
- fputs($fp,$msg);
- flock($fp,LOCK_UN);
- fclose($fp);
- return 1;
- }