/[refeed]/trunk/introspect/DB/mysql_logged.php
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/introspect/DB/mysql_logged.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Wed Jul 5 00:27:49 2006 UTC (18 years ago) by dpavlin
File size: 5377 byte(s)
make working copy of trunk
1 dpavlin 2 <?php
2     // vim: ts=4 foldcolumn=4 foldmethod=marker
3     /**
4     * DB_mysql_logged class found here.
5     *
6     * This file is part of Reblog,
7     * a derivative work of Feed On Feeds.
8     *
9     * Distributed under the Gnu Public License.
10     *
11     * @package Refeed
12     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13     * @author Michal Migurski <mike@stamen.com>
14     * @author Michael Frumin <mfrumin@eyebeam.org>
15     * @copyright ©2004 Michael Frumin, Michal Migurski
16     * @link http://reblog.org Reblog
17     * @link http://feedonfeeds.com Feed On Feeds
18     * @version $Revision: 1.1 $
19     */
20    
21     require_once 'DB/mysql.php';
22    
23     /**
24     * DB_mysql_logged is a subclass of DB_mysql.
25     *
26     * All queries and statement executions are logged to /tmp/query_log.txt
27     * with timing information for debugging purposes. This is used only when
28     * DSN's have "mysql_logged://" at the front instead of "mysql://", and
29     * this file's containing directory, "DB", is in the include path.
30     *
31     * @see DB::connect()
32     */
33     class DB_mysql_logged extends DB_mysql
34     {
35     function DB_mysql_logged()
36     {
37     $this->DB_mysql();
38     }
39    
40     /**
41     * @return float current time, in milliseconds
42     */
43     function milliseconds()
44     {
45     list($msec, $sec) = split(' ', microtime());
46     return (intval($sec) + floatval($msec)) * 1000;
47     }
48    
49     /**
50     * Scrubs queries a little so they are suitable for logging.
51     *
52     * @param string $query raw query
53     * @return string scrubbed query
54     */
55     function scrubValues($query)
56     {
57     // find context line
58     preg_match('/^#\s+Context:\s+((\w+):(\w+)\(\)\s+(\S.*):(\d+))$/mi', $query, $context);
59    
60     // clean out comments
61     $query = preg_replace('/#.+$/m', '', $query);
62    
63     // find numbers and strings
64     //$query = preg_replace('/(\W)[\d\.\-]+(\W)/', '\1CONST\2', $query);
65     //$query = preg_replace("/'[^']*'/", 'CONST', $query);
66    
67     // remove extra spaces
68     $query = preg_replace('/\s\s+/', ' ', $query);
69     $query = preg_replace('/\(\s+/', '(', $query);
70     $query = preg_replace('/\s+\)/', ')', $query);
71    
72     // scrub out the reblog root directory
73     $query = trim($query);
74     $root = realpath(dirname(__FILE__).'/../../..');
75     $context = str_replace("{$root}/", '', $context[1]);
76    
77     return array($query, $context);
78     }
79    
80     /**
81     * Logs queries to /tmp/query_log.txt, with some extra information.
82     *
83     * @param string $query database query
84     * @param float $time current time in milliseconds
85     *
86     * @uses DB_mysql_logged::scrubValues()
87     */
88     function logQuery($query, $time)
89     {
90     list($query, $context) = $this->scrubValues($query);
91    
92     if($log = fopen('/tmp/query_log.txt', 'a') /*&& flock($log, LOCK_EX)*/) {
93     fwrite($log, $query);
94     fwrite($log, sprintf("\n# Context: %s\n# Query took %0.1f msec\n\n", $context, $time));
95    
96     /*flock($log, LOCK_UN);*/
97     fclose($log);
98     chmod('/tmp/query_log.txt', 0666);
99     }
100    
101     if($log = fopen('/tmp/query_log.sql', 'a') /*&& flock($log, LOCK_EX)*/) {
102     $insert = sprintf('INSERT INTO query_log (stamp, context, query, duration) VALUES(%s, %s, %s, %f)',
103     $this->quoteSmart(date('Y-m-d H:i:s')),
104     $this->quoteSmart($context),
105     $this->quoteSmart($query),
106     $time);
107    
108     fwrite($log, "{$insert};\n");
109    
110     /*flock($log, LOCK_UN);*/
111     fclose($log);
112     chmod('/tmp/query_log.sql', 0666);
113     }
114     }
115    
116     /**
117     * Sends a query to DB_mysql::query, then logs it before returning a result.
118     *
119     * @param string $query database query
120     * @param array $params extra parameters
121     * @return DB_result Query result
122     *
123     * @uses DB_mysql_logged::milliseconds()
124     * @uses DB_mysql_logged::logQuery()
125     */
126     function &query($query, $params=array())
127     {
128     $start = $this->milliseconds();
129     $result =& parent::query($query, $params);
130    
131     $this->logQuery($query, $this->milliseconds() - $start);
132    
133     return $result;
134     }
135    
136     /**
137     * Sends a statement to DB_mysql::execute, then logs it before returning a result.
138     *
139     * @param string $statement database statement
140     * @param array $data extra parameters
141     * @return DB_result Query result
142     *
143     * @uses DB_mysql_logged::milliseconds()
144     * @uses DB_mysql_logged::logQuery()
145     */
146     function &execute($statement, $data=array())
147     {
148     $start = $this->milliseconds();
149     $result =& parent::execute($statement, $data);
150    
151     $this->logQuery($statement, $this->milliseconds() - $start);
152    
153     return $result;
154     }
155     }
156    
157     ?>

  ViewVC Help
Powered by ViewVC 1.1.26