~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_query/logging_query.cc

  • Committer: Brian Aker
  • Date: 2009-03-20 23:37:03 UTC
  • mfrom: (950.1.4 mordred)
  • Revision ID: brian@tangent.org-20090320233703-z37n3ijavbaaleim
Merging Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 *)
172
 
{
173
 
  return false;
174
 
}
175
 
 
176
 
bool logging_query_func_post (Session *session)
177
 
{
178
 
  char msgbuf[MAX_MSG_LEN];
179
 
  int msgbuf_len= 0;
180
 
  int wrv;
181
 
 
182
 
  assert(session != NULL);
183
 
 
184
 
  if (fd < 0)
185
 
    return false;
186
 
 
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. */
193
 
 
194
 
  // return if not enabled or query was too fast or resultset was too small
195
 
  if (sysvar_logging_query_enable == false)
196
 
    return false;
197
 
  if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset)
198
 
    return false;
199
 
  if (session->examined_row_count < sysvar_logging_query_threshold_big_examined)
200
 
    return false;
201
 
 
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 */
208
 
 
209
 
  uint64_t t_mark= get_microtime();
210
 
 
211
 
  if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
212
 
    return false;
213
 
 
214
 
  // buffer to quotify the query
215
 
  unsigned char qs[255];
216
 
 
217
 
  // to avoid trying to printf %s something that is potentially NULL
218
 
  const char *dbs= (session->db) ? session->db : "";
219
 
  int dbl= 0;
220
 
  if (dbs != NULL)
221
 
    dbl= session->db_length;
222
 
 
223
 
  msgbuf_len=
224
 
    snprintf(msgbuf, MAX_MSG_LEN,
225
 
             "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
226
 
             "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
227
 
             t_mark,
228
 
             session->thread_id,
229
 
             session->query_id,
230
 
             // dont need to quote the db name, always CSV safe
231
 
             dbl, dbs,
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);
245
 
 
246
 
 
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);
250
 
 
251
 
  return false;
252
 
}
 
171
class Logging_query: public Logging_handler
 
172
{
 
173
  virtual bool pre (Session *)
 
174
  {
 
175
    return false;
 
176
  }
 
177
 
 
178
  virtual bool post (Session *session)
 
179
  {
 
180
    char msgbuf[MAX_MSG_LEN];
 
181
    int msgbuf_len= 0;
 
182
    int wrv;
 
183
 
 
184
    assert(session != NULL);
 
185
 
 
186
    if (fd < 0)
 
187
      return false;
 
188
 
 
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. */
 
195
 
 
196
    // return if not enabled or query was too fast or resultset was too small
 
197
    if (sysvar_logging_query_enable == false)
 
198
      return false;
 
199
    if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset)
 
200
      return false;
 
201
    if (session->examined_row_count < sysvar_logging_query_threshold_big_examined)
 
202
      return false;
 
203
 
 
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 */
 
210
  
 
211
    uint64_t t_mark= get_microtime();
 
212
  
 
213
    if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
 
214
      return false;
 
215
  
 
216
    // buffer to quotify the query
 
217
    unsigned char qs[255];
 
218
  
 
219
    // to avoid trying to printf %s something that is potentially NULL
 
220
    const char *dbs= (session->db) ? session->db : "";
 
221
    int dbl= 0;
 
222
    if (dbs != NULL)
 
223
      dbl= session->db_length;
 
224
  
 
225
    msgbuf_len=
 
226
      snprintf(msgbuf, MAX_MSG_LEN,
 
227
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
 
228
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
 
229
               t_mark,
 
230
               session->thread_id,
 
231
               session->query_id,
 
232
               // dont need to quote the db name, always CSV safe
 
233
               dbl, dbs,
 
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);
 
247
  
 
248
  
 
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);
 
252
  
 
253
    return false;
 
254
  }
 
255
};
253
256
 
254
257
static int logging_query_plugin_init(void *p)
255
258
{
256
 
  logging_t *l= static_cast<logging_t *>(p);
 
259
  Logging_handler **handler= static_cast<Logging_handler **>(p);
 
260
  *handler= NULL;
257
261
 
258
262
  if (sysvar_logging_query_filename == NULL)
259
263
  {
282
286
    return 0;
283
287
  }
284
288
 
285
 
  l->logging_pre= logging_query_func_pre;
286
 
  l->logging_post= logging_query_func_post;
 
289
  *handler= new Logging_query();
287
290
 
288
291
  return 0;
289
292
}
290
293
 
291
294
static int logging_query_plugin_deinit(void *p)
292
295
{
293
 
  logging_st *l= (logging_st *) p;
 
296
  Logging_query *handler= static_cast<Logging_query *>(p);
294
297
 
295
298
  if (fd >= 0)
296
299
  {
298
301
    fd= -1;
299
302
  }
300
303
 
301
 
  l->logging_pre= NULL;
302
 
  l->logging_post= NULL;
 
304
  delete handler;
303
305
 
304
306
  return 0;
305
307
}