drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
query_log.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright 2011 Daniel Nichter
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include <string>
22#include <fcntl.h>
23#include <drizzled/item.h>
25#include <drizzled/session.h>
26#include <drizzled/session/times.h>
27#include <drizzled/plugin.h>
28#include "query_log.h"
29
30using namespace std;
31using namespace drizzled;
32using namespace plugin;
33
34QueryLog::QueryLog(bool enabled, QueryLoggerFile *logger_file) :
35 drizzled::plugin::EventObserver("file_query_log"),
36 sysvar_enabled(enabled),
37 _logger_file(logger_file)
38{
39}
40
41void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers)
42{
43 registerEvent(observers, AFTER_STATEMENT);
44}
45
46bool QueryLog::observeEventDo(EventData &data)
47{
48 // Don't log and return successful if...
49 if (not sysvar_enabled // all logging is disabled
50 || not sysvar_file_enabled) // or file logging is disabled
51 return false;
52
53 switch (data.event) {
54 case AFTER_STATEMENT:
55 afterStatement((AfterStatementEventData &)data);
56 break;
57 default:
58 fprintf(stderr, "query_log: Unexpected event '%s'\n",
59 EventObserver::eventName(data.event));
60 }
61
62 return false;
63}
64
66{
67 Session *session= &data.session;
68
69 // For the moment we're only interestd in queries, not admin
70 // command and such.
71 if (session->command != COM_QUERY)
72 return false;
73
74 // Query end time (microseconds from epoch)
75 uint64_t t_mark= session->times.getCurrentTimestamp(false);
76
80 _event.execution_time= session->times.getElapsedTime();
81 _event.lock_time= (t_mark - session->times.utime_after_lock);
82 _event.session_time= (t_mark - session->times.getConnectMicroseconds());
83
89 if ( _event.execution_time < sysvar_threshold_execution_time
90 || _event.lock_time < sysvar_threshold_lock_time
91 || _event.session_time < sysvar_threshold_session_time)
92 return false;
93
99 _event.execution_time= _event.execution_time * 0.000001;
100 _event.lock_time= _event.lock_time * 0.000001;
101 _event.session_time= _event.session_time * 0.000001;
102
106 _event.session_id= session->getSessionId();
107 _event.query_id= session->getQueryId();
108 _event.rows_examined= session->examined_row_count;
109 _event.rows_sent= session->sent_row_count;
110 _event.tmp_tables= session->tmp_table;
111 _event.warnings= session->total_warn_count;
112
113 if ( _event.rows_examined < sysvar_threshold_rows_examined
114 || _event.rows_sent < sysvar_threshold_rows_sent
115 || _event.tmp_tables < sysvar_threshold_tmp_tables
116 || _event.warnings < sysvar_threshold_warnings)
117 return false;
118
122 _event.error= session->is_error() ? "true" : "false";
123
127 _event.schema= session->schema()->c_str();
128
132 _event.query= session->getQueryString()->c_str();
133
138 boost::posix_time::ptime t_start= session->times.start_timer();
139 _event.ts= boost::posix_time::to_iso_extended_string(t_start);
140
141 // Pass the event to the loggers.
142 _logger_file->logEvent((const event_t *)&_event);
143
144 return false; // success
145}
QueryLoggerFile implements logging to a file for the QueryLog class.
Definition file.h:35
bool afterStatement(AfterStatementEventData &data)
Definition query_log.cc:65
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.