16
15
* You should have received a copy of the GNU General Public License
17
16
* along with this program; if not, write to the Free Software
18
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
20
/* need to define DRIZZLE_SERVER to get inside the THD */
28
26
#define MAX_MSG_LEN (32*1024)
30
28
static char* logging_query_filename= NULL;
29
static bool logging_query_enable= true;
30
static ulong logging_query_enable_time= 0;
31
static ulong logging_query_thresh_slow= 0;
32
static ulong logging_query_thresh_bigret= 0;
33
static ulong logging_query_thresh_bigexa= 0;
73
76
The following loop is here because gettimeofday may fail on some systems
75
while (gettimeofday(&t, NULL) != 0)
78
while (gettimeofday(&t, NULL) != 0) {}
77
79
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
79
81
#endif /* defined(HAVE_GETHRTIME) */
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 */
87
bool logging_query_func_pre (THD *thd __attribute__((unused)))
83
bool logging_query_func_pre (THD *thd)
92
bool logging_query_func_post (THD *thd)
85
94
char msgbuf[MAX_MSG_LEN];
98
if (fd < 0) return false;
92
100
assert(thd != NULL);
106
114
/* todo, the THD should have a "utime command completed" inside
107
115
itself, so be more accurate, and so plugins dont have to keep
108
116
calling current_utime, which can be slow */
110
uint64_t t_mark= get_microtime();
113
snprintf(msgbuf, MAX_MSG_LEN,
114
"log bgn thread_id=%ld query_id=%ld"
117
" db=\"%.*s\" query=\"%.*s\"\n",
118
(unsigned long) thd->thread_id,
119
(unsigned long) thd->query_id,
120
t_mark - thd->connect_utime,
121
(uint32_t)command_name[thd->command].length,
122
command_name[thd->command].str,
123
thd->db_length, thd->db,
124
thd->query_length, thd->query);
125
/* a single write has a OS level thread lock
126
so there is no need to have mutexes guarding this write,
128
wrv= write(fd, msgbuf, msgbuf_len);
129
assert(wrv == msgbuf_len);
134
bool logging_query_func_post (THD *thd)
136
char msgbuf[MAX_MSG_LEN];
140
if (fd < 0) return false;
144
/* todo, the THD should have a "utime command completed" inside
145
itself, so be more accurate, and so plugins dont have to keep
146
calling current_utime, which can be slow */
147
uint64_t t_mark= get_microtime();
150
snprintf(msgbuf, MAX_MSG_LEN,
151
"log end thread_id=%ld query_id=%ld"
152
" t_connect=%lld t_start=%lld t_lock=%lld"
154
" rows_sent=%ld rows_examined=%u\n",
117
uint64_t t_mark= get_microtime();
120
snprintf(msgbuf, MAX_MSG_LEN,
121
_("thread_id=%ld query_id=%ld"
122
" t_connect=%lld t_start=%lld t_lock=%lld"
124
" rows_sent=%ld rows_examined=%u\n"
125
" db=\"%.*s\" query=\"%.*s\"\n"),
155
126
(unsigned long) thd->thread_id,
156
127
(unsigned long) thd->query_id,
157
128
t_mark - thd->connect_utime,
160
131
(uint32_t)command_name[thd->command].length,
161
132
command_name[thd->command].str,
162
133
(unsigned long) thd->sent_row_count,
163
(uint32_t) thd->examined_row_count);
134
(uint32_t) thd->examined_row_count,
135
thd->db_length, thd->db,
136
thd->query_length, thd->query);
164
137
/* a single write has a OS level thread lock
165
138
so there is no need to have mutexes guarding this write,
167
140
wrv= write(fd, msgbuf, msgbuf_len);
168
141
assert(wrv == msgbuf_len);
226
static DRIZZLE_SYSVAR_STR(filename, logging_query_filename,
198
static DRIZZLE_SYSVAR_STR(
200
logging_query_filename,
227
201
PLUGIN_VAR_READONLY,
228
"File to log queries to.",
202
N_("File to log to"),
203
NULL, /* check func */
204
NULL, /* update func*/
207
static DRIZZLE_SYSVAR_BOOL(
209
logging_query_enable,
211
N_("Enable logging"),
212
NULL, /* check func */
213
NULL, /* update func */
216
static DRIZZLE_SYSVAR_ULONG(
218
logging_query_enable_time,
220
N_("Disable after this many seconds. Zero for forever"),
221
NULL, /* check func */
222
NULL, /* update func */
228
static DRIZZLE_SYSVAR_ULONG(
230
logging_query_thresh_slow,
232
N_("Threshold for logging slow queries, in microseconds"),
233
NULL, /* check func */
234
NULL, /* update func */
240
static DRIZZLE_SYSVAR_ULONG(
242
logging_query_thresh_bigret,
244
N_("Threshold for logging big queries, for rows returned"),
245
NULL, /* check func */
246
NULL, /* update func */
252
static DRIZZLE_SYSVAR_ULONG(
254
logging_query_thresh_bigexa,
256
N_("Threshold for logging big queries, for rows examined"),
257
NULL, /* check func */
258
NULL, /* update func */
231
264
static struct st_mysql_sys_var* logging_query_system_variables[]= {
232
265
DRIZZLE_SYSVAR(filename),
266
DRIZZLE_SYSVAR(enable),
267
DRIZZLE_SYSVAR(enable_time),
268
DRIZZLE_SYSVAR(thresh_slow),
269
DRIZZLE_SYSVAR(thresh_bigret),
270
DRIZZLE_SYSVAR(thresh_bigexa),