206
239
/* 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)))
240
int check_if_log_table(uint db_len __attribute__((__unused__)),
241
const char *db __attribute__((__unused__)),
242
uint table_name_len __attribute__((__unused__)),
243
const char *table_name __attribute__((__unused__)),
244
uint check_if_opened __attribute__((__unused__)))
249
/* log event handlers */
251
bool Log_to_file_event_handler::
252
log_error(enum loglevel level, const char *format,
255
return vprint_msg_to_log(level, format, args);
258
void Log_to_file_event_handler::init_pthread_objects()
260
mysql_log.init_pthread_objects();
261
mysql_slow_log.init_pthread_objects();
265
/** Wrapper around MYSQL_LOG::write() for slow log. */
267
bool Log_to_file_event_handler::
268
log_slow(THD *thd, time_t current_time, time_t query_start_arg,
269
const char *user_host, uint user_host_len,
270
ulonglong query_utime, ulonglong lock_utime, bool is_command,
271
const char *sql_text, uint sql_text_len)
273
return mysql_slow_log.write(thd, current_time, query_start_arg,
274
user_host, user_host_len,
275
query_utime, lock_utime, is_command,
276
sql_text, sql_text_len);
281
Wrapper around MYSQL_LOG::write() for general log. We need it since we
282
want all log event handlers to have the same signature.
285
bool Log_to_file_event_handler::
286
log_general(THD *thd __attribute__((__unused__)),
287
time_t event_time, const char *user_host,
288
uint user_host_len, int thread_id,
289
const char *command_type, uint command_type_len,
290
const char *sql_text, uint sql_text_len,
291
CHARSET_INFO *client_cs __attribute__((__unused__)))
293
return mysql_log.write(event_time, user_host, user_host_len,
294
thread_id, command_type, command_type_len,
295
sql_text, sql_text_len);
299
bool Log_to_file_event_handler::init()
304
mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
307
mysql_log.open_query_log(sys_var_general_log_path.value);
309
is_initialized= true;
316
void Log_to_file_event_handler::cleanup()
319
mysql_slow_log.cleanup();
322
void Log_to_file_event_handler::flush()
324
/* reopen log files */
326
mysql_log.reopen_file();
328
mysql_slow_log.reopen_file();
217
332
Log error with all enabled log event handlers
283
411
logger.lock_exclusive();
413
/* reopen log files */
414
file_log_handler->flush();
285
416
/* end of log flush */
290
void LOGGER::init_error_log(uint32_t error_log_printer)
423
Log slow query with all enabled log event handlers
428
thd THD of the query being logged
429
query The query being logged
430
query_length The length of the query string
431
current_utime Current time in microseconds (from undefined start)
438
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
439
ulonglong current_utime)
443
Log_event_handler **current_handler;
444
bool is_command= false;
445
char user_host_buff[MAX_USER_HOST_SIZE];
446
Security_context *sctx= thd->security_ctx;
447
uint user_host_len= 0;
448
ulonglong query_utime, lock_utime;
451
Print the message to the buffer if we have slow log enabled
454
if (*slow_log_handler_list)
458
/* do not log slow queries from replication threads */
459
if (thd->slave_thread && !opt_log_slow_slave_statements)
469
/* fill in user_host value: the format is "%s[%s] @ %s [%s]" */
470
user_host_len= (strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
471
sctx->priv_user ? sctx->priv_user : "", "[",
472
sctx->user ? sctx->user : "", "] @ ",
473
sctx->host ? sctx->host : "", " [",
474
sctx->ip ? sctx->ip : "", "]", NullS) -
477
current_time= my_time_possible_from_micro(current_utime);
478
if (thd->start_utime)
480
query_utime= (current_utime - thd->start_utime);
481
lock_utime= (thd->utime_after_lock - thd->start_utime);
485
query_utime= lock_utime= 0;
491
query= command_name[thd->command].str;
492
query_length= command_name[thd->command].length;
495
for (current_handler= slow_log_handler_list; *current_handler ;)
496
error= (*current_handler++)->log_slow(thd, current_time, thd->start_time,
497
user_host_buff, user_host_len,
498
query_utime, lock_utime, is_command,
499
query, query_length) || error;
506
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
507
const char *query, uint query_length)
510
Log_event_handler **current_handler= general_log_handler_list;
511
char user_host_buff[MAX_USER_HOST_SIZE];
512
Security_context *sctx= thd->security_ctx;
514
uint user_host_len= 0;
518
id= thd->thread_id; /* Normal thread */
520
id= 0; /* Log from connect handler */
528
user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
529
sctx->priv_user ? sctx->priv_user : "", "[",
530
sctx->user ? sctx->user : "", "] @ ",
531
sctx->host ? sctx->host : "", " [",
532
sctx->ip ? sctx->ip : "", "]", NullS) -
535
current_time= my_time(0);
537
while (*current_handler)
538
error|= (*current_handler++)->
539
log_general(thd, current_time, user_host_buff,
541
command_name[(uint) command].str,
542
command_name[(uint) command].length,
544
thd->variables.character_set_client) || error;
550
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
551
const char *format, va_list args)
553
uint message_buff_len= 0;
554
char message_buff[MAX_LOG_BUFFER_SIZE];
556
/* prepare message */
558
message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
561
message_buff[0]= '\0';
563
return general_log_write(thd, command, message_buff, message_buff_len);
566
void LOGGER::init_error_log(uint error_log_printer)
292
568
if (error_log_printer & LOG_NONE)
300
int LOGGER::set_handlers(uint32_t error_log_printer)
574
switch (error_log_printer) {
576
error_log_handler_list[0]= file_log_handler;
577
error_log_handler_list[1]= 0;
579
/* these two are disabled for now */
583
case LOG_TABLE|LOG_FILE:
589
void LOGGER::init_slow_log(uint slow_log_printer)
591
if (slow_log_printer & LOG_NONE)
593
slow_log_handler_list[0]= 0;
597
slow_log_handler_list[0]= file_log_handler;
598
slow_log_handler_list[1]= 0;
601
void LOGGER::init_general_log(uint general_log_printer)
603
if (general_log_printer & LOG_NONE)
605
general_log_handler_list[0]= 0;
609
general_log_handler_list[0]= file_log_handler;
610
general_log_handler_list[1]= 0;
614
bool LOGGER::activate_log_handler(THD* thd __attribute__((__unused__)),
617
MYSQL_QUERY_LOG *file_log;
624
file_log= file_log_handler->get_mysql_slow_log();
626
file_log->open_slow_log(sys_var_slow_log_path.value);
627
init_slow_log(log_output_options);
631
case QUERY_LOG_GENERAL:
634
file_log= file_log_handler->get_mysql_log();
636
file_log->open_query_log(sys_var_general_log_path.value);
637
init_general_log(log_output_options);
649
void LOGGER::deactivate_log_handler(THD *thd __attribute__((__unused__)),
657
tmp_opt= &opt_slow_log;
658
file_log= file_log_handler->get_mysql_slow_log();
660
case QUERY_LOG_GENERAL:
662
file_log= file_log_handler->get_mysql_log();
665
assert(0); // Impossible
677
int LOGGER::set_handlers(uint error_log_printer,
678
uint slow_log_printer,
679
uint general_log_printer)
302
681
/* error log table is not supported yet */
682
assert(error_log_printer < LOG_TABLE);
303
684
lock_exclusive();
305
686
init_error_log(error_log_printer);
687
init_slow_log(slow_log_printer);
688
init_general_log(general_log_printer);
1406
Reopen the log file. The method is used during FLUSH LOGS
1407
and locks LOCK_log mutex
1411
void MYSQL_QUERY_LOG::reopen_file()
1420
pthread_mutex_lock(&LOCK_log);
1423
name= 0; // Don't free name
1424
close(LOG_CLOSE_TO_BE_OPENED);
1427
Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1430
open(save_name, log_type, 0, io_cache_type);
1431
my_free(save_name, MYF(0));
1433
pthread_mutex_unlock(&LOCK_log);
1440
Write a command to traditional general log file
1445
event_time command start timestamp
1446
user_host the pointer to the string with user@host info
1447
user_host_len length of the user_host string. this is computed once
1448
and passed to all general log event handlers
1449
thread_id Id of the thread, issued a query
1450
command_type the type of the command being logged
1451
command_type_len the length of the string above
1452
sql_text the very text of the query being executed
1453
sql_text_len the length of sql_text string
1457
Log given command to to normal (not rotable) log file
1461
TRUE - error occured
1464
bool MYSQL_QUERY_LOG::write(time_t event_time,
1465
const char *user_host __attribute__((__unused__)),
1466
uint user_host_len __attribute__((__unused__)),
1468
const char *command_type, uint command_type_len,
1469
const char *sql_text, uint sql_text_len)
1473
char local_time_buff[MAX_TIME_SIZE];
1475
uint time_buff_len= 0;
1477
(void) pthread_mutex_lock(&LOCK_log);
1479
/* Test if someone closed between the is_open test and lock */
1482
/* Note that my_b_write() assumes it knows the length for this */
1483
if (event_time != last_time)
1485
last_time= event_time;
1487
localtime_r(&event_time, &start);
1489
time_buff_len= snprintf(local_time_buff, MAX_TIME_SIZE,
1490
"%02d%02d%02d %2d:%02d:%02d",
1491
start.tm_year % 100, start.tm_mon + 1,
1492
start.tm_mday, start.tm_hour,
1493
start.tm_min, start.tm_sec);
1495
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1499
if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1502
/* command_type, thread_id */
1503
length= snprintf(buff, 32, "%5ld ", (long) thread_id);
1505
if (my_b_write(&log_file, (uchar*) buff, length))
1508
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1511
if (my_b_write(&log_file, (uchar*) "\t", 1))
1515
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1518
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1519
flush_io_cache(&log_file))
1523
(void) pthread_mutex_unlock(&LOCK_log);
1530
sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1532
(void) pthread_mutex_unlock(&LOCK_log);
1538
Log a query to the traditional slow log file
1543
thd THD of the query
1544
current_time current timestamp
1545
query_start_arg command start timestamp
1546
user_host the pointer to the string with user@host info
1547
user_host_len length of the user_host string. this is computed once
1548
and passed to all general log event handlers
1549
query_utime Amount of time the query took to execute (in microseconds)
1550
lock_utime Amount of time the query was locked (in microseconds)
1551
is_command The flag, which determines, whether the sql_text is a
1552
query or an administrator command.
1553
sql_text the very text of the query or administrator command
1555
sql_text_len the length of sql_text string
1559
Log a query to the slow log file.
1563
TRUE - error occured
1566
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
1567
time_t query_start_arg __attribute__((__unused__)),
1568
const char *user_host,
1569
uint user_host_len, ulonglong query_utime,
1570
ulonglong lock_utime, bool is_command,
1571
const char *sql_text, uint sql_text_len)
1575
(void) pthread_mutex_lock(&LOCK_log);
1579
(void) pthread_mutex_unlock(&LOCK_log);
1584
{ // Safety agains reopen
1586
char buff[80], *end;
1587
char query_time_buff[22+7], lock_time_buff[22+7];
1591
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1593
if (current_time != last_time)
1595
last_time= current_time;
1597
localtime_r(¤t_time, &start);
1599
buff_len= snprintf(buff, sizeof buff,
1600
"# Time: %02d%02d%02d %2d:%02d:%02d\n",
1601
start.tm_year % 100, start.tm_mon + 1,
1602
start.tm_mday, start.tm_hour,
1603
start.tm_min, start.tm_sec);
1605
/* Note that my_b_write() assumes it knows the length for this */
1606
if (my_b_write(&log_file, (uchar*) buff, buff_len))
1609
const uchar uh[]= "# User@Host: ";
1610
if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1612
if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1614
if (my_b_write(&log_file, (uchar*) "\n", 1))
1617
/* For slow query log */
1618
sprintf(query_time_buff, "%.6f", ulonglong2double(query_utime)/1000000.0);
1619
sprintf(lock_time_buff, "%.6f", ulonglong2double(lock_utime)/1000000.0);
1620
if (my_b_printf(&log_file,
1621
"# Query_time: %s Lock_time: %s"
1622
" Rows_sent: %lu Rows_examined: %lu\n",
1623
query_time_buff, lock_time_buff,
1624
(ulong) thd->sent_row_count,
1625
(ulong) thd->examined_row_count) == (uint) -1)
1627
if (thd->db && strcmp(thd->db, db))
1628
{ // Database changed
1629
if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1633
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1635
end=strmov(end, ",last_insert_id=");
1636
end=longlong10_to_str((longlong)
1637
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1640
// Save value if we do an insert.
1641
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1643
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1645
end=strmov(end,",insert_id=");
1646
end=longlong10_to_str((longlong)
1647
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1653
This info used to show up randomly, depending on whether the query
1654
checked the query start time or not. now we always write current
1655
timestamp to the slow log
1657
end= strmov(end, ",timestamp=");
1658
end= int10_to_str((long) current_time, end, 10);
1664
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1665
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1670
end= strxmov(buff, "# administrator command: ", NullS);
1671
buff_len= (ulong) (end - buff);
1672
my_b_write(&log_file, (uchar*) buff, buff_len);
1674
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1675
my_b_write(&log_file, (uchar*) ";\n",2) ||
1676
flush_io_cache(&log_file))
1684
sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1688
(void) pthread_mutex_unlock(&LOCK_log);
1028
1695
The following should be using fn_format(); We just need to
1029
1696
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,
1698
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1699
const char *suffix,
1033
1700
bool strip_ext, char *buff)
2656
3329
return logger.error_log_print(level, format, args);
2659
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
3333
bool slow_log_print(THD *thd, const char *query, uint query_length,
3334
ulonglong current_utime)
3336
return logger.slow_log_print(thd, query, query_length, current_utime);
3340
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
3343
Log command if we have at least one log event handler enabled and want
3344
to log this king of commands
3346
if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
3348
if (thd->options & OPTION_LOG_OFF)
3361
bool general_log_print(THD *thd, enum enum_server_command command,
3362
const char *format, ...)
3367
/* Print the message to the buffer if we want to log this king of commands */
3368
if (! logger.log_command(thd, command))
3371
va_start(args, format);
3372
error= logger.general_log_print(thd, command, format, args);
3378
bool general_log_write(THD *thd, enum enum_server_command command,
3379
const char *query, uint query_length)
3381
/* Write the message to the log if we want to log this king of commands */
3382
if (logger.log_command(thd, command))
3383
return logger.general_log_write(thd, command, query, query_length);
3388
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2661
3390
if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3391
pthread_mutex_lock(&LOCK_log);