206
205
/* Check if a given table is opened log table */
207
int check_if_log_table(uint32_t db_len __attribute__((unused)),
208
const char *db __attribute__((unused)),
209
uint32_t table_name_len __attribute__((unused)),
210
const char *table_name __attribute__((unused)),
211
uint32_t check_if_opened __attribute__((unused)))
206
int check_if_log_table(uint db_len __attribute__((__unused__)),
207
const char *db __attribute__((__unused__)),
208
uint table_name_len __attribute__((__unused__)),
209
const char *table_name __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->priv_user ? sctx->priv_user : "", "[",
438
sctx->user ? sctx->user : "", "] @ ",
439
sctx->host ? sctx->host : "", " [",
440
sctx->ip ? 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,
495
sctx->priv_user ? sctx->priv_user : "", "[",
496
sctx->user ? sctx->user : "", "] @ ",
497
sctx->host ? sctx->host : "", " [",
498
sctx->ip ? 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
void LOGGER::init_error_log(uint error_log_printer)
292
534
if (error_log_printer & LOG_NONE)
300
int LOGGER::set_handlers(uint32_t error_log_printer)
540
switch (error_log_printer) {
542
error_log_handler_list[0]= file_log_handler;
543
error_log_handler_list[1]= 0;
545
/* these two are disabled for now */
549
case LOG_TABLE|LOG_FILE:
555
void LOGGER::init_slow_log(uint slow_log_printer)
557
if (slow_log_printer & LOG_NONE)
559
slow_log_handler_list[0]= 0;
563
slow_log_handler_list[0]= file_log_handler;
564
slow_log_handler_list[1]= 0;
567
void LOGGER::init_general_log(uint general_log_printer)
569
if (general_log_printer & LOG_NONE)
571
general_log_handler_list[0]= 0;
575
general_log_handler_list[0]= file_log_handler;
576
general_log_handler_list[1]= 0;
580
bool LOGGER::activate_log_handler(THD* thd __attribute__((__unused__)),
583
MYSQL_QUERY_LOG *file_log;
590
file_log= file_log_handler->get_mysql_slow_log();
592
file_log->open_slow_log(sys_var_slow_log_path.value);
593
init_slow_log(log_output_options);
597
case QUERY_LOG_GENERAL:
600
file_log= file_log_handler->get_mysql_log();
602
file_log->open_query_log(sys_var_general_log_path.value);
603
init_general_log(log_output_options);
615
void LOGGER::deactivate_log_handler(THD *thd __attribute__((__unused__)),
623
tmp_opt= &opt_slow_log;
624
file_log= file_log_handler->get_mysql_slow_log();
626
case QUERY_LOG_GENERAL:
628
file_log= file_log_handler->get_mysql_log();
631
assert(0); // Impossible
643
int LOGGER::set_handlers(uint error_log_printer,
644
uint slow_log_printer,
645
uint general_log_printer)
302
647
/* error log table is not supported yet */
648
assert(error_log_printer < LOG_TABLE);
303
650
lock_exclusive();
305
652
init_error_log(error_log_printer);
653
init_slow_log(slow_log_printer);
654
init_general_log(general_log_printer);
1372
Reopen the log file. The method is used during FLUSH LOGS
1373
and locks LOCK_log mutex
1377
void MYSQL_QUERY_LOG::reopen_file()
1386
pthread_mutex_lock(&LOCK_log);
1389
name= 0; // Don't free name
1390
close(LOG_CLOSE_TO_BE_OPENED);
1393
Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1396
open(save_name, log_type, 0, io_cache_type);
1397
my_free(save_name, MYF(0));
1399
pthread_mutex_unlock(&LOCK_log);
1406
Write a command to traditional general log file
1411
event_time command start timestamp
1412
user_host the pointer to the string with user@host info
1413
user_host_len length of the user_host string. this is computed once
1414
and passed to all general log event handlers
1415
thread_id Id of the thread, issued a query
1416
command_type the type of the command being logged
1417
command_type_len the length of the string above
1418
sql_text the very text of the query being executed
1419
sql_text_len the length of sql_text string
1423
Log given command to to normal (not rotable) log file
1427
TRUE - error occured
1430
bool MYSQL_QUERY_LOG::write(time_t event_time,
1431
const char *user_host __attribute__((__unused__)),
1432
uint user_host_len __attribute__((__unused__)),
1434
const char *command_type, uint command_type_len,
1435
const char *sql_text, uint sql_text_len)
1439
char local_time_buff[MAX_TIME_SIZE];
1441
uint time_buff_len= 0;
1443
(void) pthread_mutex_lock(&LOCK_log);
1445
/* Test if someone closed between the is_open test and lock */
1448
/* Note that my_b_write() assumes it knows the length for this */
1449
if (event_time != last_time)
1451
last_time= event_time;
1453
localtime_r(&event_time, &start);
1455
time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
1456
"%02d%02d%02d %2d:%02d:%02d",
1457
start.tm_year % 100, start.tm_mon + 1,
1458
start.tm_mday, start.tm_hour,
1459
start.tm_min, start.tm_sec);
1461
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1465
if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1468
/* command_type, thread_id */
1469
length= snprintf(buff, 32, "%5ld ", (long) thread_id);
1471
if (my_b_write(&log_file, (uchar*) buff, length))
1474
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1477
if (my_b_write(&log_file, (uchar*) "\t", 1))
1481
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1484
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1485
flush_io_cache(&log_file))
1489
(void) pthread_mutex_unlock(&LOCK_log);
1496
sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1498
(void) pthread_mutex_unlock(&LOCK_log);
1504
Log a query to the traditional slow log file
1509
thd THD of the query
1510
current_time current timestamp
1511
query_start_arg command start timestamp
1512
user_host the pointer to the string with user@host info
1513
user_host_len length of the user_host string. this is computed once
1514
and passed to all general log event handlers
1515
query_utime Amount of time the query took to execute (in microseconds)
1516
lock_utime Amount of time the query was locked (in microseconds)
1517
is_command The flag, which determines, whether the sql_text is a
1518
query or an administrator command.
1519
sql_text the very text of the query or administrator command
1521
sql_text_len the length of sql_text string
1525
Log a query to the slow log file.
1529
TRUE - error occured
1532
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
1533
time_t query_start_arg __attribute__((__unused__)),
1534
const char *user_host,
1535
uint user_host_len, uint64_t query_utime,
1536
uint64_t lock_utime, bool is_command,
1537
const char *sql_text, uint sql_text_len)
1541
(void) pthread_mutex_lock(&LOCK_log);
1545
(void) pthread_mutex_unlock(&LOCK_log);
1550
{ // Safety agains reopen
1552
char buff[80], *end;
1553
char query_time_buff[22+7], lock_time_buff[22+7];
1557
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1559
if (current_time != last_time)
1561
last_time= current_time;
1563
localtime_r(¤t_time, &start);
1565
buff_len= snprintf(buff, sizeof buff,
1566
"# Time: %02d%02d%02d %2d:%02d:%02d\n",
1567
start.tm_year % 100, start.tm_mon + 1,
1568
start.tm_mday, start.tm_hour,
1569
start.tm_min, start.tm_sec);
1571
/* Note that my_b_write() assumes it knows the length for this */
1572
if (my_b_write(&log_file, (uchar*) buff, buff_len))
1575
const uchar uh[]= "# User@Host: ";
1576
if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1578
if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1580
if (my_b_write(&log_file, (uchar*) "\n", 1))
1583
/* For slow query log */
1584
sprintf(query_time_buff, "%.6f", uint64_t2double(query_utime)/1000000.0);
1585
sprintf(lock_time_buff, "%.6f", uint64_t2double(lock_utime)/1000000.0);
1586
if (my_b_printf(&log_file,
1587
"# Query_time: %s Lock_time: %s"
1588
" Rows_sent: %lu Rows_examined: %lu\n",
1589
query_time_buff, lock_time_buff,
1590
(ulong) thd->sent_row_count,
1591
(ulong) thd->examined_row_count) == (uint) -1)
1593
if (thd->db && strcmp(thd->db, db))
1594
{ // Database changed
1595
if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1599
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1601
end=strmov(end, ",last_insert_id=");
1602
end=int64_t10_to_str((int64_t)
1603
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1606
// Save value if we do an insert.
1607
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1609
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1611
end=strmov(end,",insert_id=");
1612
end=int64_t10_to_str((int64_t)
1613
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1619
This info used to show up randomly, depending on whether the query
1620
checked the query start time or not. now we always write current
1621
timestamp to the slow log
1623
end= strmov(end, ",timestamp=");
1624
end= int10_to_str((long) current_time, end, 10);
1630
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1631
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1636
end= strxmov(buff, "# administrator command: ", NullS);
1637
buff_len= (ulong) (end - buff);
1638
my_b_write(&log_file, (uchar*) buff, buff_len);
1640
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1641
my_b_write(&log_file, (uchar*) ";\n",2) ||
1642
flush_io_cache(&log_file))
1650
sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1654
(void) pthread_mutex_unlock(&LOCK_log);
1028
1661
The following should be using fn_format(); We just need to
1029
1662
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,
1664
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1665
const char *suffix,
1033
1666
bool strip_ext, char *buff)
2656
3295
return logger.error_log_print(level, format, args);
2659
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
3299
bool slow_log_print(THD *thd, const char *query, uint query_length,
3300
uint64_t current_utime)
3302
return logger.slow_log_print(thd, query, query_length, current_utime);
3306
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
3309
Log command if we have at least one log event handler enabled and want
3310
to log this king of commands
3312
if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
3314
if (thd->options & OPTION_LOG_OFF)
3327
bool general_log_print(THD *thd, enum enum_server_command command,
3328
const char *format, ...)
3333
/* Print the message to the buffer if we want to log this king of commands */
3334
if (! logger.log_command(thd, command))
3337
va_start(args, format);
3338
error= logger.general_log_print(thd, command, format, args);
3344
bool general_log_write(THD *thd, enum enum_server_command command,
3345
const char *query, uint query_length)
3347
/* Write the message to the log if we want to log this king of commands */
3348
if (logger.log_command(thd, command))
3349
return logger.general_log_write(thd, command, query, query_length);
3354
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2661
3356
if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3357
pthread_mutex_lock(&LOCK_log);