owncloud.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind1991@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * logging utilities
  24. *
  25. * Log is saved at data/owncloud.log (on default)
  26. */
  27. class OC_Log_Owncloud {
  28. static protected $logFile;
  29. /**
  30. * Init class data
  31. */
  32. public static function init() {
  33. $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' );
  34. self::$logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' );
  35. }
  36. /**
  37. * write a message in the log
  38. * @param string $app
  39. * @param string $message
  40. * @param int level
  41. */
  42. public static function write($app, $message, $level) {
  43. $minLevel=min(OC_Config::getValue( "loglevel", OC_Log::WARN ),OC_Log::ERROR);
  44. if($level>=$minLevel){
  45. $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level,'time'=>time());
  46. $fh=fopen(self::$logFile, 'a');
  47. fwrite($fh, json_encode($entry)."\n");
  48. fclose($fh);
  49. }
  50. }
  51. /**
  52. * get entries from the log in reverse chronological order
  53. * @param int limit
  54. * @param int offset
  55. * @return array
  56. */
  57. public static function getEntries($limit=50, $offset=0){
  58. self::init();
  59. $minLevel=OC_Config::getValue( "loglevel", OC_Log::WARN );
  60. $entries = array();
  61. $handle = @fopen(self::$logFile, 'rb');
  62. if ($handle) {
  63. fseek($handle, 0, SEEK_END);
  64. $pos = ftell($handle);
  65. $line = '';
  66. $entriesCount = 0;
  67. $lines = 0;
  68. // Loop through each character of the file looking for new lines
  69. while ($pos >= 0 && $entriesCount < $limit) {
  70. fseek($handle, $pos);
  71. $ch = fgetc($handle);
  72. if ($ch == "\n" || $pos == 0) {
  73. if ($line != '') {
  74. // Add the first character if at the start of the file, because it doesn't hit the else in the loop
  75. if ($pos == 0) {
  76. $line = $ch.$line;
  77. }
  78. $entry = json_decode($line);
  79. // Add the line as an entry if it is passed the offset and is equal or above the log level
  80. if ($entry->level >= $minLevel) {
  81. $lines++;
  82. if ($lines > $offset) {
  83. $entries[] = $entry;
  84. $entriesCount++;
  85. }
  86. }
  87. $line = '';
  88. }
  89. } else {
  90. $line = $ch.$line;
  91. }
  92. $pos--;
  93. }
  94. fclose($handle);
  95. }
  96. return $entries;
  97. }
  98. }