/google_api/Logger/File.php

  1. <?php 
  2. /** 
  3. * Copyright 2014 Google Inc. 
  4. * 
  5. * Licensed under the Apache License, Version 2.0 (the "License"); 
  6. * you may not use this file except in compliance with the License. 
  7. * You may obtain a copy of the License at 
  8. * 
  9. * http://www.apache.org/licenses/LICENSE-2.0 
  10. * 
  11. * Unless required by applicable law or agreed to in writing, software 
  12. * distributed under the License is distributed on an "AS IS" BASIS,  
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14. * See the License for the specific language governing permissions and 
  15. * limitations under the License. 
  16. */ 
  17.  
  18. if (!class_exists('Google_Client')) { 
  19. require_once dirname(__FILE__) . '/../autoload.php'; 
  20.  
  21. /** 
  22. * File logging class based on the PSR-3 standard. 
  23. * 
  24. * This logger writes to a PHP stream resource. 
  25. */ 
  26. class Google_Logger_File extends Google_Logger_Abstract 
  27. /** 
  28. * @var string|resource $file Where logs are written 
  29. */ 
  30. private $file; 
  31. /** 
  32. * @var integer $mode The mode to use if the log file needs to be created 
  33. */ 
  34. private $mode = 0640; 
  35. /** 
  36. * @var boolean $lock If a lock should be attempted before writing to the log 
  37. */ 
  38. private $lock = false; 
  39.  
  40. /** 
  41. * @var integer $trappedErrorNumber Trapped error number 
  42. */ 
  43. private $trappedErrorNumber; 
  44. /** 
  45. * @var string $trappedErrorString Trapped error string 
  46. */ 
  47. private $trappedErrorString; 
  48.  
  49. /** 
  50. * {@inheritdoc} 
  51. */ 
  52. public function __construct(Google_Client $client) 
  53. parent::__construct($client); 
  54.  
  55. $file = $client->getClassConfig('Google_Logger_File', 'file'); 
  56. if (!is_string($file) && !is_resource($file)) { 
  57. throw new Google_Logger_Exception( 
  58. 'File logger requires a filename or a valid file pointer' 
  59. ); 
  60.  
  61. $mode = $client->getClassConfig('Google_Logger_File', 'mode'); 
  62. if (!$mode) { 
  63. $this->mode = $mode; 
  64.  
  65. $this->lock = (bool) $client->getClassConfig('Google_Logger_File', 'lock'); 
  66. $this->file = $file; 
  67.  
  68. /** 
  69. * {@inheritdoc} 
  70. */ 
  71. protected function write($message) 
  72. if (is_string($this->file)) { 
  73. $this->open(); 
  74. } elseif (!is_resource($this->file)) { 
  75. throw new Google_Logger_Exception('File pointer is no longer available'); 
  76.  
  77. if ($this->lock) { 
  78. flock($this->file, LOCK_EX); 
  79.  
  80. fwrite($this->file, (string) $message); 
  81.  
  82. if ($this->lock) { 
  83. flock($this->file, LOCK_UN); 
  84.  
  85. /** 
  86. * Opens the log for writing. 
  87. * 
  88. * @return resource 
  89. */ 
  90. private function open() 
  91. // Used for trapping `fopen()` errors. 
  92. $this->trappedErrorNumber = null; 
  93. $this->trappedErrorString = null; 
  94.  
  95. $old = set_error_handler(array($this, 'trapError')); 
  96.  
  97. $needsChmod = !file_exists($this->file); 
  98. $fh = fopen($this->file, 'a'); 
  99.  
  100. restore_error_handler(); 
  101.  
  102. // Handles trapped `fopen()` errors. 
  103. if ($this->trappedErrorNumber) { 
  104. throw new Google_Logger_Exception( 
  105. sprintf( 
  106. "Logger Error: '%s'",  
  107. $this->trappedErrorString 
  108. ),  
  109. $this->trappedErrorNumber 
  110. ); 
  111.  
  112. if ($needsChmod) { 
  113. @chmod($this->file, $this->mode & ~umask()); 
  114.  
  115. return $this->file = $fh; 
  116.  
  117. /** 
  118. * Closes the log stream resource. 
  119. */ 
  120. private function close() 
  121. if (is_resource($this->file)) { 
  122. fclose($this->file); 
  123.  
  124. /** 
  125. * Traps `fopen()` errors. 
  126. * 
  127. * @param integer $errno The error number 
  128. * @param string $errstr The error string 
  129. */ 
  130. private function trapError($errno, $errstr) 
  131. $this->trappedErrorNumber = $errno; 
  132. $this->trappedErrorString = $errstr; 
  133.  
  134. public function __destruct() 
  135. $this->close(); 
.