rtdebug.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. class RTDEBUG {
  3. //Is debug on
  4. private static $is_debug = false;
  5. //
  6. private static $fo = null;
  7. private static $pid = -1;
  8. private static $host = '';
  9. private static $uri = '';
  10. private static $port = '';
  11. //Log Type
  12. const LEVEL_LOG = 0;
  13. const LEVEL_DEBUG = 1;
  14. const LEVEL_WARNING = 2;
  15. const LEVEL_ERROR = 3;
  16. /**
  17. * Init stream socket
  18. * @param socketURL
  19. * @param port
  20. */
  21. public static function init($host='',$uri='',$port=80){
  22. if($host=='') return;
  23. $fo = @fsockopen($host,$port,$errno,$errstr,30);
  24. if(!$fo) return;
  25. @stream_set_blocking($fp, 0);
  26. self::$fo = $fo;
  27. self::$host = $host;
  28. self::$port = $port;
  29. self::$uri = $uri;
  30. self::$pid = time().rand(100000, 999999);
  31. self::$is_debug = true;
  32. }
  33. /**
  34. * @param msg require!!!
  35. */
  36. public static function addLog($msg,$level=0){
  37. // print_r();
  38. if(!self::$is_debug || !$msg) return false;
  39. //Add last calling func. info
  40. $_backtrace = debug_backtrace();
  41. $data = '';
  42. switch(gettype($msg)){
  43. case 'array':
  44. case 'object':
  45. $data = array('data'=>$msg);
  46. break;
  47. default:
  48. $data = array('msg'=>$msg);
  49. }
  50. $data['backtrace'] = array('file'=>str_replace($_SERVER['DOCUMENT_ROOT'], '', $_backtrace[0]['file']),'line'=>$_backtrace[0]['line']);
  51. self::sendLog($data,$level);
  52. }
  53. private static function sendLog($data,$level){
  54. if(!self::$is_debug || !$data || $data=='' || $level<0) return false;
  55. $data = urlencode(json_encode($data));
  56. $url = self::$uri.'?data='.$data.'&level='.$level.'&pid='.self::$pid.'&_t='.time().round(microtime()*1000,0);
  57. $http = array(
  58. 'GET '.$url.' HTTP/1.1',
  59. 'Host: '.self::$host.':'.self::$port,
  60. 'Connection: close',
  61. 'User-Agent: RTDEBUG/1.0 RTDEBUG'
  62. );
  63. $request = join("\n",$http)."\n\n";
  64. fputs(self::$fo,$request);
  65. // fclose(self::$fo);
  66. }
  67. }