206
205
/* Check if a given table is opened log table */
207
int check_if_log_table(uint32_t db_len __attribute__((unused)),
206
int check_if_log_table(uint db_len __attribute__((unused)),
208
207
const char *db __attribute__((unused)),
209
uint32_t table_name_len __attribute__((unused)),
208
uint table_name_len __attribute__((unused)),
210
209
const char *table_name __attribute__((unused)),
211
uint32_t check_if_opened __attribute__((unused)))
210
uint check_if_opened __attribute__((unused)))
215
/* log event handlers */
217
bool Log_to_file_event_handler::
218
log_error(enum loglevel level, const char *format,
221
return vprint_msg_to_log(level, format, args);
224
void Log_to_file_event_handler::init_pthread_objects()
226
mysql_log.init_pthread_objects();
227
mysql_slow_log.init_pthread_objects();
231
/** Wrapper around MYSQL_LOG::write() for slow log. */
233
bool Log_to_file_event_handler::
234
log_slow(THD *thd, time_t current_time, time_t query_start_arg,
235
const char *user_host, uint user_host_len,
236
uint64_t query_utime, uint64_t lock_utime, bool is_command,
237
const char *sql_text, uint sql_text_len)
239
return mysql_slow_log.write(thd, current_time, query_start_arg,
240
user_host, user_host_len,
241
query_utime, lock_utime, is_command,
242
sql_text, sql_text_len);
247
Wrapper around MYSQL_LOG::write() for general log. We need it since we
248
want all log event handlers to have the same signature.
251
bool Log_to_file_event_handler::
252
log_general(THD *thd __attribute__((unused)),
253
time_t event_time, const char *user_host,
254
uint user_host_len, int thread_id,
255
const char *command_type, uint command_type_len,
256
const char *sql_text, uint sql_text_len,
257
CHARSET_INFO *client_cs __attribute__((unused)))
259
return mysql_log.write(event_time, user_host, user_host_len,
260
thread_id, command_type, command_type_len,
261
sql_text, sql_text_len);
265
bool Log_to_file_event_handler::init()
270
mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
273
mysql_log.open_query_log(sys_var_general_log_path.value);
275
is_initialized= true;
282
void Log_to_file_event_handler::cleanup()
285
mysql_slow_log.cleanup();
288
void Log_to_file_event_handler::flush()
290
/* reopen log files */
292
mysql_log.reopen_file();
294
mysql_slow_log.reopen_file();
217
298
Log error with all enabled log event handlers
283
377
logger.lock_exclusive();
379
/* reopen log files */
380
file_log_handler->flush();
285
382
/* end of log flush */
290
void LOGGER::init_error_log(uint32_t error_log_printer)
389
Log slow query with all enabled log event handlers
394
thd THD of the query being logged
395
query The query being logged
396
query_length The length of the query string
397
current_utime Current time in microseconds (from undefined start)
404
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
405
uint64_t current_utime)
409
Log_event_handler **current_handler;
410
bool is_command= false;
411
char user_host_buff[MAX_USER_HOST_SIZE];
412
Security_context *sctx= thd->security_ctx;
413
uint user_host_len= 0;
414
uint64_t query_utime, lock_utime;
417
Print the message to the buffer if we have slow log enabled
420
if (*slow_log_handler_list)
424
/* do not log slow queries from replication threads */
425
if (thd->slave_thread && !opt_log_slow_slave_statements)
435
/* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
436
user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
437
sctx->user, "[", sctx->user, "] @ ",
439
sctx->ip, "]", NullS) -
442
current_time= my_time_possible_from_micro(current_utime);
443
if (thd->start_utime)
445
query_utime= (current_utime - thd->start_utime);
446
lock_utime= (thd->utime_after_lock - thd->start_utime);
450
query_utime= lock_utime= 0;
456
query= command_name[thd->command].str;
457
query_length= command_name[thd->command].length;
460
for (current_handler= slow_log_handler_list; *current_handler ;)
461
error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
462
user_host_buff, user_host_len,
463
query_utime, lock_utime, is_command,
464
query, query_length) || error;
471
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
472
const char *query, uint query_length)
475
Log_event_handler **current_handler= general_log_handler_list;
476
char user_host_buff[MAX_USER_HOST_SIZE];
477
Security_context *sctx= thd->security_ctx;
479
uint user_host_len= 0;
483
id= thd->thread_id; /* Normal thread */
485
id= 0; /* Log from connect handler */
493
user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
497
sctx->ip, "]", NullS) -
500
current_time= my_time(0);
502
while (*current_handler)
503
error|= (*current_handler++)->
504
log_general(thd, current_time, user_host_buff,
506
command_name[(uint) command].str,
507
command_name[(uint) command].length,
509
thd->variables.character_set_client) || error;
515
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
516
const char *format, va_list args)
518
uint message_buff_len= 0;
519
char message_buff[MAX_LOG_BUFFER_SIZE];
521
/* prepare message */
523
message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
526
message_buff[0]= '\0';
528
return general_log_write(thd, command, message_buff, message_buff_len);
531
void LOGGER::init_error_log(uint error_log_printer)
292
533
if (error_log_printer & LOG_NONE)
300
int LOGGER::set_handlers(uint32_t error_log_printer)
539
switch (error_log_printer) {
541
error_log_handler_list[0]= file_log_handler;
542
error_log_handler_list[1]= 0;
547
void LOGGER::init_slow_log(uint slow_log_printer)
549
if (slow_log_printer & LOG_NONE)
551
slow_log_handler_list[0]= 0;
555
slow_log_handler_list[0]= file_log_handler;
556
slow_log_handler_list[1]= 0;
559
void LOGGER::init_general_log(uint general_log_printer)
561
if (general_log_printer & LOG_NONE)
563
general_log_handler_list[0]= 0;
567
general_log_handler_list[0]= file_log_handler;
568
general_log_handler_list[1]= 0;
572
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
575
MYSQL_QUERY_LOG *file_log;
582
file_log= file_log_handler->get_mysql_slow_log();
584
file_log->open_slow_log(sys_var_slow_log_path.value);
585
init_slow_log(log_output_options);
589
case QUERY_LOG_GENERAL:
592
file_log= file_log_handler->get_mysql_log();
594
file_log->open_query_log(sys_var_general_log_path.value);
595
init_general_log(log_output_options);
607
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
615
tmp_opt= &opt_slow_log;
616
file_log= file_log_handler->get_mysql_slow_log();
618
case QUERY_LOG_GENERAL:
620
file_log= file_log_handler->get_mysql_log();
623
assert(0); // Impossible
635
int LOGGER::set_handlers(uint error_log_printer,
636
uint slow_log_printer,
637
uint general_log_printer)
302
639
/* error log table is not supported yet */
303
640
lock_exclusive();
305
642
init_error_log(error_log_printer);
643
init_slow_log(slow_log_printer);
644
init_general_log(general_log_printer);
1363
Reopen the log file. The method is used during FLUSH LOGS
1364
and locks LOCK_log mutex
1368
void MYSQL_QUERY_LOG::reopen_file()
1377
pthread_mutex_lock(&LOCK_log);
1380
name= 0; // Don't free name
1381
close(LOG_CLOSE_TO_BE_OPENED);
1384
Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1387
open(save_name, log_type, 0, io_cache_type);
1388
my_free(save_name, MYF(0));
1390
pthread_mutex_unlock(&LOCK_log);
1397
Write a command to traditional general log file
1402
event_time command start timestamp
1403
user_host the pointer to the string with user@host info
1404
user_host_len length of the user_host string. this is computed once
1405
and passed to all general log event handlers
1406
thread_id Id of the thread, issued a query
1407
command_type the type of the command being logged
1408
command_type_len the length of the string above
1409
sql_text the very text of the query being executed
1410
sql_text_len the length of sql_text string
1414
Log given command to to normal (not rotable) log file
1418
TRUE - error occured
1421
bool MYSQL_QUERY_LOG::write(time_t event_time,
1422
const char *user_host __attribute__((unused)),
1423
uint user_host_len __attribute__((unused)),
1425
const char *command_type, uint command_type_len,
1426
const char *sql_text, uint sql_text_len)
1430
char local_time_buff[MAX_TIME_SIZE];
1432
uint time_buff_len= 0;
1434
(void) pthread_mutex_lock(&LOCK_log);
1436
/* Test if someone closed between the is_open test and lock */
1439
/* Note that my_b_write() assumes it knows the length for this */
1440
if (event_time != last_time)
1442
last_time= event_time;
1444
localtime_r(&event_time, &start);
1446
time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
1447
"%02d%02d%02d %2d:%02d:%02d",
1448
start.tm_year % 100, start.tm_mon + 1,
1449
start.tm_mday, start.tm_hour,
1450
start.tm_min, start.tm_sec);
1452
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1456
if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1459
/* command_type, thread_id */
1460
length= snprintf(buff, 32, "%5ld ", (long) thread_id);
1462
if (my_b_write(&log_file, (uchar*) buff, length))
1465
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1468
if (my_b_write(&log_file, (uchar*) "\t", 1))
1472
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1475
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1476
flush_io_cache(&log_file))
1480
(void) pthread_mutex_unlock(&LOCK_log);
1487
sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1489
(void) pthread_mutex_unlock(&LOCK_log);
1495
Log a query to the traditional slow log file
1500
thd THD of the query
1501
current_time current timestamp
1502
query_start_arg command start timestamp
1503
user_host the pointer to the string with user@host info
1504
user_host_len length of the user_host string. this is computed once
1505
and passed to all general log event handlers
1506
query_utime Amount of time the query took to execute (in microseconds)
1507
lock_utime Amount of time the query was locked (in microseconds)
1508
is_command The flag, which determines, whether the sql_text is a
1509
query or an administrator command.
1510
sql_text the very text of the query or administrator command
1512
sql_text_len the length of sql_text string
1516
Log a query to the slow log file.
1520
TRUE - error occured
1523
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
1524
time_t query_start_arg __attribute__((unused)),
1525
const char *user_host,
1526
uint user_host_len, uint64_t query_utime,
1527
uint64_t lock_utime, bool is_command,
1528
const char *sql_text, uint sql_text_len)
1532
(void) pthread_mutex_lock(&LOCK_log);
1536
(void) pthread_mutex_unlock(&LOCK_log);
1541
{ // Safety agains reopen
1543
char buff[80], *end;
1544
char query_time_buff[22+7], lock_time_buff[22+7];
1548
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1550
if (current_time != last_time)
1552
last_time= current_time;
1554
localtime_r(¤t_time, &start);
1556
buff_len= snprintf(buff, sizeof buff,
1557
"# Time: %02d%02d%02d %2d:%02d:%02d\n",
1558
start.tm_year % 100, start.tm_mon + 1,
1559
start.tm_mday, start.tm_hour,
1560
start.tm_min, start.tm_sec);
1562
/* Note that my_b_write() assumes it knows the length for this */
1563
if (my_b_write(&log_file, (uchar*) buff, buff_len))
1566
const uchar uh[]= "# User@Host: ";
1567
if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1569
if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1571
if (my_b_write(&log_file, (uchar*) "\n", 1))
1574
/* For slow query log */
1575
sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
1576
sprintf(lock_time_buff, "%.6f", uint64_t2double(lock_utime)/1000000.0);
1577
if (my_b_printf(&log_file,
1578
"# Query_time: %s Lock_time: %s"
1579
" Rows_sent: %lu Rows_examined: %lu\n",
1580
query_time_buff, lock_time_buff,
1581
(ulong) thd->sent_row_count,
1582
(ulong) thd->examined_row_count) == (uint) -1)
1584
if (thd->db && strcmp(thd->db, db))
1585
{ // Database changed
1586
if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1590
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1592
end=strmov(end, ",last_insert_id=");
1593
end=int64_t10_to_str((int64_t)
1594
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1597
// Save value if we do an insert.
1598
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1600
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1602
end=strmov(end,",insert_id=");
1603
end=int64_t10_to_str((int64_t)
1604
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1610
This info used to show up randomly, depending on whether the query
1611
checked the query start time or not. now we always write current
1612
timestamp to the slow log
1614
end= strmov(end, ",timestamp=");
1615
end= int10_to_str((long) current_time, end, 10);
1621
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1622
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1627
end= strxmov(buff, "# administrator command: ", NullS);
1628
buff_len= (ulong) (end - buff);
1629
my_b_write(&log_file, (uchar*) buff, buff_len);
1631
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1632
my_b_write(&log_file, (uchar*) ";\n",2) ||
1633
flush_io_cache(&log_file))
1641
sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1645
(void) pthread_mutex_unlock(&LOCK_log);
1028
1652
The following should be using fn_format(); We just need to
1029
1653
first change fn_format() to cut the file name if it's too long.
1031
const char *DRIZZLE_LOG::generate_name(const char *log_name,
1655
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1656
const char *suffix,
1033
1657
bool strip_ext, char *buff)
2656
3286
return logger.error_log_print(level, format, args);
2659
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
3290
bool slow_log_print(THD *thd, const char *query, uint query_length,
3291
uint64_t current_utime)
3293
return logger.slow_log_print(thd, query, query_length, current_utime);
3297
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
3300
Log command if we have at least one log event handler enabled and want
3301
to log this king of commands
3303
if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
3305
if (thd->options & OPTION_LOG_OFF)
3318
bool general_log_print(THD *thd, enum enum_server_command command,
3319
const char *format, ...)
3324
/* Print the message to the buffer if we want to log this king of commands */
3325
if (! logger.log_command(thd, command))
3328
va_start(args, format);
3329
error= logger.general_log_print(thd, command, format, args);
3335
bool general_log_write(THD *thd, enum enum_server_command command,
3336
const char *query, uint query_length)
3338
/* Write the message to the log if we want to log this king of commands */
3339
if (logger.log_command(thd, command))
3340
return logger.general_log_write(thd, command, query, query_length);
3345
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2661
3347
if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3348
pthread_mutex_lock(&LOCK_log);