~drizzle-trunk/drizzle/development

499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
3
 *
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
4
 *  Copyright (C) 2008 Mark Atwood
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
 */
383.6.1 by Mark Atwood
add pluggable logging
19
520.1.21 by Brian Aker
THD -> Session rename
20
/* need to define DRIZZLE_SERVER to get inside the Session */
383.6.1 by Mark Atwood
add pluggable logging
21
#define DRIZZLE_SERVER 1
22
#include <drizzled/server_includes.h>
23
#include <drizzled/plugin_logging.h>
549 by Monty Taylor
Took gettext.h out of header files.
24
#include <drizzled/gettext.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
25
#include <drizzled/session.h>
383.6.1 by Mark Atwood
add pluggable logging
26
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
27
/* todo, make this dynamic as needed */
383.6.3 by Mark Atwood
more work on plugable logging
28
#define MAX_MSG_LEN (32*1024)
29
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
30
static char* logging_query_filename= NULL;
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
31
static bool logging_query_enable= true;
32
static ulong logging_query_enable_time= 0;
499.2.15 by Mark Atwood
change thresh_bigexa to threadhold_big_examined. "Use English!" --BrianA
33
static ulong logging_query_threshold_big_resultset= 0;
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
34
383.6.4 by Mark Atwood
more make plug logging work
35
static int fd= -1;
36
37
// copied from drizzled/sql_parse.cc
38
const LEX_STRING command_name[]={
39
  { C_STRING_WITH_LEN("Sleep") },
40
  { C_STRING_WITH_LEN("Quit") },
41
  { C_STRING_WITH_LEN("InitDB") },
42
  { C_STRING_WITH_LEN("Query") },
43
  { C_STRING_WITH_LEN("FieldList") },
44
  { C_STRING_WITH_LEN("CreateDB") },
45
  { C_STRING_WITH_LEN("DropDB") },
46
  { C_STRING_WITH_LEN("Refresh") },
47
  { C_STRING_WITH_LEN("Shutdown") },
48
  { C_STRING_WITH_LEN("Processlist") },
49
  { C_STRING_WITH_LEN("Connect") },
50
  { C_STRING_WITH_LEN("Kill") },
51
  { C_STRING_WITH_LEN("Ping") },
52
  { C_STRING_WITH_LEN("Time") },
53
  { C_STRING_WITH_LEN("ChangeUser") },
54
  { C_STRING_WITH_LEN("BinlogDump") },
55
  { C_STRING_WITH_LEN("ConnectOut") },
56
  { C_STRING_WITH_LEN("RegisterSlave") },
57
  { C_STRING_WITH_LEN("SetOption") },
58
  { C_STRING_WITH_LEN("Daemon") },
59
  { C_STRING_WITH_LEN("Error") }
60
};
61
383.6.3 by Mark Atwood
more work on plugable logging
62
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
63
/* stolen from mysys/my_getsystime
520.1.21 by Brian Aker
THD -> Session rename
64
   until the Session has a good utime "now" we can use
499.2.12 by Mark Atwood
have logging_query.cc call gettimeofday, since we cant get it from the thd
65
   will have to use this instead */
66
67
#include <sys/time.h>
68
static uint64_t get_microtime()
69
{
70
#if defined(HAVE_GETHRTIME)
71
  return gethrtime()/1000;
72
#else
73
  uint64_t newtime;
74
  struct timeval t;
75
  /*
76
    The following loop is here because gettimeofday may fail on some systems
77
  */
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
78
  while (gettimeofday(&t, NULL) != 0) {}
499.2.12 by Mark Atwood
have logging_query.cc call gettimeofday, since we cant get it from the thd
79
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
80
  return newtime;
81
#endif  /* defined(HAVE_GETHRTIME) */
82
}
83
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
84
/* we could just not have a pre entrypoint at all,
85
   and have logging_pre == NULL
86
   but we have this here for the sake of being an example */
520.1.22 by Brian Aker
Second pass of thd cleanup
87
bool logging_query_func_pre (Session *session __attribute__((unused)))
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
88
{
89
  return false;
90
}
499.2.12 by Mark Atwood
have logging_query.cc call gettimeofday, since we cant get it from the thd
91
520.1.22 by Brian Aker
Second pass of thd cleanup
92
bool logging_query_func_post (Session *session)
383.6.1 by Mark Atwood
add pluggable logging
93
{
383.6.3 by Mark Atwood
more work on plugable logging
94
  char msgbuf[MAX_MSG_LEN];
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
95
  int msgbuf_len= 0;
383.6.3 by Mark Atwood
more work on plugable logging
96
  int wrv;
97
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
98
  if (fd < 0) return false;
438.2.1 by Mark Atwood
create logging file, and gracefully handle when it cant be
99
520.1.22 by Brian Aker
Second pass of thd cleanup
100
  assert(session != NULL);
383.6.3 by Mark Atwood
more work on plugable logging
101
499.2.11 by Mark Atwood
add elapsed time back to logging plugin
102
  /*
520.1.21 by Brian Aker
THD -> Session rename
103
    here is some time stuff from class Session
499.2.11 by Mark Atwood
add elapsed time back to logging plugin
104
      uint64_t connect_utime;
499.2.13 by Mark Atwood
comment about connect_utime not being set
105
        todo, looks like this isnt being set
106
	we could store the time this plugin was loaded
107
	but that would just be a dumb workaround
499.2.11 by Mark Atwood
add elapsed time back to logging plugin
108
      uint64_t start_utime;
499.2.12 by Mark Atwood
have logging_query.cc call gettimeofday, since we cant get it from the thd
109
      uint64_t utime_after_lock;
499.2.13 by Mark Atwood
comment about connect_utime not being set
110
      uint64_t current_utime();
111
        todo, cant get to because of namemangling
499.2.11 by Mark Atwood
add elapsed time back to logging plugin
112
  */
113
520.1.21 by Brian Aker
THD -> Session rename
114
  /* todo, the Session should have a "utime command completed" inside
499.2.11 by Mark Atwood
add elapsed time back to logging plugin
115
     itself, so be more accurate, and so plugins dont have to keep
116
     calling current_utime, which can be slow */
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
117
  uint64_t t_mark= get_microtime();
118
119
  msgbuf_len=
120
    snprintf(msgbuf, MAX_MSG_LEN,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
121
             "thread_id=%ld query_id=%ld"
122
             " t_connect=%lld t_start=%lld t_lock=%lld"
123
             " command=%.*s"
124
             " rows_sent=%ld rows_examined=%u\n"
125
             " db=\"%.*s\" query=\"%.*s\"\n",
126
             (unsigned long) session->thread_id,
127
             (unsigned long) session->query_id,
128
             (unsigned long long)(t_mark - session->connect_utime),
129
             (unsigned long long)(t_mark - session->start_utime),
130
             (unsigned long long)(t_mark - session->utime_after_lock),
131
             (uint32_t)command_name[session->command].length,
132
             command_name[session->command].str,
133
             (unsigned long) session->sent_row_count,
134
             (uint32_t) session->examined_row_count,
135
             session->db_length, session->db,
136
             session->query_length, session->query);
438.2.1 by Mark Atwood
create logging file, and gracefully handle when it cant be
137
  /* a single write has a OS level thread lock
138
     so there is no need to have mutexes guarding this write,
139
  */
383.6.3 by Mark Atwood
more work on plugable logging
140
  wrv= write(fd, msgbuf, msgbuf_len);
141
  assert(wrv == msgbuf_len);
142
143
  return false;
383.6.1 by Mark Atwood
add pluggable logging
144
}
145
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
146
static int logging_query_plugin_init(void *p)
383.6.1 by Mark Atwood
add pluggable logging
147
{
148
  logging_t *l= (logging_t *) p;
149
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
150
  if (logging_query_filename == NULL)
151
  {
152
    /* no destination filename was specified via system variables
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
153
       return now, dont set the callback pointers
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
154
    */
155
    return 0;
156
  }
157
512.1.7 by Stewart Smith
In function int open(const char*, int, ...),
158
  fd= open(logging_query_filename, O_WRONLY | O_APPEND | O_CREAT,
159
           S_IRUSR|S_IWUSR);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
160
  if (fd < 0)
438.1.3 by Brian Aker
Option for where to log file (updated to correct style for if() by me)
161
  {
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
162
    sql_print_error(_("fail open() fn=%s er=%s\n"),
163
		    logging_query_filename,
164
		    strerror(errno));
438.2.1 by Mark Atwood
create logging file, and gracefully handle when it cant be
165
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
166
    /* todo
167
       we should return an error here, so the plugin doesnt load
438.2.1 by Mark Atwood
create logging file, and gracefully handle when it cant be
168
       but this causes Drizzle to crash
169
       so until that is fixed,
170
       just return a success,
171
       but leave the function pointers as NULL and the fd as -1
172
    */
173
    return 0;
174
  }
175
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
176
  l->logging_pre= logging_query_func_pre;
177
  l->logging_post= logging_query_func_post;
383.6.1 by Mark Atwood
add pluggable logging
178
179
  return 0;
180
}
181
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
182
static int logging_query_plugin_deinit(void *p)
383.6.1 by Mark Atwood
add pluggable logging
183
{
184
  logging_st *l= (logging_st *) p;
185
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
186
  if (fd >= 0)
438.1.3 by Brian Aker
Option for where to log file (updated to correct style for if() by me)
187
  {
438.2.1 by Mark Atwood
create logging file, and gracefully handle when it cant be
188
    close(fd);
189
    fd= -1;
190
  }
383.6.3 by Mark Atwood
more work on plugable logging
191
383.6.1 by Mark Atwood
add pluggable logging
192
  l->logging_pre= NULL;
193
  l->logging_post= NULL;
194
195
  return 0;
196
}
197
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
198
static DRIZZLE_SYSVAR_STR(
199
  filename,
200
  logging_query_filename,
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
201
  PLUGIN_VAR_READONLY,
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
202
  N_("File to log to"),
203
  NULL, /* check func */
204
  NULL, /* update func*/
205
  NULL /* default */);
206
207
static DRIZZLE_SYSVAR_BOOL(
208
  enable,
209
  logging_query_enable,
210
  PLUGIN_VAR_NOCMDARG,
211
  N_("Enable logging"),
212
  NULL, /* check func */
213
  NULL, /* update func */
214
  true /* default */);
215
216
static DRIZZLE_SYSVAR_ULONG(
217
  enable_time,
218
  logging_query_enable_time,
219
  PLUGIN_VAR_OPCMDARG,
220
  N_("Disable after this many seconds. Zero for forever"),
221
  NULL, /* check func */
222
  NULL, /* update func */
223
  0, /* default */
224
  0, /* min */
225
  ULONG_MAX, /* max */
226
  0 /* blksiz */);
227
520.1.13 by Brian Aker
Merging Mark's work
228
#ifdef NOT_YET
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
229
static DRIZZLE_SYSVAR_ULONG(
499.2.15 by Mark Atwood
change thresh_bigexa to threadhold_big_examined. "Use English!" --BrianA
230
  threshhold_slow,
231
  logging_query_threshold_slow,
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
232
  PLUGIN_VAR_OPCMDARG,
233
  N_("Threshold for logging slow queries, in microseconds"),
234
  NULL, /* check func */
235
  NULL, /* update func */
236
  0, /* default */
237
  0, /* min */
238
  ULONG_MAX, /* max */
239
  0 /* blksiz */);
520.1.13 by Brian Aker
Merging Mark's work
240
#endif
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
241
242
static DRIZZLE_SYSVAR_ULONG(
499.2.15 by Mark Atwood
change thresh_bigexa to threadhold_big_examined. "Use English!" --BrianA
243
  threshold_big_resultset,
244
  logging_query_threshold_big_resultset,
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
245
  PLUGIN_VAR_OPCMDARG,
246
  N_("Threshold for logging big queries, for rows returned"),
247
  NULL, /* check func */
248
  NULL, /* update func */
249
  0, /* default */
250
  0, /* min */
251
  ULONG_MAX, /* max */
252
  0 /* blksiz */);
253
520.1.13 by Brian Aker
Merging Mark's work
254
#ifdef NOT_YET
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
255
static DRIZZLE_SYSVAR_ULONG(
499.2.15 by Mark Atwood
change thresh_bigexa to threadhold_big_examined. "Use English!" --BrianA
256
  threshhold_big_examined,
257
  logging_query_threshold_big_examined,
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
258
  PLUGIN_VAR_OPCMDARG,
259
  N_("Threshold for logging big queries, for rows examined"),
260
  NULL, /* check func */
261
  NULL, /* update func */
262
  0, /* default */
263
  0, /* min */
264
  ULONG_MAX, /* max */
265
  0 /* blksiz */);
520.1.13 by Brian Aker
Merging Mark's work
266
#endif
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
267
268
static struct st_mysql_sys_var* logging_query_system_variables[]= {
269
  DRIZZLE_SYSVAR(filename),
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
270
  DRIZZLE_SYSVAR(enable),
271
  DRIZZLE_SYSVAR(enable_time),
499.2.15 by Mark Atwood
change thresh_bigexa to threadhold_big_examined. "Use English!" --BrianA
272
  DRIZZLE_SYSVAR(threshold_big_resultset),
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
273
  NULL
274
};
275
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
276
mysql_declare_plugin(logging_query)
383.6.1 by Mark Atwood
add pluggable logging
277
{
278
  DRIZZLE_LOGGER_PLUGIN,
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
279
  "logging_query",
383.6.1 by Mark Atwood
add pluggable logging
280
  "0.1",
281
  "Mark Atwood <mark@fallenpegasus.com>",
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
282
  N_("Log queries to a file"),
383.6.1 by Mark Atwood
add pluggable logging
283
  PLUGIN_LICENSE_GPL,
383.6.5 by Mark Atwood
start renaming logging_noop to logging_query
284
  logging_query_plugin_init,
285
  logging_query_plugin_deinit,
383.6.1 by Mark Atwood
add pluggable logging
286
  NULL,   /* status variables */
438.2.2 by Mark Atwood
sysvar for filename for pluggable query logging to log to
287
  logging_query_system_variables,
288
  NULL
383.6.1 by Mark Atwood
add pluggable logging
289
}
290
mysql_declare_plugin_end;