A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.5 KiB

  1. <?php
  2. /**
  3. * Event
  4. *
  5. * Send events to a blode daemon. See also:
  6. *
  7. * https://github.com/benlemasurier/blode
  8. *
  9. * @author Ben LeMasurier <ben@sparkfun.com>
  10. * @author Brennen Bearnes <brennen@sparkfun.com>
  11. */
  12. namespace SparkLib\Blode;
  13. class Event {
  14. private static $_instance;
  15. // for PHP metadata about the current process
  16. public static $injectmeta = false;
  17. private static $_ip = null;
  18. private static $_host = null;
  19. private static $_path = null;
  20. private $_url;
  21. private $_curl;
  22. private $_socket;
  23. private $_active = false;
  24. public function __construct() {
  25. $this->_active = (defined('BLODE_EVENTS') && (BLODE_EVENTS === true));
  26. $this->_url = 'http://' . BLODE_SERVER . ':' . BLODE_HTTP_PORT;
  27. static::$_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
  28. static::$_path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null;
  29. static::$_host = gethostname();
  30. if(BLODE_USE_UDP)
  31. $this->_init_socket();
  32. else
  33. $this->_init_curl();
  34. }
  35. public function __destruct() {
  36. if(!$this->_active)
  37. return;
  38. if(BLODE_USE_UDP)
  39. socket_close($this->_socket);
  40. else
  41. curl_close($this->_curl);
  42. }
  43. public static function getInstance() {
  44. if(!isset(self::$_instance))
  45. self::$_instance = new self();
  46. return self::$_instance;
  47. }
  48. public static function __callStatic($method, array $args) {
  49. $loglevel = $method;
  50. if (static::$injectmeta && is_array($args[0])) {
  51. // inject some useful metadata into the message, provided
  52. // it's already an id.
  53. if (isset(static::$_ip)) $args[0]['ip'] = static::$_ip;
  54. if (isset(static::$_path)) $args[0]['path'] = static::$_path;
  55. if (isset(static::$_host)) $args[0]['host'] = static::$_host;
  56. }
  57. static::getInstance()->write($args[0], $loglevel);
  58. }
  59. /**
  60. * Send a message to blode, optionally with a syslog-style severity.
  61. * The message should be a valid JSON string. You might want to
  62. * use writeJSON() for that and just hand it an array.
  63. *
  64. * @param $message string
  65. * @param $severity string name of syslog-style severity
  66. */
  67. public function write($message, $severity = 'debug') {
  68. if(!$this->_active)
  69. return;
  70. if(BLODE_USE_UDP)
  71. return $this->_write_udp($message, $severity);
  72. return $this->_write_http($message, $severity);
  73. }
  74. private function _write_udp($message, $severity) {
  75. $message = json_encode(Array('severity' => $severity, 'message' => $message));
  76. // write to the socket whether it's connected or not.
  77. socket_sendto($this->_socket, $message, strlen($message), 0, BLODE_SERVER, BLODE_UDP_PORT);
  78. return true;
  79. }
  80. private function _write_http($message, $severity) {
  81. // Handle structured messages by turning them into JSON:
  82. if(is_array($message))
  83. $message = json_encode($message);
  84. $message = urlencode($message);
  85. $get = "?severity=$severity&message=$message";
  86. curl_setopt($this->_curl, CURLOPT_URL, $this->_url . $get);
  87. return curl_exec($this->_curl);
  88. }
  89. private function _init_socket() {
  90. if(!$this->_active)
  91. return;
  92. $this->_socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  93. socket_set_nonblock($this->_socket);
  94. }
  95. private function _init_curl() {
  96. if(!$this->_active)
  97. return;
  98. $this->_curl = curl_init();
  99. curl_setopt($this->_curl, CURLOPT_HEADER, 0);
  100. curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 0);
  101. curl_setopt($this->_curl, CURLOPT_FOLLOWLOCATION, 1);
  102. return;
  103. }
  104. }