1. function Dmess($msg, $prefix='') {
2. $debugMethod = 1;// 0-> print out stnd; 1-> write log file;
3. $traceArr = debug_backtrace();
4. $lastTrace = end($traceArr);
5. if( strlen($prefix)>0 )
6. $line = "[".date("Y-m-d H:i:s")."][".$prefix."] ".$msg;
7. else {
8. $line = "[".date("Y-m-d H:i:s")."][";
9. for($i=count($traceArr);$i>0;$i--){
10. $trace = $traceArr[$i-1];
11. $line .= basename($trace['file']).":".$trace['line'];
12. if( $i==count($traceArr) )
13. $line .= "|".$trace['function'];
14. $line .= "=>";
15. }
16. $line = substr($line, 0, -2);
17. $line .= "] " . $msg;
18. }
19. if( $debugMethod==0 ) {
20. print "\n<!--<debug>$msg</debug>//-->\n";
21. //print "\nDEBUG:: $msg</debug><br/>\n";
22. flush();
23. ob_flush();
24. }
25. else {
26. $logfile = "./logs/debug.log";
27. write2file($line."\n", $logfile);
28. }
29. }
30.
31. function write2file($msg,$fileName)
32. {
33. $fp = @fopen($fileName, 'a');
34. if($fp==false)
35. {
36. $fp=@fopen($fileName,'w+');
37. if($fp==false)
38. {
39. if($fileName!=DEBUGFILE) {
40. echo "ERROR:: file ".$fileName." can not be created for writing";
41. exit();
42. }
43. else
44. {
45. echo " file ".$fileName." can not be created for writing";
46. return 0;
47. }
48. }
49. }
50. $lfp = flock($fp,LOCK_EX);
51. $i = 0;
52. while ($lfp == False)
53. {
54. $i++;
55. $lfp = flock($fp,LOCK_EX);
56. if ($i==10){return(0);}
57. }
58. fputs($fp,$msg);
59. flock($fp,LOCK_UN);
60. fclose($fp);
61. return 1;
62. }
摘自 氣度非等閒blog