| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Debugging functions
|
|---|
| 4 | *
|
|---|
| 5 | * PHP version 5
|
|---|
| 6 | *
|
|---|
| 7 | * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU General Public License as
|
|---|
| 9 | * published by the Free Software Foundation, either version 3 of
|
|---|
| 10 | * the License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
|
|---|
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 14 | * FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License along
|
|---|
| 17 | * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
|
|---|
| 18 | *
|
|---|
| 19 | * @category Content Management System
|
|---|
| 20 | * @package HotaruCMS
|
|---|
| 21 | * @author Nick Ramsay <admin@hotarucms.org>
|
|---|
| 22 | * @copyright Copyright (c) 2009, Hotaru CMS
|
|---|
| 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
|
|---|
| 24 | * @link http://www.hotarucms.org/
|
|---|
| 25 | */
|
|---|
| 26 | class Debug
|
|---|
| 27 | {
|
|---|
| 28 | protected $fh = array(); // file handlers
|
|---|
| 29 | protected $log = array(); // file paths
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Shows number of database queries and the time it takes for a page to load
|
|---|
| 33 | */
|
|---|
| 34 | public function showQueriesAndTime($h)
|
|---|
| 35 | {
|
|---|
| 36 | if ($h->isDebug) {
|
|---|
| 37 |
|
|---|
| 38 | $mysql_version = $h->db->get_var("SELECT VERSION() AS VE");
|
|---|
| 39 |
|
|---|
| 40 | echo "<p class='debug'>";
|
|---|
| 41 | echo $h->lang['main_hotaru_db_queries'] . $h->db->num_queries . " | ";
|
|---|
| 42 | echo $h->lang['main_hotaru_page_load_time'] . timer_stop(1) . $h->lang['main_times_secs'] . " | ";
|
|---|
| 43 | echo $h->lang['main_hotaru_memory_usage'] . display_filesize(memory_get_usage()) . " | ";
|
|---|
| 44 | echo $h->lang['main_hotaru_php_version'] . phpversion() . " | ";
|
|---|
| 45 | echo $h->lang['main_hotaru_mysql_version'] . $mysql_version . " | ";
|
|---|
| 46 | echo $h->lang['main_hotaru_hotaru_version'] . $h->version;
|
|---|
| 47 | echo "</p>";
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Open file for logging
|
|---|
| 54 | *
|
|---|
| 55 | * @param string $type "speed", "error", etc.
|
|---|
| 56 | * @param string $mode e.g. 'a' or 'w'.
|
|---|
| 57 | * @link http://php.net/manual/en/function.fopen.php
|
|---|
| 58 | */
|
|---|
| 59 | public function openLog($type = 'debug', $mode = 'a+')
|
|---|
| 60 | {
|
|---|
| 61 | $this->log[$type] = CACHE . "debug_logs/" . $type . ".txt";
|
|---|
| 62 |
|
|---|
| 63 | // auto-delete the file after 1 week:
|
|---|
| 64 | /*
|
|---|
| 65 | $last_modified = filemtime($this->log[$type]);
|
|---|
| 66 | $expire = (7 * 24 * 60 * 60); // 1 week
|
|---|
| 67 | if ($last_modified < (time() - $expire)) { unlink ($this->log[$type]); }
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | // open/create a file:
|
|---|
| 71 | $this->fh[$type] = fopen($this->log[$type], $mode) or die("can't open file");
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Log performance and errors
|
|---|
| 77 | *
|
|---|
| 78 | * @param string $type "error", "speed", etc.
|
|---|
| 79 | */
|
|---|
| 80 | public function writeLog($type = 'debug', $string = '')
|
|---|
| 81 | {
|
|---|
| 82 | if ($string) {
|
|---|
| 83 | $string = date('d M Y H:i:s', time()) . ": " . $string . "\n";
|
|---|
| 84 | fwrite($this->fh[$type], $string);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Close log file
|
|---|
| 91 | *
|
|---|
| 92 | * @param string $type "speed", "error", etc.
|
|---|
| 93 | */
|
|---|
| 94 | public function closeLog($type = 'debug')
|
|---|
| 95 | {
|
|---|
| 96 | if (isset($this->fh[$type])) { fclose($this->fh[$type]); }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | ?>
|
|---|