216
/* log event handlers */
218
bool Log_to_file_event_handler::
219
log_error(enum loglevel level, const char *format,
222
return vprint_msg_to_log(level, format, args);
225
void Log_to_file_event_handler::init_pthread_objects()
227
mysql_log.init_pthread_objects();
228
mysql_slow_log.init_pthread_objects();
232
/** Wrapper around DRIZZLE_LOG::write() for slow log. */
234
bool Log_to_file_event_handler::
235
log_slow(THD *thd, time_t current_time, time_t query_start_arg,
236
const char *user_host, uint user_host_len,
237
uint64_t query_utime, uint64_t lock_utime, bool is_command,
238
const char *sql_text, uint sql_text_len)
240
return mysql_slow_log.write(thd, current_time, query_start_arg,
241
user_host, user_host_len,
242
query_utime, lock_utime, is_command,
243
sql_text, sql_text_len);
248
Wrapper around DRIZZLE_LOG::write() for general log. We need it since we
249
want all log event handlers to have the same signature.
252
bool Log_to_file_event_handler::
253
log_general(THD *thd __attribute__((unused)),
254
time_t event_time, const char *user_host,
255
uint user_host_len, int thread_id,
256
const char *command_type, uint command_type_len,
257
const char *sql_text, uint sql_text_len,
258
const CHARSET_INFO * const client_cs __attribute__((unused)))
260
return mysql_log.write(event_time, user_host, user_host_len,
261
thread_id, command_type, command_type_len,
262
sql_text, sql_text_len);
266
bool Log_to_file_event_handler::init()
271
mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
274
mysql_log.open_query_log(sys_var_general_log_path.value);
276
is_initialized= true;
283
void Log_to_file_event_handler::cleanup()
286
mysql_slow_log.cleanup();
289
void Log_to_file_event_handler::flush()
291
/* reopen log files */
293
mysql_log.reopen_file();
295
mysql_slow_log.reopen_file();
299
217
Log error with all enabled log event handlers
378
283
logger.lock_exclusive();
380
/* reopen log files */
381
file_log_handler->flush();
383
285
/* end of log flush */
390
Log slow query with all enabled log event handlers
395
thd THD of the query being logged
396
query The query being logged
397
query_length The length of the query string
398
current_utime Current time in microseconds (from undefined start)
405
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
406
uint64_t current_utime)
410
Log_event_handler **current_handler;
411
bool is_command= false;
412
char user_host_buff[MAX_USER_HOST_SIZE];
413
Security_context *sctx= thd->security_ctx;
414
uint user_host_len= 0;
415
uint64_t query_utime, lock_utime;
418
Print the message to the buffer if we have slow log enabled
421
if (*slow_log_handler_list)
425
/* do not log slow queries from replication threads */
426
if (thd->slave_thread && !opt_log_slow_slave_statements)
436
/* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
437
user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
438
sctx->user, "[", sctx->user, "] @ ",
440
sctx->ip, "]", NullS) -
443
current_time= my_time_possible_from_micro(current_utime);
444
if (thd->start_utime)
446
query_utime= (current_utime - thd->start_utime);
447
lock_utime= (thd->utime_after_lock - thd->start_utime);
451
query_utime= lock_utime= 0;
457
query= command_name[thd->command].str;
458
query_length= command_name[thd->command].length;
461
for (current_handler= slow_log_handler_list; *current_handler ;)
462
error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
463
user_host_buff, user_host_len,
464
query_utime, lock_utime, is_command,
465
query, query_length) || error;
472
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
473
const char *query, uint query_length)
476
Log_event_handler **current_handler= general_log_handler_list;
477
char user_host_buff[MAX_USER_HOST_SIZE];
478
Security_context *sctx= thd->security_ctx;
480
uint user_host_len= 0;
484
id= thd->thread_id; /* Normal thread */
486
id= 0; /* Log from connect handler */
494
user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
498
sctx->ip, "]", NullS) -
501
current_time= my_time(0);
503
while (*current_handler)
504
error|= (*current_handler++)->
505
log_general(thd, current_time, user_host_buff,
507
command_name[(uint) command].str,
508
command_name[(uint) command].length,
510
thd->variables.character_set_client) || error;
516
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
517
const char *format, va_list args)
519
uint message_buff_len= 0;
520
char message_buff[MAX_LOG_BUFFER_SIZE];
522
/* prepare message */
524
message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
527
message_buff[0]= '\0';
529
return general_log_write(thd, command, message_buff, message_buff_len);
532
290
void LOGGER::init_error_log(uint error_log_printer)
534
292
if (error_log_printer & LOG_NONE)
540
switch (error_log_printer) {
542
error_log_handler_list[0]= file_log_handler;
543
error_log_handler_list[1]= 0;
548
void LOGGER::init_slow_log(uint slow_log_printer)
550
if (slow_log_printer & LOG_NONE)
552
slow_log_handler_list[0]= 0;
556
slow_log_handler_list[0]= file_log_handler;
557
slow_log_handler_list[1]= 0;
560
void LOGGER::init_general_log(uint general_log_printer)
562
if (general_log_printer & LOG_NONE)
564
general_log_handler_list[0]= 0;
568
general_log_handler_list[0]= file_log_handler;
569
general_log_handler_list[1]= 0;
573
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
576
DRIZZLE_QUERY_LOG *file_log;
583
file_log= file_log_handler->get_mysql_slow_log();
585
file_log->open_slow_log(sys_var_slow_log_path.value);
586
init_slow_log(log_output_options);
590
case QUERY_LOG_GENERAL:
593
file_log= file_log_handler->get_mysql_log();
595
file_log->open_query_log(sys_var_general_log_path.value);
596
init_general_log(log_output_options);
608
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
612
DRIZZLE_LOG *file_log;
616
tmp_opt= &opt_slow_log;
617
file_log= file_log_handler->get_mysql_slow_log();
619
case QUERY_LOG_GENERAL:
621
file_log= file_log_handler->get_mysql_log();
624
assert(0); // Impossible
636
int LOGGER::set_handlers(uint error_log_printer,
637
uint slow_log_printer,
638
uint general_log_printer)
300
int LOGGER::set_handlers(uint error_log_printer)
640
302
/* error log table is not supported yet */
641
303
lock_exclusive();
643
305
init_error_log(error_log_printer);
644
init_slow_log(slow_log_printer);
645
init_general_log(general_log_printer);
1366
Reopen the log file. The method is used during FLUSH LOGS
1367
and locks LOCK_log mutex
1371
void DRIZZLE_QUERY_LOG::reopen_file()
1380
pthread_mutex_lock(&LOCK_log);
1383
name= 0; // Don't free name
1384
close(LOG_CLOSE_TO_BE_OPENED);
1387
Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1390
open(save_name, log_type, 0, io_cache_type);
1391
my_free(save_name, MYF(0));
1393
pthread_mutex_unlock(&LOCK_log);
1400
Write a command to traditional general log file
1405
event_time command start timestamp
1406
user_host the pointer to the string with user@host info
1407
user_host_len length of the user_host string. this is computed once
1408
and passed to all general log event handlers
1409
thread_id Id of the thread, issued a query
1410
command_type the type of the command being logged
1411
command_type_len the length of the string above
1412
sql_text the very text of the query being executed
1413
sql_text_len the length of sql_text string
1417
Log given command to to normal (not rotable) log file
1421
TRUE - error occured
1424
bool DRIZZLE_QUERY_LOG::write(time_t event_time,
1425
const char *user_host __attribute__((unused)),
1426
uint user_host_len __attribute__((unused)),
1428
const char *command_type, uint command_type_len,
1429
const char *sql_text, uint sql_text_len)
1433
char local_time_buff[MAX_TIME_SIZE];
1435
uint time_buff_len= 0;
1437
(void) pthread_mutex_lock(&LOCK_log);
1439
/* Test if someone closed between the is_open test and lock */
1442
/* Note that my_b_write() assumes it knows the length for this */
1443
if (event_time != last_time)
1445
last_time= event_time;
1447
localtime_r(&event_time, &start);
1449
time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
1450
"%02d%02d%02d %2d:%02d:%02d",
1451
start.tm_year % 100, start.tm_mon + 1,
1452
start.tm_mday, start.tm_hour,
1453
start.tm_min, start.tm_sec);
1455
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1459
if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1462
/* command_type, thread_id */
1463
length= snprintf(buff, 32, "%5ld ", (long) thread_id);
1465
if (my_b_write(&log_file, (uchar*) buff, length))
1468
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1471
if (my_b_write(&log_file, (uchar*) "\t", 1))
1475
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1478
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1479
flush_io_cache(&log_file))
1483
(void) pthread_mutex_unlock(&LOCK_log);
1490
sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1492
(void) pthread_mutex_unlock(&LOCK_log);
1498
Log a query to the traditional slow log file
1503
thd THD of the query
1504
current_time current timestamp
1505
query_start_arg command start timestamp
1506
user_host the pointer to the string with user@host info
1507
user_host_len length of the user_host string. this is computed once
1508
and passed to all general log event handlers
1509
query_utime Amount of time the query took to execute (in microseconds)
1510
lock_utime Amount of time the query was locked (in microseconds)
1511
is_command The flag, which determines, whether the sql_text is a
1512
query or an administrator command.
1513
sql_text the very text of the query or administrator command
1515
sql_text_len the length of sql_text string
1519
Log a query to the slow log file.
1523
TRUE - error occured
1526
bool DRIZZLE_QUERY_LOG::write(THD *thd, time_t current_time,
1527
time_t query_start_arg __attribute__((unused)),
1528
const char *user_host,
1529
uint user_host_len, uint64_t query_utime,
1530
uint64_t lock_utime, bool is_command,
1531
const char *sql_text, uint sql_text_len)
1535
(void) pthread_mutex_lock(&LOCK_log);
1539
(void) pthread_mutex_unlock(&LOCK_log);
1544
{ // Safety agains reopen
1546
char buff[80], *end;
1547
char query_time_buff[22+7], lock_time_buff[22+7];
1552
if (current_time != last_time)
1554
last_time= current_time;
1556
localtime_r(¤t_time, &start);
1558
buff_len= snprintf(buff, sizeof buff,
1559
"# Time: %02d%02d%02d %2d:%02d:%02d\n",
1560
start.tm_year % 100, start.tm_mon + 1,
1561
start.tm_mday, start.tm_hour,
1562
start.tm_min, start.tm_sec);
1564
/* Note that my_b_write() assumes it knows the length for this */
1565
if (my_b_write(&log_file, (uchar*) buff, buff_len))
1568
const uchar uh[]= "# User@Host: ";
1569
if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1571
if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1573
if (my_b_write(&log_file, (uchar*) "\n", 1))
1576
/* For slow query log */
1577
sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
1578
sprintf(lock_time_buff, "%.6f", uint64_t2double(lock_utime)/1000000.0);
1579
if (my_b_printf(&log_file,
1580
"# Query_time: %s Lock_time: %s"
1581
" Rows_sent: %lu Rows_examined: %lu\n",
1582
query_time_buff, lock_time_buff,
1583
(ulong) thd->sent_row_count,
1584
(ulong) thd->examined_row_count) == (uint) -1)
1586
if (thd->db && strcmp(thd->db, db))
1587
{ // Database changed
1588
if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1590
my_stpcpy(db,thd->db);
1592
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1594
end=my_stpcpy(end, ",last_insert_id=");
1595
end=int64_t10_to_str((int64_t)
1596
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1599
// Save value if we do an insert.
1600
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1603
end=my_stpcpy(end,",insert_id=");
1604
end=int64_t10_to_str((int64_t)
1605
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1611
This info used to show up randomly, depending on whether the query
1612
checked the query start time or not. now we always write current
1613
timestamp to the slow log
1615
end= my_stpcpy(end, ",timestamp=");
1616
end= int10_to_str((long) current_time, end, 10);
1622
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1623
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1628
end= strxmov(buff, "# administrator command: ", NullS);
1629
buff_len= (ulong) (end - buff);
1630
my_b_write(&log_file, (uchar*) buff, buff_len);
1632
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1633
my_b_write(&log_file, (uchar*) ";\n",2) ||
1634
flush_io_cache(&log_file))
1642
sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1646
(void) pthread_mutex_unlock(&LOCK_log);
1653
1020
The following should be using fn_format(); We just need to