206
243
/* 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)))
244
int check_if_log_table(uint db_len, const char *db, uint table_name_len,
245
const char *table_name, uint check_if_opened)
250
/* log event handlers */
252
bool Log_to_file_event_handler::
253
log_error(enum loglevel level, const char *format,
256
return vprint_msg_to_log(level, format, args);
259
void Log_to_file_event_handler::init_pthread_objects()
261
mysql_log.init_pthread_objects();
262
mysql_slow_log.init_pthread_objects();
266
/** Wrapper around MYSQL_LOG::write() for slow log. */
268
bool Log_to_file_event_handler::
269
log_slow(THD *thd, time_t current_time, time_t query_start_arg,
270
const char *user_host, uint user_host_len,
271
ulonglong query_utime, ulonglong lock_utime, bool is_command,
272
const char *sql_text, uint sql_text_len)
274
return mysql_slow_log.write(thd, current_time, query_start_arg,
275
user_host, user_host_len,
276
query_utime, lock_utime, is_command,
277
sql_text, sql_text_len);
282
Wrapper around MYSQL_LOG::write() for general log. We need it since we
283
want all log event handlers to have the same signature.
286
bool Log_to_file_event_handler::
287
log_general(THD *thd, 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)
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= my_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, uint log_type)
616
MYSQL_QUERY_LOG *file_log;
623
file_log= file_log_handler->get_mysql_slow_log();
625
file_log->open_slow_log(sys_var_slow_log_path.value);
626
init_slow_log(log_output_options);
630
case QUERY_LOG_GENERAL:
633
file_log= file_log_handler->get_mysql_log();
635
file_log->open_query_log(sys_var_general_log_path.value);
636
init_general_log(log_output_options);
648
void LOGGER::deactivate_log_handler(THD *thd, uint log_type)
655
tmp_opt= &opt_slow_log;
656
file_log= file_log_handler->get_mysql_slow_log();
658
case QUERY_LOG_GENERAL:
660
file_log= file_log_handler->get_mysql_log();
663
assert(0); // Impossible
675
int LOGGER::set_handlers(uint error_log_printer,
676
uint slow_log_printer,
677
uint general_log_printer)
302
679
/* error log table is not supported yet */
680
DBUG_ASSERT(error_log_printer < LOG_TABLE);
303
682
lock_exclusive();
305
684
init_error_log(error_log_printer);
685
init_slow_log(slow_log_printer);
686
init_general_log(general_log_printer);
895
1303
if (log_type == LOG_NORMAL)
898
int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
899
"started with:\nTCP Port: %d, Named Pipe: %s\n",
900
my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
903
end= my_stpncpy(buff + len, "Time Id Command Argument\n",
1306
int len=my_snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
1307
"started with:\nTCP Port: %d, Named Pipe: %s\n",
1308
my_progname, server_version, MYSQL_COMPILATION_COMMENT,
1311
end= strnmov(buff + len, "Time Id Command Argument\n",
904
1312
sizeof(buff) - len);
905
if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
1313
if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
906
1314
flush_io_cache(&log_file))
910
1318
log_state= LOG_OPENED;
914
sql_print_error(_("Could not use %s for logging (error %d). "
915
"Turning logging off for the whole duration of the "
916
"Drizzle server process. "
917
"To turn it on again: fix the cause, "
918
"shutdown the Drizzle server and restart it."),
1322
sql_print_error("Could not use %s for logging (error %d). \
1323
Turning logging off for the whole duration of the MySQL server process. \
1324
To turn it on again: fix the cause, \
1325
shutdown the MySQL server and restart it.", name, errno);
921
1327
my_close(file, MYF(0));
922
1328
end_io_cache(&log_file);
928
1330
log_state= LOG_CLOSED;
932
DRIZZLE_LOG::DRIZZLE_LOG()
933
: name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
1334
MYSQL_LOG::MYSQL_LOG()
1335
: name(0), write_error(FALSE), inited(FALSE), log_type(LOG_UNKNOWN),
934
1336
log_state(LOG_CLOSED)
1434
Reopen the log file. The method is used during FLUSH LOGS
1435
and locks LOCK_log mutex
1439
void MYSQL_QUERY_LOG::reopen_file()
1443
DBUG_ENTER("MYSQL_LOG::reopen_file");
1446
DBUG_PRINT("info",("log is closed"));
1450
pthread_mutex_lock(&LOCK_log);
1453
name= 0; // Don't free name
1454
close(LOG_CLOSE_TO_BE_OPENED);
1457
Note that at this point, log_state != LOG_CLOSED (important for is_open()).
1460
open(save_name, log_type, 0, io_cache_type);
1461
my_free(save_name, MYF(0));
1463
pthread_mutex_unlock(&LOCK_log);
1470
Write a command to traditional general log file
1475
event_time command start timestamp
1476
user_host the pointer to the string with user@host info
1477
user_host_len length of the user_host string. this is computed once
1478
and passed to all general log event handlers
1479
thread_id Id of the thread, issued a query
1480
command_type the type of the command being logged
1481
command_type_len the length of the string above
1482
sql_text the very text of the query being executed
1483
sql_text_len the length of sql_text string
1487
Log given command to to normal (not rotable) log file
1491
TRUE - error occured
1494
bool MYSQL_QUERY_LOG::write(time_t event_time, const char *user_host,
1495
uint user_host_len, int thread_id,
1496
const char *command_type, uint command_type_len,
1497
const char *sql_text, uint sql_text_len)
1501
char local_time_buff[MAX_TIME_SIZE];
1503
uint time_buff_len= 0;
1505
(void) pthread_mutex_lock(&LOCK_log);
1507
/* Test if someone closed between the is_open test and lock */
1510
/* Note that my_b_write() assumes it knows the length for this */
1511
if (event_time != last_time)
1513
last_time= event_time;
1515
localtime_r(&event_time, &start);
1517
time_buff_len= my_snprintf(local_time_buff, MAX_TIME_SIZE,
1518
"%02d%02d%02d %2d:%02d:%02d",
1519
start.tm_year % 100, start.tm_mon + 1,
1520
start.tm_mday, start.tm_hour,
1521
start.tm_min, start.tm_sec);
1523
if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
1527
if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
1530
/* command_type, thread_id */
1531
length= my_snprintf(buff, 32, "%5ld ", (long) thread_id);
1533
if (my_b_write(&log_file, (uchar*) buff, length))
1536
if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
1539
if (my_b_write(&log_file, (uchar*) "\t", 1))
1543
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
1546
if (my_b_write(&log_file, (uchar*) "\n", 1) ||
1547
flush_io_cache(&log_file))
1551
(void) pthread_mutex_unlock(&LOCK_log);
1558
sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
1560
(void) pthread_mutex_unlock(&LOCK_log);
1566
Log a query to the traditional slow log file
1571
thd THD of the query
1572
current_time current timestamp
1573
query_start_arg command start timestamp
1574
user_host the pointer to the string with user@host info
1575
user_host_len length of the user_host string. this is computed once
1576
and passed to all general log event handlers
1577
query_utime Amount of time the query took to execute (in microseconds)
1578
lock_utime Amount of time the query was locked (in microseconds)
1579
is_command The flag, which determines, whether the sql_text is a
1580
query or an administrator command.
1581
sql_text the very text of the query or administrator command
1583
sql_text_len the length of sql_text string
1587
Log a query to the slow log file.
1591
TRUE - error occured
1594
bool MYSQL_QUERY_LOG::write(THD *thd, time_t current_time,
1595
time_t query_start_arg, const char *user_host,
1596
uint user_host_len, ulonglong query_utime,
1597
ulonglong lock_utime, bool is_command,
1598
const char *sql_text, uint sql_text_len)
1601
DBUG_ENTER("MYSQL_QUERY_LOG::write");
1603
(void) pthread_mutex_lock(&LOCK_log);
1607
(void) pthread_mutex_unlock(&LOCK_log);
1612
{ // Safety agains reopen
1614
char buff[80], *end;
1615
char query_time_buff[22+7], lock_time_buff[22+7];
1619
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1621
if (current_time != last_time)
1623
last_time= current_time;
1625
localtime_r(¤t_time, &start);
1627
buff_len= my_snprintf(buff, sizeof buff,
1628
"# Time: %02d%02d%02d %2d:%02d:%02d\n",
1629
start.tm_year % 100, start.tm_mon + 1,
1630
start.tm_mday, start.tm_hour,
1631
start.tm_min, start.tm_sec);
1633
/* Note that my_b_write() assumes it knows the length for this */
1634
if (my_b_write(&log_file, (uchar*) buff, buff_len))
1637
const uchar uh[]= "# User@Host: ";
1638
if (my_b_write(&log_file, uh, sizeof(uh) - 1))
1640
if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
1642
if (my_b_write(&log_file, (uchar*) "\n", 1))
1645
/* For slow query log */
1646
sprintf(query_time_buff, "%.6f", ulonglong2double(query_utime)/1000000.0);
1647
sprintf(lock_time_buff, "%.6f", ulonglong2double(lock_utime)/1000000.0);
1648
if (my_b_printf(&log_file,
1649
"# Query_time: %s Lock_time: %s"
1650
" Rows_sent: %lu Rows_examined: %lu\n",
1651
query_time_buff, lock_time_buff,
1652
(ulong) thd->sent_row_count,
1653
(ulong) thd->examined_row_count) == (uint) -1)
1655
if (thd->db && strcmp(thd->db, db))
1656
{ // Database changed
1657
if (my_b_printf(&log_file,"use %s;\n",thd->db) == (uint) -1)
1661
if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
1663
end=strmov(end, ",last_insert_id=");
1664
end=longlong10_to_str((longlong)
1665
thd->first_successful_insert_id_in_prev_stmt_for_binlog,
1668
// Save value if we do an insert.
1669
if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
1671
if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
1673
end=strmov(end,",insert_id=");
1674
end=longlong10_to_str((longlong)
1675
thd->auto_inc_intervals_in_cur_stmt_for_binlog.minimum(),
1681
This info used to show up randomly, depending on whether the query
1682
checked the query start time or not. now we always write current
1683
timestamp to the slow log
1685
end= strmov(end, ",timestamp=");
1686
end= int10_to_str((long) current_time, end, 10);
1692
if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
1693
my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
1698
end= strxmov(buff, "# administrator command: ", NullS);
1699
buff_len= (ulong) (end - buff);
1700
my_b_write(&log_file, (uchar*) buff, buff_len);
1702
if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len) ||
1703
my_b_write(&log_file, (uchar*) ";\n",2) ||
1704
flush_io_cache(&log_file))
1712
sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
1716
(void) pthread_mutex_unlock(&LOCK_log);
1028
1723
The following should be using fn_format(); We just need to
1029
1724
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,
1726
const char *MYSQL_LOG::generate_name(const char *log_name,
1032
1727
const char *suffix,
1033
1728
bool strip_ext, char *buff)