168
168
/* we could just not have a pre entrypoint at all,
169
169
and have logging_pre == NULL
170
170
but we have this here for the sake of being an example */
171
bool logging_query_func_pre (Session *)
176
bool logging_query_func_post (Session *session)
178
char msgbuf[MAX_MSG_LEN];
182
assert(session != NULL);
187
/* Yes, we know that checking sysvar_logging_query_enable,
188
sysvar_logging_query_threshold_big_resultset, and
189
sysvar_logging_query_threshold_big_examined is not threadsafe,
190
because some other thread might change these sysvars. But we
191
don't care. We might start logging a little late as it spreads
192
to other threads. Big deal. */
194
// return if not enabled or query was too fast or resultset was too small
195
if (sysvar_logging_query_enable == false)
197
if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset)
199
if (session->examined_row_count < sysvar_logging_query_threshold_big_examined)
202
/* TODO, looks like connect_utime isnt being set in the session
203
object. We could store the time this plugin was loaded, but that
204
would just be a dumb workaround. */
205
/* TODO, the session object should have a "utime command completed"
206
inside itself, so be more accurate, and so this doesnt have to
207
keep calling current_utime, which can be slow */
209
uint64_t t_mark= get_microtime();
211
if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
214
// buffer to quotify the query
215
unsigned char qs[255];
217
// to avoid trying to printf %s something that is potentially NULL
218
const char *dbs= (session->db) ? session->db : "";
221
dbl= session->db_length;
224
snprintf(msgbuf, MAX_MSG_LEN,
225
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
226
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
230
// dont need to quote the db name, always CSV safe
232
// do need to quote the query
233
quotify((unsigned char *)session->query,
234
session->query_length, qs, sizeof(qs)),
235
// command_name is defined in drizzled/sql_parse.cc
236
// dont need to quote the command name, always CSV safe
237
(int)command_name[session->command].length,
238
command_name[session->command].str,
239
// counters are at end, to make it easier to add more
240
(t_mark - session->connect_utime),
241
(t_mark - session->start_utime),
242
(t_mark - session->utime_after_lock),
243
session->sent_row_count,
244
session->examined_row_count);
247
// a single write has a kernel thread lock, thus no need mutex guard this
248
wrv= write(fd, msgbuf, msgbuf_len);
249
assert(wrv == msgbuf_len);
171
class Logging_query: public Logging_handler
173
virtual bool pre (Session *)
178
virtual bool post (Session *session)
180
char msgbuf[MAX_MSG_LEN];
184
assert(session != NULL);
189
/* Yes, we know that checking sysvar_logging_query_enable,
190
sysvar_logging_query_threshold_big_resultset, and
191
sysvar_logging_query_threshold_big_examined is not threadsafe,
192
because some other thread might change these sysvars. But we
193
don't care. We might start logging a little late as it spreads
194
to other threads. Big deal. */
196
// return if not enabled or query was too fast or resultset was too small
197
if (sysvar_logging_query_enable == false)
199
if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset)
201
if (session->examined_row_count < sysvar_logging_query_threshold_big_examined)
204
/* TODO, looks like connect_utime isnt being set in the session
205
object. We could store the time this plugin was loaded, but that
206
would just be a dumb workaround. */
207
/* TODO, the session object should have a "utime command completed"
208
inside itself, so be more accurate, and so this doesnt have to
209
keep calling current_utime, which can be slow */
211
uint64_t t_mark= get_microtime();
213
if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
216
// buffer to quotify the query
217
unsigned char qs[255];
219
// to avoid trying to printf %s something that is potentially NULL
220
const char *dbs= (session->db) ? session->db : "";
223
dbl= session->db_length;
226
snprintf(msgbuf, MAX_MSG_LEN,
227
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
228
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
232
// dont need to quote the db name, always CSV safe
234
// do need to quote the query
235
quotify((unsigned char *)session->query,
236
session->query_length, qs, sizeof(qs)),
237
// command_name is defined in drizzled/sql_parse.cc
238
// dont need to quote the command name, always CSV safe
239
(int)command_name[session->command].length,
240
command_name[session->command].str,
241
// counters are at end, to make it easier to add more
242
(t_mark - session->connect_utime),
243
(t_mark - session->start_utime),
244
(t_mark - session->utime_after_lock),
245
session->sent_row_count,
246
session->examined_row_count);
249
// a single write has a kernel thread lock, thus no need mutex guard this
250
wrv= write(fd, msgbuf, msgbuf_len);
251
assert(wrv == msgbuf_len);
254
257
static int logging_query_plugin_init(void *p)
256
logging_t *l= static_cast<logging_t *>(p);
259
Logging_handler **handler= static_cast<Logging_handler **>(p);
258
262
if (sysvar_logging_query_filename == NULL)