~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/query_log/file.cc

  • Committer: Daniel Nichter
  • Date: 2011-05-15 17:58:28 UTC
  • mto: This revision was merged to the branch mainline in revision 2387.
  • Revision ID: daniel@percona.com-20110515175828-1n4orh47k6vw44o7
Add query_log plugin.  It's tested and documented.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 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; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include <string>
 
22
#include <fcntl.h>
 
23
#include "file.h"
 
24
 
 
25
using namespace std;
 
26
 
 
27
QueryLoggerFile::QueryLoggerFile()
 
28
{
 
29
  _fd= LOG_FILE_CLOSED;
 
30
 
 
31
  // If you add something here, the number of params must match the number
 
32
  // of values pushed to the formatter in logEvent().
 
33
  _formatter.parse(
 
34
    "# %s\n"
 
35
    "# session_id=%d query_id=%d rows_examined=%d rows_sent=%d tmp_tables=%d warnings=%d\n"
 
36
    "# execution_time=%.6f lock_time=%.6f session_time=%.6f\n"
 
37
    "# error=%s\n"
 
38
    "# schema=\"%s\"\n"
 
39
    "%s;\n#\n"
 
40
  );
 
41
}
 
42
 
 
43
QueryLoggerFile::~QueryLoggerFile()
 
44
{
 
45
  closeLogFile();
 
46
}
 
47
 
 
48
bool QueryLoggerFile::logEvent(const event_t *event)
 
49
{
 
50
  if (_fd == LOG_FILE_CLOSED)
 
51
    return false;
 
52
 
 
53
  _formatter
 
54
    % event->ts
 
55
    % event->session_id
 
56
    % event->query_id
 
57
    % event->rows_examined
 
58
    % event->rows_sent
 
59
    % event->tmp_tables
 
60
    % event->warnings
 
61
    % event->execution_time
 
62
    % event->lock_time  // broken
 
63
    % event->session_time
 
64
    % event->error
 
65
    % event->schema
 
66
    % event->query;
 
67
  string msgbuf= _formatter.str();
 
68
 
 
69
  size_t wrv;
 
70
  wrv= write(_fd, msgbuf.c_str(), msgbuf.length());
 
71
  assert(wrv == msgbuf.length());
 
72
 
 
73
  return false; // success
 
74
}
 
75
 
 
76
bool QueryLoggerFile::openLogFile(const char *file)
 
77
{
 
78
  assert(file != NULL);
 
79
 
 
80
  int new_fd= open(file, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
 
81
  if (new_fd < 0)
 
82
    return true; // error
 
83
 
 
84
  closeLogFile();
 
85
  _fd= new_fd;
 
86
 
 
87
  return false; // success
 
88
}
 
89
 
 
90
bool QueryLoggerFile::closeLogFile()
 
91
{
 
92
  if (not _fd == LOG_FILE_CLOSED)
 
93
    close(_fd);  // TODO: catch errors
 
94
  _fd= LOG_FILE_CLOSED;
 
95
  return false;  // success
 
96
}