~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log.cc

  • Committer: Mats Kindahl
  • Date: 2008-08-07 06:24:22 UTC
  • mfrom: (265 drizzle)
  • mto: (264.1.19 codestyle)
  • mto: This revision was merged to the branch mainline in revision 266.
  • Revision ID: mats@mysql.com-20080807062422-20kyv6ssp4grfm0s
Manual merge of lp:drizzle into ~mkindahl/remove-mem-casts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    Abort logging when we get an error in reading or writing log files
25
25
*/
26
26
 
27
 
#include <drizzled/server_includes.h>
 
27
#include "mysql_priv.h"
28
28
#include "sql_repl.h"
29
29
#include "rpl_filter.h"
30
30
#include "rpl_rli.h"
34
34
 
35
35
#include <drizzled/plugin.h>
36
36
#include <drizzled/drizzled_error_messages.h>
37
 
#include <libdrizzle/gettext.h>
38
37
 
39
38
/* max size of the log message */
40
39
#define MAX_LOG_BUFFER_SIZE 1024
46
45
 
47
46
LOGGER logger;
48
47
 
49
 
DRIZZLE_BIN_LOG mysql_bin_log;
 
48
MYSQL_BIN_LOG mysql_bin_log;
50
49
ulong sync_binlog_counter= 0;
51
50
 
52
51
static bool test_if_number(const char *str,
204
203
 
205
204
 
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)))
212
211
{
213
212
  return 0;
214
213
}
215
214
 
 
215
/* log event handlers */
 
216
 
 
217
bool Log_to_file_event_handler::
 
218
  log_error(enum loglevel level, const char *format,
 
219
            va_list args)
 
220
{
 
221
  return vprint_msg_to_log(level, format, args);
 
222
}
 
223
 
 
224
void Log_to_file_event_handler::init_pthread_objects()
 
225
{
 
226
  mysql_log.init_pthread_objects();
 
227
  mysql_slow_log.init_pthread_objects();
 
228
}
 
229
 
 
230
 
 
231
/** Wrapper around MYSQL_LOG::write() for slow log. */
 
232
 
 
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)
 
238
{
 
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);
 
243
}
 
244
 
 
245
 
 
246
/**
 
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.
 
249
*/
 
250
 
 
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)))
 
258
{
 
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);
 
262
}
 
263
 
 
264
 
 
265
bool Log_to_file_event_handler::init()
 
266
{
 
267
  if (!is_initialized)
 
268
  {
 
269
    if (opt_slow_log)
 
270
      mysql_slow_log.open_slow_log(sys_var_slow_log_path.value);
 
271
 
 
272
    if (opt_log)
 
273
      mysql_log.open_query_log(sys_var_general_log_path.value);
 
274
 
 
275
    is_initialized= true;
 
276
  }
 
277
 
 
278
  return false;
 
279
}
 
280
 
 
281
 
 
282
void Log_to_file_event_handler::cleanup()
 
283
{
 
284
  mysql_log.cleanup();
 
285
  mysql_slow_log.cleanup();
 
286
}
 
287
 
 
288
void Log_to_file_event_handler::flush()
 
289
{
 
290
  /* reopen log files */
 
291
  if (opt_log)
 
292
    mysql_log.reopen_file();
 
293
  if (opt_slow_log)
 
294
    mysql_slow_log.reopen_file();
 
295
}
 
296
 
216
297
/*
217
298
  Log error with all enabled log event handlers
218
299
 
247
328
{
248
329
  assert(inited == 1);
249
330
  rwlock_destroy(&LOCK_logger);
 
331
  if (file_log_handler)
 
332
    file_log_handler->cleanup();
250
333
}
251
334
 
252
335
 
253
336
void LOGGER::cleanup_end()
254
337
{
255
338
  assert(inited == 1);
 
339
  if (file_log_handler)
 
340
    delete file_log_handler;
256
341
}
257
342
 
258
343
 
265
350
  assert(inited == 0);
266
351
  inited= 1;
267
352
 
 
353
  /*
 
354
    Here we create file log handler. We don't do it for the table log handler
 
355
    here as it cannot be created so early. The reason is THD initialization,
 
356
    which depends on the system variables (parsed later).
 
357
  */
 
358
  if (!file_log_handler)
 
359
    file_log_handler= new Log_to_file_event_handler;
 
360
 
268
361
  /* by default we use traditional error log */
269
362
  init_error_log(LOG_FILE);
270
363
 
 
364
  file_log_handler->init_pthread_objects();
271
365
  my_rwlock_init(&LOCK_logger, NULL);
272
366
}
273
367
 
282
376
  */
283
377
  logger.lock_exclusive();
284
378
 
 
379
  /* reopen log files */
 
380
  file_log_handler->flush();
 
381
 
285
382
  /* end of log flush */
286
383
  logger.unlock();
287
384
  return rc;
288
385
}
289
386
 
290
 
void LOGGER::init_error_log(uint32_t error_log_printer)
 
387
 
 
388
/*
 
389
  Log slow query with all enabled log event handlers
 
390
 
 
391
  SYNOPSIS
 
392
    slow_log_print()
 
393
 
 
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)
 
398
 
 
399
  RETURN
 
400
    FALSE   OK
 
401
    TRUE    error occured
 
402
*/
 
403
 
 
404
bool LOGGER::slow_log_print(THD *thd, const char *query, uint query_length,
 
405
                            uint64_t current_utime)
 
406
 
 
407
{
 
408
  bool error= false;
 
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;
 
415
 
 
416
  /*
 
417
    Print the message to the buffer if we have slow log enabled
 
418
  */
 
419
 
 
420
  if (*slow_log_handler_list)
 
421
  {
 
422
    time_t current_time;
 
423
 
 
424
    /* do not log slow queries from replication threads */
 
425
    if (thd->slave_thread && !opt_log_slow_slave_statements)
 
426
      return 0;
 
427
 
 
428
    lock_shared();
 
429
    if (!opt_slow_log)
 
430
    {
 
431
      unlock();
 
432
      return 0;
 
433
    }
 
434
 
 
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, "] @ ",
 
438
                             sctx->ip, " [",
 
439
                             sctx->ip, "]", NullS) -
 
440
                    user_host_buff);
 
441
 
 
442
    current_time= my_time_possible_from_micro(current_utime);
 
443
    if (thd->start_utime)
 
444
    {
 
445
      query_utime= (current_utime - thd->start_utime);
 
446
      lock_utime=  (thd->utime_after_lock - thd->start_utime);
 
447
    }
 
448
    else
 
449
    {
 
450
      query_utime= lock_utime= 0;
 
451
    }
 
452
 
 
453
    if (!query)
 
454
    {
 
455
      is_command= true;
 
456
      query= command_name[thd->command].str;
 
457
      query_length= command_name[thd->command].length;
 
458
    }
 
459
 
 
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;
 
465
 
 
466
    unlock();
 
467
  }
 
468
  return error;
 
469
}
 
470
 
 
471
bool LOGGER::general_log_write(THD *thd, enum enum_server_command command,
 
472
                               const char *query, uint query_length)
 
473
{
 
474
  bool error= false;
 
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;
 
478
  ulong id;
 
479
  uint user_host_len= 0;
 
480
  time_t current_time;
 
481
 
 
482
  if (thd)
 
483
    id= thd->thread_id;                 /* Normal thread */
 
484
  else
 
485
    id= 0;                              /* Log from connect handler */
 
486
 
 
487
  lock_shared();
 
488
  if (!opt_log)
 
489
  {
 
490
    unlock();
 
491
    return 0;
 
492
  }
 
493
  user_host_len= strxnmov(user_host_buff, MAX_USER_HOST_SIZE,
 
494
                          sctx->user, "[",
 
495
                          sctx->user, "] @ ",
 
496
                          sctx->ip, " [",
 
497
                          sctx->ip, "]", NullS) -
 
498
                                                          user_host_buff;
 
499
 
 
500
  current_time= my_time(0);
 
501
 
 
502
  while (*current_handler)
 
503
    error|= (*current_handler++)->
 
504
      log_general(thd, current_time, user_host_buff,
 
505
                  user_host_len, id,
 
506
                  command_name[(uint) command].str,
 
507
                  command_name[(uint) command].length,
 
508
                  query, query_length,
 
509
                  thd->variables.character_set_client) || error;
 
510
  unlock();
 
511
 
 
512
  return error;
 
513
}
 
514
 
 
515
bool LOGGER::general_log_print(THD *thd, enum enum_server_command command,
 
516
                               const char *format, va_list args)
 
517
{
 
518
  uint message_buff_len= 0;
 
519
  char message_buff[MAX_LOG_BUFFER_SIZE];
 
520
 
 
521
  /* prepare message */
 
522
  if (format)
 
523
    message_buff_len= vsnprintf(message_buff, sizeof(message_buff),
 
524
                                   format, args);
 
525
  else
 
526
    message_buff[0]= '\0';
 
527
 
 
528
  return general_log_write(thd, command, message_buff, message_buff_len);
 
529
}
 
530
 
 
531
void LOGGER::init_error_log(uint error_log_printer)
291
532
{
292
533
  if (error_log_printer & LOG_NONE)
293
534
  {
295
536
    return;
296
537
  }
297
538
 
298
 
}
299
 
 
300
 
int LOGGER::set_handlers(uint32_t error_log_printer)
 
539
  switch (error_log_printer) {
 
540
  case LOG_FILE:
 
541
    error_log_handler_list[0]= file_log_handler;
 
542
    error_log_handler_list[1]= 0;
 
543
    break;
 
544
  }
 
545
}
 
546
 
 
547
void LOGGER::init_slow_log(uint slow_log_printer)
 
548
{
 
549
  if (slow_log_printer & LOG_NONE)
 
550
  {
 
551
    slow_log_handler_list[0]= 0;
 
552
    return;
 
553
  }
 
554
 
 
555
  slow_log_handler_list[0]= file_log_handler;
 
556
  slow_log_handler_list[1]= 0;
 
557
}
 
558
 
 
559
void LOGGER::init_general_log(uint general_log_printer)
 
560
{
 
561
  if (general_log_printer & LOG_NONE)
 
562
  {
 
563
    general_log_handler_list[0]= 0;
 
564
    return;
 
565
  }
 
566
 
 
567
  general_log_handler_list[0]= file_log_handler;
 
568
  general_log_handler_list[1]= 0;
 
569
}
 
570
 
 
571
 
 
572
bool LOGGER::activate_log_handler(THD* thd __attribute__((unused)),
 
573
                                  uint log_type)
 
574
{
 
575
  MYSQL_QUERY_LOG *file_log;
 
576
  bool res= false;
 
577
  lock_exclusive();
 
578
  switch (log_type) {
 
579
  case QUERY_LOG_SLOW:
 
580
    if (!opt_slow_log)
 
581
    {
 
582
      file_log= file_log_handler->get_mysql_slow_log();
 
583
 
 
584
      file_log->open_slow_log(sys_var_slow_log_path.value);
 
585
      init_slow_log(log_output_options);
 
586
      opt_slow_log= true;
 
587
    }
 
588
    break;
 
589
  case QUERY_LOG_GENERAL:
 
590
    if (!opt_log)
 
591
    {
 
592
      file_log= file_log_handler->get_mysql_log();
 
593
 
 
594
      file_log->open_query_log(sys_var_general_log_path.value);
 
595
      init_general_log(log_output_options);
 
596
      opt_log= true;
 
597
    }
 
598
    break;
 
599
  default:
 
600
    assert(0);
 
601
  }
 
602
  unlock();
 
603
  return res;
 
604
}
 
605
 
 
606
 
 
607
void LOGGER::deactivate_log_handler(THD *thd __attribute__((unused)),
 
608
                                    uint log_type)
 
609
{
 
610
  bool *tmp_opt= 0;
 
611
  MYSQL_LOG *file_log;
 
612
 
 
613
  switch (log_type) {
 
614
  case QUERY_LOG_SLOW:
 
615
    tmp_opt= &opt_slow_log;
 
616
    file_log= file_log_handler->get_mysql_slow_log();
 
617
    break;
 
618
  case QUERY_LOG_GENERAL:
 
619
    tmp_opt= &opt_log;
 
620
    file_log= file_log_handler->get_mysql_log();
 
621
    break;
 
622
  default:
 
623
    assert(0);                                  // Impossible
 
624
  }
 
625
 
 
626
  if (!(*tmp_opt))
 
627
    return;
 
628
 
 
629
  lock_exclusive();
 
630
  file_log->close(0);
 
631
  *tmp_opt= false;
 
632
  unlock();
 
633
}
 
634
 
 
635
int LOGGER::set_handlers(uint error_log_printer,
 
636
                         uint slow_log_printer,
 
637
                         uint general_log_printer)
301
638
{
302
639
  /* error log table is not supported yet */
303
640
  lock_exclusive();
304
641
 
305
642
  init_error_log(error_log_printer);
 
643
  init_slow_log(slow_log_printer);
 
644
  init_general_log(general_log_printer);
 
645
 
306
646
  unlock();
307
647
 
308
648
  return 0;
369
709
 
370
710
/*
371
711
  this function is mostly a placeholder.
372
 
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
 
712
  conceptually, binlog initialization (now mostly done in MYSQL_BIN_LOG::open)
373
713
  should be moved here.
374
714
*/
375
715
 
377
717
{
378
718
  binlog_hton= (handlerton *)p;
379
719
  binlog_hton->state=opt_bin_log ? SHOW_OPTION_YES : SHOW_OPTION_NO;
 
720
  binlog_hton->db_type=DB_TYPE_BINLOG;
380
721
  binlog_hton->savepoint_offset= sizeof(my_off_t);
381
722
  binlog_hton->close_connection= binlog_close_connection;
382
723
  binlog_hton->savepoint_set= binlog_savepoint_set;
397
738
  assert(trx_data->empty());
398
739
  thd_set_ha_data(thd, binlog_hton, NULL);
399
740
  trx_data->~binlog_trx_data();
400
 
  free((unsigned char*)trx_data);
 
741
  my_free((uchar*)trx_data, MYF(0));
401
742
  return 0;
402
743
}
403
744
 
504
845
    do nothing.
505
846
    just pretend we can do 2pc, so that MySQL won't
506
847
    switch to 1pc.
507
 
    real work will be done in DRIZZLE_BIN_LOG::log_xid()
 
848
    real work will be done in MYSQL_BIN_LOG::log_xid()
508
849
  */
509
850
  return 0;
510
851
}
532
873
 
533
874
  if (trx_data->empty())
534
875
  {
535
 
    // we're here because trans_log was flushed in DRIZZLE_BIN_LOG::log_xid()
 
876
    // we're here because trans_log was flushed in MYSQL_BIN_LOG::log_xid()
536
877
    trx_data->reset();
537
878
    return(0);
538
879
  }
600
941
  if ((in_transaction && (all || (!trx_data->at_least_one_stmt && thd->transaction.stmt.modified_non_trans_table))) || (!in_transaction && !all))
601
942
  {
602
943
    Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), true, false);
603
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
944
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
604
945
    int error= binlog_end_trans(thd, trx_data, &qev, all);
605
946
    return(error);
606
947
  }
647
988
      rolled back on the slave.
648
989
    */
649
990
    Query_log_event qev(thd, STRING_WITH_LEN("ROLLBACK"), true, false);
650
 
    qev.error_code= 0; // see comment in DRIZZLE_LOG::write(THD, IO_CACHE)
 
991
    qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
651
992
    error= binlog_end_trans(thd, trx_data, &qev, all);
652
993
  }
653
994
  else if ((all && !thd->transaction.all.modified_non_trans_table) ||
725
1066
  char magic[4];
726
1067
  assert(my_b_tell(log) == 0);
727
1068
 
728
 
  if (my_b_read(log, (unsigned char*) magic, sizeof(magic)))
 
1069
  if (my_b_read(log, (uchar*) magic, sizeof(magic)))
729
1070
  {
730
 
    *errmsg = _("I/O error reading the header from the binary log");
 
1071
    *errmsg = "I/O error reading the header from the binary log";
731
1072
    sql_print_error("%s, errno=%d, io cache code=%d", *errmsg, my_errno,
732
1073
                    log->error);
733
1074
    return 1;
734
1075
  }
735
1076
  if (memcmp(magic, BINLOG_MAGIC, sizeof(magic)))
736
1077
  {
737
 
    *errmsg = _("Binlog has bad magic number;  It's not a binary log file "
738
 
                "that can be used by this version of Drizzle");
 
1078
    *errmsg = "Binlog has bad magic number;  It's not a binary log file that can be used by this version of MySQL";
739
1079
    return 1;
740
1080
  }
741
1081
  return 0;
746
1086
{
747
1087
  File file;
748
1088
 
749
 
  if ((file = my_open(log_file_name, O_RDONLY, 
 
1089
  if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE, 
750
1090
                      MYF(MY_WME))) < 0)
751
1091
  {
752
 
    sql_print_error(_("Failed to open log (file '%s', errno %d)"),
 
1092
    sql_print_error("Failed to open log (file '%s', errno %d)",
753
1093
                    log_file_name, my_errno);
754
 
    *errmsg = _("Could not open log file");
 
1094
    *errmsg = "Could not open log file";
755
1095
    goto err;
756
1096
  }
757
1097
  if (init_io_cache(log, file, IO_SIZE*2, READ_CACHE, 0, 0,
758
1098
                    MYF(MY_WME|MY_DONT_CHECK_FILESIZE)))
759
1099
  {
760
 
    sql_print_error(_("Failed to create a cache on log (file '%s')"),
 
1100
    sql_print_error("Failed to create a cache on log (file '%s')",
761
1101
                    log_file_name);
762
 
    *errmsg = _("Could not open log file");
 
1102
    *errmsg = "Could not open log file";
763
1103
    goto err;
764
1104
  }
765
1105
  if (check_binlog_magic(log,errmsg))
788
1128
static int find_uniq_filename(char *name)
789
1129
{
790
1130
  long                  number;
791
 
  uint32_t                  i;
 
1131
  uint                  i;
792
1132
  char                  buff[FN_REFLEN];
793
1133
  struct st_my_dir     *dir_info;
794
1134
  register struct fileinfo *file_info;
798
1138
 
799
1139
  length= dirname_part(buff, name, &buf_length);
800
1140
  start=  name + length;
801
 
  end= strchr(start, '\0');
 
1141
  end=    strend(start);
802
1142
 
803
1143
  *end='.';
804
1144
  length= (size_t) (end-start+1);
805
1145
 
806
1146
  if (!(dir_info = my_dir(buff,MYF(MY_DONT_SORT))))
807
1147
  {                                             // This shouldn't happen
808
 
    my_stpcpy(end,".1");                                // use name+1
 
1148
    strmov(end,".1");                           // use name+1
809
1149
    return(0);
810
1150
  }
811
1151
  file_info= dir_info->dir_entry;
825
1165
}
826
1166
 
827
1167
 
828
 
void DRIZZLE_LOG::init(enum_log_type log_type_arg,
 
1168
void MYSQL_LOG::init(enum_log_type log_type_arg,
829
1169
                     enum cache_type io_cache_type_arg)
830
1170
{
831
1171
  log_type= log_type_arg;
855
1195
    1   error
856
1196
*/
857
1197
 
858
 
bool DRIZZLE_LOG::open(const char *log_name, enum_log_type log_type_arg,
 
1198
bool MYSQL_LOG::open(const char *log_name, enum_log_type log_type_arg,
859
1199
                     const char *new_name, enum cache_type io_cache_type_arg)
860
1200
{
861
1201
  char buff[FN_REFLEN];
862
1202
  File file= -1;
863
 
  int open_flags= O_CREAT;
 
1203
  int open_flags= O_CREAT | O_BINARY;
864
1204
 
865
1205
  write_error= 0;
866
1206
 
873
1213
  }
874
1214
 
875
1215
  if (new_name)
876
 
    my_stpcpy(log_file_name, new_name);
 
1216
    strmov(log_file_name, new_name);
877
1217
  else if (generate_new_name(log_file_name, name))
878
1218
    goto err;
879
1219
 
897
1237
    char *end;
898
1238
    int len=snprintf(buff, sizeof(buff), "%s, Version: %s (%s). "
899
1239
                     "started with:\nTCP Port: %d, Named Pipe: %s\n",
900
 
                     my_progname, server_version, DRIZZLE_COMPILATION_COMMENT,
 
1240
                     my_progname, server_version, MYSQL_COMPILATION_COMMENT,
901
1241
                     mysqld_port, ""
902
1242
                     );
903
 
    end= my_stpncpy(buff + len, "Time                 Id Command    Argument\n",
 
1243
    end= strnmov(buff + len, "Time                 Id Command    Argument\n",
904
1244
                 sizeof(buff) - len);
905
 
    if (my_b_write(&log_file, (unsigned char*) buff, (uint) (end-buff)) ||
 
1245
    if (my_b_write(&log_file, (uchar*) buff, (uint) (end-buff)) ||
906
1246
        flush_io_cache(&log_file))
907
1247
      goto err;
908
1248
  }
911
1251
  return(0);
912
1252
 
913
1253
err:
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."),
919
 
                    name, errno);
 
1254
  sql_print_error("Could not use %s for logging (error %d). \
 
1255
Turning logging off for the whole duration of the MySQL server process. \
 
1256
To turn it on again: fix the cause, \
 
1257
shutdown the MySQL server and restart it.", name, errno);
920
1258
  if (file >= 0)
921
1259
    my_close(file, MYF(0));
922
1260
  end_io_cache(&log_file);
923
 
  if (name)
924
 
  {
925
 
    free(name);
926
 
    name= NULL;
927
 
  }
 
1261
  safeFree(name);
928
1262
  log_state= LOG_CLOSED;
929
1263
  return(1);
930
1264
}
931
1265
 
932
 
DRIZZLE_LOG::DRIZZLE_LOG()
 
1266
MYSQL_LOG::MYSQL_LOG()
933
1267
  : name(0), write_error(false), inited(false), log_type(LOG_UNKNOWN),
934
1268
    log_state(LOG_CLOSED)
935
1269
{
942
1276
  memset(&log_file, 0, sizeof(log_file));
943
1277
}
944
1278
 
945
 
void DRIZZLE_LOG::init_pthread_objects()
 
1279
void MYSQL_LOG::init_pthread_objects()
946
1280
{
947
1281
  assert(inited == 0);
948
1282
  inited= 1;
963
1297
    The internal structures are not freed until cleanup() is called
964
1298
*/
965
1299
 
966
 
void DRIZZLE_LOG::close(uint32_t exiting)
 
1300
void MYSQL_LOG::close(uint exiting)
967
1301
{                                       // One can't set log_type here!
968
1302
  if (log_state == LOG_OPENED)
969
1303
  {
983
1317
  }
984
1318
 
985
1319
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
986
 
  if (name)
987
 
  {
988
 
    free(name);
989
 
    name= NULL;
990
 
  }
 
1320
  safeFree(name);
991
1321
  return;
992
1322
}
993
1323
 
994
1324
/** This is called only once. */
995
1325
 
996
 
void DRIZZLE_LOG::cleanup()
 
1326
void MYSQL_LOG::cleanup()
997
1327
{
998
1328
  if (inited)
999
1329
  {
1005
1335
}
1006
1336
 
1007
1337
 
1008
 
int DRIZZLE_LOG::generate_new_name(char *new_name, const char *log_name)
 
1338
int MYSQL_LOG::generate_new_name(char *new_name, const char *log_name)
1009
1339
{
1010
1340
  fn_format(new_name, log_name, mysql_data_home, "", 4);
1011
1341
  if (log_type == LOG_BIN)
1023
1353
}
1024
1354
 
1025
1355
 
 
1356
/*
 
1357
  Reopen the log file
 
1358
 
 
1359
  SYNOPSIS
 
1360
    reopen_file()
 
1361
 
 
1362
  DESCRIPTION
 
1363
    Reopen the log file. The method is used during FLUSH LOGS
 
1364
    and locks LOCK_log mutex
 
1365
*/
 
1366
 
 
1367
 
 
1368
void MYSQL_QUERY_LOG::reopen_file()
 
1369
{
 
1370
  char *save_name;
 
1371
 
 
1372
  if (!is_open())
 
1373
  {
 
1374
    return;
 
1375
  }
 
1376
 
 
1377
  pthread_mutex_lock(&LOCK_log);
 
1378
 
 
1379
  save_name= name;
 
1380
  name= 0;                              // Don't free name
 
1381
  close(LOG_CLOSE_TO_BE_OPENED);
 
1382
 
 
1383
  /*
 
1384
     Note that at this point, log_state != LOG_CLOSED (important for is_open()).
 
1385
  */
 
1386
 
 
1387
  open(save_name, log_type, 0, io_cache_type);
 
1388
  my_free(save_name, MYF(0));
 
1389
 
 
1390
  pthread_mutex_unlock(&LOCK_log);
 
1391
 
 
1392
  return;
 
1393
}
 
1394
 
 
1395
 
 
1396
/*
 
1397
  Write a command to traditional general log file
 
1398
 
 
1399
  SYNOPSIS
 
1400
    write()
 
1401
 
 
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
 
1411
 
 
1412
  DESCRIPTION
 
1413
 
 
1414
   Log given command to to normal (not rotable) log file
 
1415
 
 
1416
  RETURN
 
1417
    FASE - OK
 
1418
    TRUE - error occured
 
1419
*/
 
1420
 
 
1421
bool MYSQL_QUERY_LOG::write(time_t event_time,
 
1422
                            const char *user_host __attribute__((unused)),
 
1423
                            uint user_host_len __attribute__((unused)),
 
1424
                            int thread_id,
 
1425
                            const char *command_type, uint command_type_len,
 
1426
                            const char *sql_text, uint sql_text_len)
 
1427
{
 
1428
  char buff[32];
 
1429
  uint length= 0;
 
1430
  char local_time_buff[MAX_TIME_SIZE];
 
1431
  struct tm start;
 
1432
  uint time_buff_len= 0;
 
1433
 
 
1434
  (void) pthread_mutex_lock(&LOCK_log);
 
1435
 
 
1436
  /* Test if someone closed between the is_open test and lock */
 
1437
  if (is_open())
 
1438
  {
 
1439
    /* Note that my_b_write() assumes it knows the length for this */
 
1440
      if (event_time != last_time)
 
1441
      {
 
1442
        last_time= event_time;
 
1443
 
 
1444
        localtime_r(&event_time, &start);
 
1445
 
 
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);
 
1451
 
 
1452
        if (my_b_write(&log_file, (uchar*) local_time_buff, time_buff_len))
 
1453
          goto err;
 
1454
      }
 
1455
      else
 
1456
        if (my_b_write(&log_file, (uchar*) "\t\t" ,2) < 0)
 
1457
          goto err;
 
1458
 
 
1459
      /* command_type, thread_id */
 
1460
      length= snprintf(buff, 32, "%5ld ", (long) thread_id);
 
1461
 
 
1462
    if (my_b_write(&log_file, (uchar*) buff, length))
 
1463
      goto err;
 
1464
 
 
1465
    if (my_b_write(&log_file, (uchar*) command_type, command_type_len))
 
1466
      goto err;
 
1467
 
 
1468
    if (my_b_write(&log_file, (uchar*) "\t", 1))
 
1469
      goto err;
 
1470
 
 
1471
    /* sql_text */
 
1472
    if (my_b_write(&log_file, (uchar*) sql_text, sql_text_len))
 
1473
      goto err;
 
1474
 
 
1475
    if (my_b_write(&log_file, (uchar*) "\n", 1) ||
 
1476
        flush_io_cache(&log_file))
 
1477
      goto err;
 
1478
  }
 
1479
 
 
1480
  (void) pthread_mutex_unlock(&LOCK_log);
 
1481
  return false;
 
1482
err:
 
1483
 
 
1484
  if (!write_error)
 
1485
  {
 
1486
    write_error= 1;
 
1487
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
 
1488
  }
 
1489
  (void) pthread_mutex_unlock(&LOCK_log);
 
1490
  return true;
 
1491
}
 
1492
 
 
1493
 
 
1494
/*
 
1495
  Log a query to the traditional slow log file
 
1496
 
 
1497
  SYNOPSIS
 
1498
    write()
 
1499
 
 
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
 
1511
                      processed
 
1512
    sql_text_len      the length of sql_text string
 
1513
 
 
1514
  DESCRIPTION
 
1515
 
 
1516
   Log a query to the slow log file.
 
1517
 
 
1518
  RETURN
 
1519
    FALSE - OK
 
1520
    TRUE - error occured
 
1521
*/
 
1522
 
 
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)
 
1529
{
 
1530
  bool error= 0;
 
1531
 
 
1532
  (void) pthread_mutex_lock(&LOCK_log);
 
1533
 
 
1534
  if (!is_open())
 
1535
  {
 
1536
    (void) pthread_mutex_unlock(&LOCK_log);
 
1537
    return(0);
 
1538
  }
 
1539
 
 
1540
  if (is_open())
 
1541
  {                                             // Safety agains reopen
 
1542
    int tmp_errno= 0;
 
1543
    char buff[80], *end;
 
1544
    char query_time_buff[22+7], lock_time_buff[22+7];
 
1545
    uint buff_len;
 
1546
    end= buff;
 
1547
 
 
1548
    if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1549
    {
 
1550
      if (current_time != last_time)
 
1551
      {
 
1552
        last_time= current_time;
 
1553
        struct tm start;
 
1554
        localtime_r(&current_time, &start);
 
1555
 
 
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);
 
1561
 
 
1562
        /* Note that my_b_write() assumes it knows the length for this */
 
1563
        if (my_b_write(&log_file, (uchar*) buff, buff_len))
 
1564
          tmp_errno= errno;
 
1565
      }
 
1566
      const uchar uh[]= "# User@Host: ";
 
1567
      if (my_b_write(&log_file, uh, sizeof(uh) - 1))
 
1568
        tmp_errno= errno;
 
1569
      if (my_b_write(&log_file, (uchar*) user_host, user_host_len))
 
1570
        tmp_errno= errno;
 
1571
      if (my_b_write(&log_file, (uchar*) "\n", 1))
 
1572
        tmp_errno= errno;
 
1573
    }
 
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)
 
1583
      tmp_errno= errno;
 
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)
 
1587
        tmp_errno= errno;
 
1588
      strmov(db,thd->db);
 
1589
    }
 
1590
    if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
 
1591
    {
 
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,
 
1595
                            end, -10);
 
1596
    }
 
1597
    // Save value if we do an insert.
 
1598
    if (thd->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements() > 0)
 
1599
    {
 
1600
      if (!(specialflag & SPECIAL_SHORT_LOG_FORMAT))
 
1601
      {
 
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(),
 
1605
                              end, -10);
 
1606
      }
 
1607
    }
 
1608
 
 
1609
    /*
 
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
 
1613
    */
 
1614
    end= strmov(end, ",timestamp=");
 
1615
    end= int10_to_str((long) current_time, end, 10);
 
1616
 
 
1617
    if (end != buff)
 
1618
    {
 
1619
      *end++=';';
 
1620
      *end='\n';
 
1621
      if (my_b_write(&log_file, (uchar*) "SET ", 4) ||
 
1622
          my_b_write(&log_file, (uchar*) buff + 1, (uint) (end-buff)))
 
1623
        tmp_errno= errno;
 
1624
    }
 
1625
    if (is_command)
 
1626
    {
 
1627
      end= strxmov(buff, "# administrator command: ", NullS);
 
1628
      buff_len= (ulong) (end - buff);
 
1629
      my_b_write(&log_file, (uchar*) buff, buff_len);
 
1630
    }
 
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))
 
1634
      tmp_errno= errno;
 
1635
    if (tmp_errno)
 
1636
    {
 
1637
      error= 1;
 
1638
      if (! write_error)
 
1639
      {
 
1640
        write_error= 1;
 
1641
        sql_print_error(ER(ER_ERROR_ON_WRITE), name, error);
 
1642
      }
 
1643
    }
 
1644
  }
 
1645
  (void) pthread_mutex_unlock(&LOCK_log);
 
1646
  return(error);
 
1647
}
 
1648
 
 
1649
 
1026
1650
/**
1027
1651
  @todo
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.
1030
1654
*/
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)
1034
1658
{
1042
1666
  if (strip_ext)
1043
1667
  {
1044
1668
    char *p= fn_ext(log_name);
1045
 
    uint32_t length= (uint) (p - log_name);
1046
 
    strmake(buff, log_name, cmin(length, (uint)FN_REFLEN));
 
1669
    uint length= (uint) (p - log_name);
 
1670
    strmake(buff, log_name, min(length, FN_REFLEN));
1047
1671
    return (const char*)buff;
1048
1672
  }
1049
1673
  return log_name;
1051
1675
 
1052
1676
 
1053
1677
 
1054
 
DRIZZLE_BIN_LOG::DRIZZLE_BIN_LOG()
 
1678
MYSQL_BIN_LOG::MYSQL_BIN_LOG()
1055
1679
  :bytes_written(0), prepared_xids(0), file_id(1), open_count(1),
1056
1680
   need_start_event(true), m_table_map_version(0),
1057
1681
   description_event_for_exec(0), description_event_for_queue(0)
1068
1692
 
1069
1693
/* this is called only once */
1070
1694
 
1071
 
void DRIZZLE_BIN_LOG::cleanup()
 
1695
void MYSQL_BIN_LOG::cleanup()
1072
1696
{
1073
1697
  if (inited)
1074
1698
  {
1085
1709
 
1086
1710
 
1087
1711
/* Init binlog-specific vars */
1088
 
void DRIZZLE_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
 
1712
void MYSQL_BIN_LOG::init(bool no_auto_events_arg, ulong max_size_arg)
1089
1713
{
1090
1714
  no_auto_events= no_auto_events_arg;
1091
1715
  max_size= max_size_arg;
1093
1717
}
1094
1718
 
1095
1719
 
1096
 
void DRIZZLE_BIN_LOG::init_pthread_objects()
 
1720
void MYSQL_BIN_LOG::init_pthread_objects()
1097
1721
{
1098
1722
  assert(inited == 0);
1099
1723
  inited= 1;
1103
1727
}
1104
1728
 
1105
1729
 
1106
 
bool DRIZZLE_BIN_LOG::open_index_file(const char *index_file_name_arg,
 
1730
bool MYSQL_BIN_LOG::open_index_file(const char *index_file_name_arg,
1107
1731
                                const char *log_name)
1108
1732
{
1109
1733
  File index_file_nr= -1;
1123
1747
  fn_format(index_file_name, index_file_name_arg, mysql_data_home,
1124
1748
            ".index", opt);
1125
1749
  if ((index_file_nr= my_open(index_file_name,
1126
 
                              O_RDWR | O_CREAT,
 
1750
                              O_RDWR | O_CREAT | O_BINARY ,
1127
1751
                              MYF(MY_WME))) < 0 ||
1128
1752
       my_sync(index_file_nr, MYF(MY_WME)) ||
1129
1753
       init_io_cache(&index_file, index_file_nr,
1158
1782
    1   error
1159
1783
*/
1160
1784
 
1161
 
bool DRIZZLE_BIN_LOG::open(const char *log_name,
 
1785
bool MYSQL_BIN_LOG::open(const char *log_name,
1162
1786
                         enum_log_type log_type_arg,
1163
1787
                         const char *new_name,
1164
1788
                         enum cache_type io_cache_type_arg,
1171
1795
  write_error=0;
1172
1796
 
1173
1797
  /* open the main log file */
1174
 
  if (DRIZZLE_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
 
1798
  if (MYSQL_LOG::open(log_name, log_type_arg, new_name, io_cache_type_arg))
1175
1799
    return(1);                            /* all warnings issued */
1176
1800
 
1177
1801
  init(no_auto_events_arg, max_size_arg);
1191
1815
        an extension for the binary log files.
1192
1816
        In this case we write a standard header to it.
1193
1817
      */
1194
 
      if (my_b_safe_write(&log_file, (unsigned char*) BINLOG_MAGIC,
 
1818
      if (my_b_safe_write(&log_file, (uchar*) BINLOG_MAGIC,
1195
1819
                          BIN_LOG_HEADER_SIZE))
1196
1820
        goto err;
1197
1821
      bytes_written+= BIN_LOG_HEADER_SIZE;
1262
1886
        As this is a new log file, we write the file name to the index
1263
1887
        file. As every time we write to the index file, we sync it.
1264
1888
      */
1265
 
      if (my_b_write(&index_file, (unsigned char*) log_file_name,
 
1889
      if (my_b_write(&index_file, (uchar*) log_file_name,
1266
1890
                     strlen(log_file_name)) ||
1267
 
          my_b_write(&index_file, (unsigned char*) "\n", 1) ||
 
1891
          my_b_write(&index_file, (uchar*) "\n", 1) ||
1268
1892
          flush_io_cache(&index_file) ||
1269
1893
          my_sync(index_file.file, MYF(MY_WME)))
1270
1894
        goto err;
1275
1899
  return(0);
1276
1900
 
1277
1901
err:
1278
 
  sql_print_error(_("Could not use %s for logging (error %d). "
1279
 
                    "Turning logging off for the whole duration of the "
1280
 
                    "Drizzle server process. "
1281
 
                    "To turn it on again: fix the cause, "
1282
 
                    "shutdown the Drizzle server and restart it."),
1283
 
                    name, errno);
 
1902
  sql_print_error("Could not use %s for logging (error %d). \
 
1903
Turning logging off for the whole duration of the MySQL server process. \
 
1904
To turn it on again: fix the cause, \
 
1905
shutdown the MySQL server and restart it.", name, errno);
1284
1906
  if (file >= 0)
1285
1907
    my_close(file,MYF(0));
1286
1908
  end_io_cache(&log_file);
1287
1909
  end_io_cache(&index_file);
1288
 
  if (name)
1289
 
  {
1290
 
    free(name);
1291
 
    name= NULL;
1292
 
  }
 
1910
  safeFree(name);
1293
1911
  log_state= LOG_CLOSED;
1294
1912
  return(1);
1295
1913
}
1296
1914
 
1297
1915
 
1298
 
int DRIZZLE_BIN_LOG::get_current_log(LOG_INFO* linfo)
 
1916
int MYSQL_BIN_LOG::get_current_log(LOG_INFO* linfo)
1299
1917
{
1300
1918
  pthread_mutex_lock(&LOCK_log);
1301
1919
  int ret = raw_get_current_log(linfo);
1303
1921
  return ret;
1304
1922
}
1305
1923
 
1306
 
int DRIZZLE_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
 
1924
int MYSQL_BIN_LOG::raw_get_current_log(LOG_INFO* linfo)
1307
1925
{
1308
1926
  strmake(linfo->log_file_name, log_file_name, sizeof(linfo->log_file_name)-1);
1309
1927
  linfo->pos = my_b_tell(&log_file);
1327
1945
    0   ok
1328
1946
*/
1329
1947
 
 
1948
#ifdef HAVE_REPLICATION
 
1949
 
1330
1950
static bool copy_up_file_and_fill(IO_CACHE *index_file, my_off_t offset)
1331
1951
{
1332
1952
  int bytes_read;
1333
1953
  my_off_t init_offset= offset;
1334
1954
  File file= index_file->file;
1335
 
  unsigned char io_buf[IO_SIZE*2];
 
1955
  uchar io_buf[IO_SIZE*2];
1336
1956
 
1337
1957
  for (;; offset+= bytes_read)
1338
1958
  {
1358
1978
  return(1);
1359
1979
}
1360
1980
 
 
1981
#endif /* HAVE_REPLICATION */
 
1982
 
1361
1983
/**
1362
1984
  Find the position in the log-index-file for the given log name.
1363
1985
 
1380
2002
    LOG_INFO_IO         Got IO error while reading file
1381
2003
*/
1382
2004
 
1383
 
int DRIZZLE_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
 
2005
int MYSQL_BIN_LOG::find_log_pos(LOG_INFO *linfo, const char *log_name,
1384
2006
                            bool need_lock)
1385
2007
{
1386
2008
  int error= 0;
1387
2009
  char *fname= linfo->log_file_name;
1388
 
  uint32_t log_name_len= log_name ? (uint) strlen(log_name) : 0;
 
2010
  uint log_name_len= log_name ? (uint) strlen(log_name) : 0;
1389
2011
 
1390
2012
  /*
1391
2013
    Mutex needed because we need to make sure the file pointer does not
1400
2022
 
1401
2023
  for (;;)
1402
2024
  {
1403
 
    uint32_t length;
 
2025
    uint length;
1404
2026
    my_off_t offset= my_b_tell(&index_file);
1405
2027
    /* If we get 0 or 1 characters, this is the end of the file */
1406
2028
 
1453
2075
    LOG_INFO_IO         Got IO error while reading file
1454
2076
*/
1455
2077
 
1456
 
int DRIZZLE_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
 
2078
int MYSQL_BIN_LOG::find_next_log(LOG_INFO* linfo, bool need_lock)
1457
2079
{
1458
2080
  int error= 0;
1459
 
  uint32_t length;
 
2081
  uint length;
1460
2082
  char *fname= linfo->log_file_name;
1461
2083
 
1462
2084
  if (need_lock)
1500
2122
    1   error
1501
2123
*/
1502
2124
 
1503
 
bool DRIZZLE_BIN_LOG::reset_logs(THD* thd)
 
2125
bool MYSQL_BIN_LOG::reset_logs(THD* thd)
1504
2126
{
1505
2127
  LOG_INFO linfo;
1506
2128
  bool error=0;
1507
2129
  const char* save_name;
1508
2130
 
 
2131
  ha_reset_logs(thd);
1509
2132
  /*
1510
2133
    We need to get both locks to be sure that no one is trying to
1511
2134
    write to the index log file.
1519
2142
    thread. If the transaction involved MyISAM tables, it should go
1520
2143
    into binlog even on rollback.
1521
2144
  */
1522
 
  pthread_mutex_lock(&LOCK_thread_count);
 
2145
  VOID(pthread_mutex_lock(&LOCK_thread_count));
1523
2146
 
1524
2147
  /* Save variables so that we can reopen the log */
1525
2148
  save_name=name;
1528
2151
 
1529
2152
  /* First delete all old log files */
1530
2153
 
1531
 
  if (find_log_pos(&linfo, NULL, 0))
 
2154
  if (find_log_pos(&linfo, NullS, 0))
1532
2155
  {
1533
2156
    error=1;
1534
2157
    goto err;
1543
2166
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1544
2167
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1545
2168
                            linfo.log_file_name);
1546
 
        sql_print_information(_("Failed to delete file '%s'"),
 
2169
        sql_print_information("Failed to delete file '%s'",
1547
2170
                              linfo.log_file_name);
1548
2171
        my_errno= 0;
1549
2172
        error= 0;
1552
2175
      {
1553
2176
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1554
2177
                            ER_BINLOG_PURGE_FATAL_ERR,
1555
 
                            _("a problem with deleting %s; "
 
2178
                            "a problem with deleting %s; "
1556
2179
                            "consider examining correspondence "
1557
2180
                            "of your binlog index file "
1558
 
                            "to the actual binlog files"),
 
2181
                            "to the actual binlog files",
1559
2182
                            linfo.log_file_name);
1560
2183
        error= 1;
1561
2184
        goto err;
1574
2197
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1575
2198
                          ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1576
2199
                          index_file_name);
1577
 
      sql_print_information(_("Failed to delete file '%s'"),
 
2200
      sql_print_information("Failed to delete file '%s'",
1578
2201
                            index_file_name);
1579
2202
      my_errno= 0;
1580
2203
      error= 0;
1596
2219
    need_start_event=1;
1597
2220
  if (!open_index_file(index_file_name, 0))
1598
2221
    open(save_name, log_type, 0, io_cache_type, no_auto_events, max_size, 0);
1599
 
  free((unsigned char*) save_name);
 
2222
  my_free((uchar*) save_name, MYF(0));
1600
2223
 
1601
2224
err:
1602
 
  pthread_mutex_unlock(&LOCK_thread_count);
 
2225
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
1603
2226
  pthread_mutex_unlock(&LOCK_index);
1604
2227
  pthread_mutex_unlock(&LOCK_log);
1605
2228
  return(error);
1643
2266
    LOG_INFO_IO         Got IO error while reading file
1644
2267
*/
1645
2268
 
 
2269
#ifdef HAVE_REPLICATION
1646
2270
 
1647
 
int DRIZZLE_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
 
2271
int MYSQL_BIN_LOG::purge_first_log(Relay_log_info* rli, bool included)
1648
2272
{
1649
2273
  int error;
1650
2274
 
1651
2275
  assert(is_open());
1652
2276
  assert(rli->slave_running == 1);
1653
 
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name.c_str()));
 
2277
  assert(!strcmp(rli->linfo.log_file_name,rli->event_relay_log_name));
1654
2278
 
1655
2279
  pthread_mutex_lock(&LOCK_index);
1656
2280
  pthread_mutex_lock(&rli->log_space_lock);
1657
 
  rli->relay_log.purge_logs(rli->group_relay_log_name.c_str(), included,
 
2281
  rli->relay_log.purge_logs(rli->group_relay_log_name, included,
1658
2282
                            0, 0, &rli->log_space_total);
1659
2283
  // Tell the I/O thread to take the relay_log_space_limit into account
1660
2284
  rli->ignore_log_space_limit= 0;
1673
2297
    If included is true, we want the first relay log;
1674
2298
    otherwise we want the one after event_relay_log_name.
1675
2299
  */
1676
 
  if ((included && (error=find_log_pos(&rli->linfo, NULL, 0))) ||
 
2300
  if ((included && (error=find_log_pos(&rli->linfo, NullS, 0))) ||
1677
2301
      (!included &&
1678
 
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name.c_str(), 0)) ||
 
2302
       ((error=find_log_pos(&rli->linfo, rli->event_relay_log_name, 0)) ||
1679
2303
        (error=find_next_log(&rli->linfo, 0)))))
1680
2304
  {
1681
2305
    char buff[22];
1682
 
    sql_print_error(_("next log error: %d  offset: %s  log: %s included: %d"),
 
2306
    sql_print_error("next log error: %d  offset: %s  log: %s included: %d",
1683
2307
                    error,
1684
2308
                    llstr(rli->linfo.index_file_offset,buff),
1685
 
                    rli->group_relay_log_name.c_str(),
 
2309
                    rli->group_relay_log_name,
1686
2310
                    included);
1687
2311
    goto err;
1688
2312
  }
1691
2315
    Reset rli's coordinates to the current log.
1692
2316
  */
1693
2317
  rli->event_relay_log_pos= BIN_LOG_HEADER_SIZE;
1694
 
  rli->event_relay_log_name.assign(rli->linfo.log_file_name);
 
2318
  strmake(rli->event_relay_log_name,rli->linfo.log_file_name,
 
2319
          sizeof(rli->event_relay_log_name)-1);
1695
2320
 
1696
2321
  /*
1697
2322
    If we removed the rli->group_relay_log_name file,
1701
2326
  if (included)
1702
2327
  {
1703
2328
    rli->group_relay_log_pos = BIN_LOG_HEADER_SIZE;
1704
 
    rli->group_relay_log_name.assign(rli->linfo.log_file_name);
 
2329
    strmake(rli->group_relay_log_name,rli->linfo.log_file_name,
 
2330
            sizeof(rli->group_relay_log_name)-1);
1705
2331
    rli->notify_group_relay_log_name_update();
1706
2332
  }
1707
2333
 
1717
2343
  Update log index_file.
1718
2344
*/
1719
2345
 
1720
 
int DRIZZLE_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
 
2346
int MYSQL_BIN_LOG::update_log_index(LOG_INFO* log_info, bool need_update_threads)
1721
2347
{
1722
2348
  if (copy_up_file_and_fill(&index_file, log_info->index_file_start_offset))
1723
2349
    return LOG_INFO_IO;
1752
2378
                                stat() or my_delete()
1753
2379
*/
1754
2380
 
1755
 
int DRIZZLE_BIN_LOG::purge_logs(const char *to_log, 
 
2381
int MYSQL_BIN_LOG::purge_logs(const char *to_log, 
1756
2382
                          bool included,
1757
2383
                          bool need_mutex, 
1758
2384
                          bool need_update_threads, 
1772
2398
    File name exists in index file; delete until we find this file
1773
2399
    or a file that is used.
1774
2400
  */
1775
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2401
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1776
2402
    goto err;
1777
2403
  while ((strcmp(to_log,log_info.log_file_name) || (exit_loop=included)) &&
1778
2404
         !log_in_use(log_info.log_file_name))
1789
2415
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1790
2416
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1791
2417
                            log_info.log_file_name);
1792
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2418
        sql_print_information("Failed to execute stat on file '%s'",
1793
2419
                              log_info.log_file_name);
1794
2420
        my_errno= 0;
1795
2421
      }
1800
2426
        */
1801
2427
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1802
2428
                            ER_BINLOG_PURGE_FATAL_ERR,
1803
 
                            _("a problem with getting info on being purged %s; "
 
2429
                            "a problem with getting info on being purged %s; "
1804
2430
                            "consider examining correspondence "
1805
2431
                            "of your binlog index file "
1806
 
                            "to the actual binlog files"),
 
2432
                            "to the actual binlog files",
1807
2433
                            log_info.log_file_name);
1808
2434
        error= LOG_INFO_FATAL;
1809
2435
        goto err;
1823
2449
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1824
2450
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1825
2451
                              log_info.log_file_name);
1826
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2452
          sql_print_information("Failed to delete file '%s'",
1827
2453
                                log_info.log_file_name);
1828
2454
          my_errno= 0;
1829
2455
        }
1831
2457
        {
1832
2458
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1833
2459
                              ER_BINLOG_PURGE_FATAL_ERR,
1834
 
                              _("a problem with deleting %s; "
 
2460
                              "a problem with deleting %s; "
1835
2461
                              "consider examining correspondence "
1836
2462
                              "of your binlog index file "
1837
 
                              "to the actual binlog files"),
 
2463
                              "to the actual binlog files",
1838
2464
                              log_info.log_file_name);
1839
2465
          if (my_errno == EMFILE)
1840
2466
          {
1846
2472
      }
1847
2473
    }
1848
2474
 
 
2475
    ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
 
2476
 
1849
2477
    if (find_next_log(&log_info, 0) || exit_loop)
1850
2478
      break;
1851
2479
  }
1884
2512
                                stat() or my_delete()
1885
2513
*/
1886
2514
 
1887
 
int DRIZZLE_BIN_LOG::purge_logs_before_date(time_t purge_time)
 
2515
int MYSQL_BIN_LOG::purge_logs_before_date(time_t purge_time)
1888
2516
{
1889
2517
  int error;
1890
2518
  LOG_INFO log_info;
1897
2525
    or a file that is used or a file
1898
2526
    that is older than purge_time.
1899
2527
  */
1900
 
  if ((error=find_log_pos(&log_info, NULL, 0 /*no mutex*/)))
 
2528
  if ((error=find_log_pos(&log_info, NullS, 0 /*no mutex*/)))
1901
2529
    goto err;
1902
2530
 
1903
2531
  while (strcmp(log_file_name, log_info.log_file_name) &&
1913
2541
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1914
2542
                            ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1915
2543
                            log_info.log_file_name);
1916
 
        sql_print_information(_("Failed to execute stat() on file '%s'"),
 
2544
        sql_print_information("Failed to execute stat on file '%s'",
1917
2545
                              log_info.log_file_name);
1918
2546
        my_errno= 0;
1919
2547
      }
1924
2552
        */
1925
2553
        push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1926
2554
                            ER_BINLOG_PURGE_FATAL_ERR,
1927
 
                            _("a problem with getting info on being purged %s; "
 
2555
                            "a problem with getting info on being purged %s; "
1928
2556
                            "consider examining correspondence "
1929
2557
                            "of your binlog index file "
1930
 
                            "to the actual binlog files"),
 
2558
                            "to the actual binlog files",
1931
2559
                            log_info.log_file_name);
1932
2560
        error= LOG_INFO_FATAL;
1933
2561
        goto err;
1945
2573
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1946
2574
                              ER_LOG_PURGE_NO_FILE, ER(ER_LOG_PURGE_NO_FILE),
1947
2575
                              log_info.log_file_name);
1948
 
          sql_print_information(_("Failed to delete file '%s'"),
 
2576
          sql_print_information("Failed to delete file '%s'",
1949
2577
                                log_info.log_file_name);
1950
2578
          my_errno= 0;
1951
2579
        }
1953
2581
        {
1954
2582
          push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1955
2583
                              ER_BINLOG_PURGE_FATAL_ERR,
1956
 
                              _("a problem with deleting %s; "
 
2584
                              "a problem with deleting %s; "
1957
2585
                              "consider examining correspondence "
1958
2586
                              "of your binlog index file "
1959
 
                              "to the actual binlog files"),
 
2587
                              "to the actual binlog files",
1960
2588
                              log_info.log_file_name);
1961
2589
          error= LOG_INFO_FATAL;
1962
2590
          goto err;
1963
2591
        }
1964
2592
      }
 
2593
      ha_binlog_index_purge_file(current_thd, log_info.log_file_name);
1965
2594
    }
1966
2595
    if (find_next_log(&log_info, 0))
1967
2596
      break;
1977
2606
  pthread_mutex_unlock(&LOCK_index);
1978
2607
  return(error);
1979
2608
}
 
2609
#endif /* HAVE_REPLICATION */
1980
2610
 
1981
2611
 
1982
2612
/**
1988
2618
    If file name will be longer then FN_REFLEN it will be truncated
1989
2619
*/
1990
2620
 
1991
 
void DRIZZLE_BIN_LOG::make_log_name(char* buf, const char* log_ident)
 
2621
void MYSQL_BIN_LOG::make_log_name(char* buf, const char* log_ident)
1992
2622
{
1993
 
  uint32_t dir_len = dirname_length(log_file_name); 
 
2623
  uint dir_len = dirname_length(log_file_name); 
1994
2624
  if (dir_len >= FN_REFLEN)
1995
2625
    dir_len=FN_REFLEN-1;
1996
 
  my_stpncpy(buf, log_file_name, dir_len);
 
2626
  strnmov(buf, log_file_name, dir_len);
1997
2627
  strmake(buf+dir_len, log_ident, FN_REFLEN - dir_len -1);
1998
2628
}
1999
2629
 
2002
2632
  Check if we are writing/reading to the given log file.
2003
2633
*/
2004
2634
 
2005
 
bool DRIZZLE_BIN_LOG::is_active(const char *log_file_name_arg)
 
2635
bool MYSQL_BIN_LOG::is_active(const char *log_file_name_arg)
2006
2636
{
2007
2637
  return !strcmp(log_file_name, log_file_name_arg);
2008
2638
}
2016
2646
  method).
2017
2647
*/
2018
2648
 
2019
 
void DRIZZLE_BIN_LOG::new_file()
 
2649
void MYSQL_BIN_LOG::new_file()
2020
2650
{
2021
2651
  new_file_impl(1);
2022
2652
}
2023
2653
 
2024
2654
 
2025
 
void DRIZZLE_BIN_LOG::new_file_without_locking()
 
2655
void MYSQL_BIN_LOG::new_file_without_locking()
2026
2656
{
2027
2657
  new_file_impl(0);
2028
2658
}
2037
2667
    The new file name is stored last in the index file
2038
2668
*/
2039
2669
 
2040
 
void DRIZZLE_BIN_LOG::new_file_impl(bool need_lock)
 
2670
void MYSQL_BIN_LOG::new_file_impl(bool need_lock)
2041
2671
{
2042
2672
  char new_name[FN_REFLEN], *new_name_ptr, *old_name;
2043
2673
 
2123
2753
 
2124
2754
  open(old_name, log_type, new_name_ptr,
2125
2755
       io_cache_type, no_auto_events, max_size, 1);
2126
 
  free(old_name);
 
2756
  my_free(old_name,MYF(0));
2127
2757
 
2128
2758
end:
2129
2759
  if (need_lock)
2134
2764
}
2135
2765
 
2136
2766
 
2137
 
bool DRIZZLE_BIN_LOG::append(Log_event* ev)
 
2767
bool MYSQL_BIN_LOG::append(Log_event* ev)
2138
2768
{
2139
2769
  bool error = 0;
2140
2770
  pthread_mutex_lock(&LOCK_log);
2160
2790
}
2161
2791
 
2162
2792
 
2163
 
bool DRIZZLE_BIN_LOG::appendv(const char* buf, uint32_t len,...)
 
2793
bool MYSQL_BIN_LOG::appendv(const char* buf, uint len,...)
2164
2794
{
2165
2795
  bool error= 0;
2166
2796
  va_list(args);
2171
2801
  safe_mutex_assert_owner(&LOCK_log);
2172
2802
  do
2173
2803
  {
2174
 
    if (my_b_append(&log_file,(unsigned char*) buf,len))
 
2804
    if (my_b_append(&log_file,(uchar*) buf,len))
2175
2805
    {
2176
2806
      error= 1;
2177
2807
      goto err;
2188
2818
}
2189
2819
 
2190
2820
 
2191
 
bool DRIZZLE_BIN_LOG::flush_and_sync()
 
2821
bool MYSQL_BIN_LOG::flush_and_sync()
2192
2822
{
2193
2823
  int err=0, fd=log_file.file;
2194
2824
  safe_mutex_assert_owner(&LOCK_log);
2202
2832
  return err;
2203
2833
}
2204
2834
 
2205
 
void DRIZZLE_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
 
2835
void MYSQL_BIN_LOG::start_union_events(THD *thd, query_id_t query_id_param)
2206
2836
{
2207
2837
  assert(!thd->binlog_evt_union.do_union);
2208
2838
  thd->binlog_evt_union.do_union= true;
2211
2841
  thd->binlog_evt_union.first_query_id= query_id_param;
2212
2842
}
2213
2843
 
2214
 
void DRIZZLE_BIN_LOG::stop_union_events(THD *thd)
 
2844
void MYSQL_BIN_LOG::stop_union_events(THD *thd)
2215
2845
{
2216
2846
  assert(thd->binlog_evt_union.do_union);
2217
2847
  thd->binlog_evt_union.do_union= false;
2218
2848
}
2219
2849
 
2220
 
bool DRIZZLE_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
 
2850
bool MYSQL_BIN_LOG::is_query_in_union(THD *thd, query_id_t query_id_param)
2221
2851
{
2222
2852
  return (thd->binlog_evt_union.do_union && 
2223
2853
          query_id_param >= thd->binlog_evt_union.first_query_id);
2242
2872
      open_cached_file(&trx_data->trans_log, mysql_tmpdir,
2243
2873
                       LOG_PREFIX, binlog_cache_size, MYF(MY_WME)))
2244
2874
  {
2245
 
    free((unsigned char*)trx_data);
 
2875
    my_free((uchar*)trx_data, MYF(MY_ALLOW_ZERO_PTR));
2246
2876
    return(1);                      // Didn't manage to set it up
2247
2877
  }
2248
2878
  thd_set_ha_data(this, binlog_hton, trx_data);
2327
2957
  Write a table map to the binary log.
2328
2958
 */
2329
2959
 
2330
 
int THD::binlog_write_table_map(Table *table, bool is_trans)
 
2960
int THD::binlog_write_table_map(TABLE *table, bool is_trans)
2331
2961
{
2332
2962
  int error;
2333
2963
 
2334
2964
  /* Pre-conditions */
2335
2965
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2336
 
  assert(table->s->table_map_id != UINT32_MAX);
 
2966
  assert(table->s->table_map_id != ULONG_MAX);
2337
2967
 
2338
2968
  Table_map_log_event::flag_set const
2339
2969
    flags= Table_map_log_event::TM_NO_FLAGS;
2386
3016
  event.
2387
3017
*/
2388
3018
int
2389
 
DRIZZLE_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
 
3019
MYSQL_BIN_LOG::flush_and_set_pending_rows_event(THD *thd,
2390
3020
                                                Rows_log_event* event)
2391
3021
{
2392
3022
  assert(mysql_bin_log.is_open());
2468
3098
  Write an event to the binary log.
2469
3099
*/
2470
3100
 
2471
 
bool DRIZZLE_BIN_LOG::write(Log_event *event_info)
 
3101
bool MYSQL_BIN_LOG::write(Log_event *event_info)
2472
3102
{
2473
3103
  THD *thd= event_info->thd;
2474
3104
  bool error= 1;
2516
3146
    if ((thd && !(thd->options & OPTION_BIN_LOG)) ||
2517
3147
        (!binlog_filter->db_ok(local_db)))
2518
3148
    {
2519
 
      pthread_mutex_unlock(&LOCK_log);
 
3149
      VOID(pthread_mutex_unlock(&LOCK_log));
2520
3150
      return(0);
2521
3151
    }
2522
3152
 
2572
3202
      {
2573
3203
        if (thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt)
2574
3204
        {
2575
 
          Intvar_log_event e(thd,(unsigned char) LAST_INSERT_ID_EVENT,
 
3205
          Intvar_log_event e(thd,(uchar) LAST_INSERT_ID_EVENT,
2576
3206
                             thd->first_successful_insert_id_in_prev_stmt_for_binlog);
2577
3207
          if (e.write(file))
2578
3208
            goto err;
2584
3214
            MyISAM or BDB) (table->next_number_keypart != 0), such event is
2585
3215
            in fact not necessary. We could avoid logging it.
2586
3216
          */
2587
 
          Intvar_log_event e(thd, (unsigned char) INSERT_ID_EVENT,
 
3217
          Intvar_log_event e(thd, (uchar) INSERT_ID_EVENT,
2588
3218
                             thd->auto_inc_intervals_in_cur_stmt_for_binlog.
2589
3219
                             minimum());
2590
3220
          if (e.write(file))
2598
3228
        }
2599
3229
        if (thd->user_var_events.elements)
2600
3230
        {
2601
 
          for (uint32_t i= 0; i < thd->user_var_events.elements; i++)
 
3231
          for (uint i= 0; i < thd->user_var_events.elements; i++)
2602
3232
          {
2603
3233
            BINLOG_USER_VAR_EVENT *user_var_event;
2604
 
            get_dynamic(&thd->user_var_events,(unsigned char*) &user_var_event, i);
 
3234
            get_dynamic(&thd->user_var_events,(uchar*) &user_var_event, i);
2605
3235
            User_var_log_event e(thd, user_var_event->user_var_event->name.str,
2606
3236
                                 user_var_event->user_var_event->name.length,
2607
3237
                                 user_var_event->value,
2656
3286
  return logger.error_log_print(level, format, args);
2657
3287
}
2658
3288
 
2659
 
void DRIZZLE_BIN_LOG::rotate_and_purge(uint32_t flags)
 
3289
 
 
3290
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
3291
                    uint64_t current_utime)
 
3292
{
 
3293
  return logger.slow_log_print(thd, query, query_length, current_utime);
 
3294
}
 
3295
 
 
3296
 
 
3297
bool LOGGER::log_command(THD *thd, enum enum_server_command command)
 
3298
{
 
3299
  /*
 
3300
    Log command if we have at least one log event handler enabled and want
 
3301
    to log this king of commands
 
3302
  */
 
3303
  if (*general_log_handler_list && (what_to_log & (1L << (uint) command)))
 
3304
  {
 
3305
    if (thd->options & OPTION_LOG_OFF)
 
3306
    {
 
3307
      /* No logging */
 
3308
      return false;
 
3309
    }
 
3310
 
 
3311
    return true;
 
3312
  }
 
3313
 
 
3314
  return false;
 
3315
}
 
3316
 
 
3317
 
 
3318
bool general_log_print(THD *thd, enum enum_server_command command,
 
3319
                       const char *format, ...)
 
3320
{
 
3321
  va_list args;
 
3322
  uint error= 0;
 
3323
 
 
3324
  /* Print the message to the buffer if we want to log this king of commands */
 
3325
  if (! logger.log_command(thd, command))
 
3326
    return false;
 
3327
 
 
3328
  va_start(args, format);
 
3329
  error= logger.general_log_print(thd, command, format, args);
 
3330
  va_end(args);
 
3331
 
 
3332
  return error;
 
3333
}
 
3334
 
 
3335
bool general_log_write(THD *thd, enum enum_server_command command,
 
3336
                       const char *query, uint query_length)
 
3337
{
 
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);
 
3341
 
 
3342
  return false;
 
3343
}
 
3344
 
 
3345
void MYSQL_BIN_LOG::rotate_and_purge(uint flags)
2660
3346
{
2661
3347
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2662
3348
    pthread_mutex_lock(&LOCK_log);
2664
3350
      (my_b_tell(&log_file) >= (my_off_t) max_size))
2665
3351
  {
2666
3352
    new_file_without_locking();
 
3353
#ifdef HAVE_REPLICATION
2667
3354
    if (expire_logs_days)
2668
3355
    {
2669
3356
      time_t purge_time= my_time(0) - expire_logs_days*24*60*60;
2670
3357
      if (purge_time >= 0)
2671
3358
        purge_logs_before_date(purge_time);
2672
3359
    }
 
3360
#endif
2673
3361
  }
2674
3362
  if (!(flags & RP_LOCK_LOG_IS_ALREADY_LOCKED))
2675
3363
    pthread_mutex_unlock(&LOCK_log);
2676
3364
}
2677
3365
 
2678
 
uint32_t DRIZZLE_BIN_LOG::next_file_id()
 
3366
uint MYSQL_BIN_LOG::next_file_id()
2679
3367
{
2680
 
  uint32_t res;
 
3368
  uint res;
2681
3369
  pthread_mutex_lock(&LOCK_log);
2682
3370
  res = file_id++;
2683
3371
  pthread_mutex_unlock(&LOCK_log);
2699
3387
    be reset as a READ_CACHE to be able to read the contents from it.
2700
3388
 */
2701
3389
 
2702
 
int DRIZZLE_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
 
3390
int MYSQL_BIN_LOG::write_cache(IO_CACHE *cache, bool lock_log, bool sync_log)
2703
3391
{
2704
3392
  Mutex_sentry sentry(lock_log ? &LOCK_log : NULL);
2705
3393
 
2706
3394
  if (reinit_io_cache(cache, READ_CACHE, 0, 0, 0))
2707
3395
    return ER_ERROR_ON_WRITE;
2708
 
  uint32_t length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
 
3396
  uint length= my_b_bytes_in_cache(cache), group, carry, hdr_offs;
2709
3397
  long val;
2710
 
  unsigned char header[LOG_EVENT_HEADER_LEN];
 
3398
  uchar header[LOG_EVENT_HEADER_LEN];
2711
3399
 
2712
3400
  /*
2713
3401
    The events in the buffer have incorrect end_log_pos data
2788
3476
        {
2789
3477
          /* we've got a full event-header, and it came in one piece */
2790
3478
 
2791
 
          unsigned char *log_pos= (unsigned char *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
 
3479
          uchar *log_pos= (uchar *)cache->read_pos + hdr_offs + LOG_POS_OFFSET;
2792
3480
 
2793
3481
          /* fix end_log_pos */
2794
3482
          val= uint4korr(log_pos) + group;
2795
3483
          int4store(log_pos, val);
2796
3484
 
2797
3485
          /* next event header at ... */
2798
 
          log_pos= (unsigned char *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
 
3486
          log_pos= (uchar *)cache->read_pos + hdr_offs + EVENT_LEN_OFFSET;
2799
3487
          hdr_offs += uint4korr(log_pos);
2800
3488
 
2801
3489
        }
2847
3535
    'cache' needs to be reinitialized after this functions returns.
2848
3536
*/
2849
3537
 
2850
 
bool DRIZZLE_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
 
3538
bool MYSQL_BIN_LOG::write(THD *thd, IO_CACHE *cache, Log_event *commit_event)
2851
3539
{
2852
 
  pthread_mutex_lock(&LOCK_log);
 
3540
  VOID(pthread_mutex_lock(&LOCK_log));
2853
3541
 
2854
3542
  /* NULL would represent nothing to replicate after ROLLBACK */
2855
3543
  assert(commit_event != NULL);
2925
3613
    else
2926
3614
      rotate_and_purge(RP_LOCK_LOG_IS_ALREADY_LOCKED);
2927
3615
  }
2928
 
  pthread_mutex_unlock(&LOCK_log);
 
3616
  VOID(pthread_mutex_unlock(&LOCK_log));
2929
3617
 
2930
3618
  return(0);
2931
3619
 
2935
3623
    write_error= 1;
2936
3624
    sql_print_error(ER(ER_ERROR_ON_WRITE), name, errno);
2937
3625
  }
2938
 
  pthread_mutex_unlock(&LOCK_log);
 
3626
  VOID(pthread_mutex_unlock(&LOCK_log));
2939
3627
  return(1);
2940
3628
}
2941
3629
 
2949
3637
    It will be released at the end of the function.
2950
3638
*/
2951
3639
 
2952
 
void DRIZZLE_BIN_LOG::wait_for_update_relay_log(THD* thd)
 
3640
void MYSQL_BIN_LOG::wait_for_update_relay_log(THD* thd)
2953
3641
{
2954
3642
  const char *old_msg;
2955
3643
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2978
3666
    LOCK_log is released by the caller.
2979
3667
*/
2980
3668
 
2981
 
int DRIZZLE_BIN_LOG::wait_for_update_bin_log(THD* thd,
 
3669
int MYSQL_BIN_LOG::wait_for_update_bin_log(THD* thd,
2982
3670
                                           const struct timespec *timeout)
2983
3671
{
2984
3672
  int ret= 0;
2985
 
  const char* old_msg = thd->get_proc_info();
 
3673
  const char* old_msg = thd->proc_info;
2986
3674
  old_msg= thd->enter_cond(&update_cond, &LOCK_log,
2987
3675
                           "Master has sent all binlog to slave; "
2988
3676
                           "waiting for binlog to be updated");
3009
3697
    The internal structures are not freed until cleanup() is called
3010
3698
*/
3011
3699
 
3012
 
void DRIZZLE_BIN_LOG::close(uint32_t exiting)
 
3700
void MYSQL_BIN_LOG::close(uint exiting)
3013
3701
{                                       // One can't set log_type here!
3014
3702
  if (log_state == LOG_OPENED)
3015
3703
  {
 
3704
#ifdef HAVE_REPLICATION
3016
3705
    if (log_type == LOG_BIN && !no_auto_events &&
3017
3706
        (exiting & LOG_CLOSE_STOP_EVENT))
3018
3707
    {
3021
3710
      bytes_written+= s.data_written;
3022
3711
      signal_update();
3023
3712
    }
 
3713
#endif /* HAVE_REPLICATION */
3024
3714
 
3025
3715
    /* don't pwrite in a file opened with O_APPEND - it doesn't work */
3026
3716
    if (log_file.type == WRITE_CACHE && log_type == LOG_BIN)
3027
3717
    {
3028
3718
      my_off_t offset= BIN_LOG_HEADER_SIZE + FLAGS_OFFSET;
3029
 
      unsigned char flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
 
3719
      uchar flags= 0;            // clearing LOG_EVENT_BINLOG_IN_USE_F
3030
3720
      pwrite(log_file.file, &flags, 1, offset);
3031
3721
    }
3032
3722
 
3033
3723
    /* this will cleanup IO_CACHE, sync and close the file */
3034
 
    DRIZZLE_LOG::close(exiting);
 
3724
    MYSQL_LOG::close(exiting);
3035
3725
  }
3036
3726
 
3037
3727
  /*
3049
3739
    }
3050
3740
  }
3051
3741
  log_state= (exiting & LOG_CLOSE_TO_BE_OPENED) ? LOG_TO_BE_OPENED : LOG_CLOSED;
3052
 
  if (name)
3053
 
  {
3054
 
    free(name);
3055
 
    name= NULL;
3056
 
  }
 
3742
  safeFree(name);
3057
3743
  return;
3058
3744
}
3059
3745
 
3060
3746
 
3061
 
void DRIZZLE_BIN_LOG::set_max_size(ulong max_size_arg)
 
3747
void MYSQL_BIN_LOG::set_max_size(ulong max_size_arg)
3062
3748
{
3063
3749
  /*
3064
3750
    We need to take locks, otherwise this may happen:
3126
3812
 
3127
3813
void sql_perror(const char *message)
3128
3814
{
 
3815
#ifdef HAVE_STRERROR
3129
3816
  sql_print_error("%s: %s",message, strerror(errno));
 
3817
#else
 
3818
  perror(message);
 
3819
#endif
3130
3820
}
3131
3821
 
3132
3822
 
3137
3827
  {
3138
3828
    char err_renamed[FN_REFLEN], *end;
3139
3829
    end= strmake(err_renamed,log_error_file,FN_REFLEN-4);
3140
 
    my_stpcpy(end, "-old");
3141
 
    pthread_mutex_lock(&LOCK_error_log);
 
3830
    strmov(end, "-old");
 
3831
    VOID(pthread_mutex_lock(&LOCK_error_log));
3142
3832
    char err_temp[FN_REFLEN+4];
3143
3833
    /*
3144
3834
     On Windows is necessary a temporary file for to rename
3145
3835
     the current error file.
3146
3836
    */
3147
 
    strxmov(err_temp, err_renamed,"-tmp",NULL);
 
3837
    strxmov(err_temp, err_renamed,"-tmp",NullS);
3148
3838
    (void) my_delete(err_temp, MYF(0)); 
3149
3839
    if (freopen(err_temp,"a+",stdout))
3150
3840
    {
3151
3841
      int fd;
3152
3842
      size_t bytes;
3153
 
      unsigned char buf[IO_SIZE];
 
3843
      uchar buf[IO_SIZE];
3154
3844
 
3155
3845
      freopen(err_temp,"a+",stderr);
3156
3846
      (void) my_delete(err_renamed, MYF(0));
3169
3859
    }
3170
3860
    else
3171
3861
     result= 1;
3172
 
    pthread_mutex_unlock(&LOCK_error_log);
 
3862
    VOID(pthread_mutex_unlock(&LOCK_error_log));
3173
3863
  }
3174
3864
   return result;
3175
3865
}
3176
3866
 
3177
 
void DRIZZLE_BIN_LOG::signal_update()
 
3867
void MYSQL_BIN_LOG::signal_update()
3178
3868
{
3179
3869
  pthread_cond_broadcast(&update_cond);
3180
3870
  return;
3205
3895
  struct tm tm_tmp;
3206
3896
  struct tm *start;
3207
3897
 
3208
 
  pthread_mutex_lock(&LOCK_error_log);
 
3898
  VOID(pthread_mutex_lock(&LOCK_error_log));
3209
3899
 
3210
3900
  skr= my_time(0);
3211
3901
  localtime_r(&skr, &tm_tmp);
3224
3914
 
3225
3915
  fflush(stderr);
3226
3916
 
3227
 
  pthread_mutex_unlock(&LOCK_error_log);
 
3917
  VOID(pthread_mutex_unlock(&LOCK_error_log));
3228
3918
  return;
3229
3919
}
3230
3920
 
3330
4020
 
3331
4021
int TC_LOG_MMAP::open(const char *opt_name)
3332
4022
{
3333
 
  uint32_t i;
 
4023
  uint i;
3334
4024
  bool crashed= false;
3335
4025
  PAGE *pg;
3336
4026
 
3358
4048
  {
3359
4049
    inited= 1;
3360
4050
    crashed= true;
3361
 
    sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4051
    sql_print_information("Recovering after a crash using %s", opt_name);
3362
4052
    if (tc_heuristic_recover)
3363
4053
    {
3364
 
      sql_print_error(_("Cannot perform automatic crash recovery when "
3365
 
                      "--tc-heuristic-recover is used"));
 
4054
      sql_print_error("Cannot perform automatic crash recovery when "
 
4055
                      "--tc-heuristic-recover is used");
3366
4056
      goto err;
3367
4057
    }
3368
4058
    file_length= my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
3370
4060
      goto err;
3371
4061
  }
3372
4062
 
3373
 
  data= (unsigned char *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
 
4063
  data= (uchar *)my_mmap(0, (size_t)file_length, PROT_READ|PROT_WRITE,
3374
4064
                        MAP_NOSYNC|MAP_SHARED, fd, 0);
3375
4065
  if (data == MAP_FAILED)
3376
4066
  {
3406
4096
      goto err;
3407
4097
 
3408
4098
  memcpy(data, tc_log_magic, sizeof(tc_log_magic));
3409
 
  data[sizeof(tc_log_magic)]= (unsigned char)total_ha_2pc;
 
4099
  data[sizeof(tc_log_magic)]= (uchar)total_ha_2pc;
3410
4100
  msync(data, tc_log_page_size, MS_SYNC);
3411
4101
  my_sync(fd, MYF(0));
3412
4102
  inited=5;
3514
4204
    threads waiting for a page, but then all these threads will be waiting
3515
4205
    for a fsync() anyway
3516
4206
 
3517
 
   If tc_log == DRIZZLE_LOG then tc_log writes transaction to binlog and
 
4207
   If tc_log == MYSQL_LOG then tc_log writes transaction to binlog and
3518
4208
   records XID in a special Xid_log_event.
3519
4209
   If tc_log = TC_LOG_MMAP then xid is written in a special memory-mapped
3520
4210
   log.
3561
4251
  }
3562
4252
 
3563
4253
  /* found! store xid there and mark the page dirty */
3564
 
  cookie= (ulong)((unsigned char *)p->ptr - data);      // can never be zero
 
4254
  cookie= (ulong)((uchar *)p->ptr - data);      // can never be zero
3565
4255
  *p->ptr++= xid;
3566
4256
  p->free--;
3567
4257
  p->state= DIRTY;
3662
4352
 
3663
4353
void TC_LOG_MMAP::close()
3664
4354
{
3665
 
  uint32_t i;
 
4355
  uint i;
3666
4356
  switch (inited) {
3667
4357
  case 6:
3668
4358
    pthread_mutex_destroy(&LOCK_sync);
3680
4370
      pthread_cond_destroy(&pages[i].cond);
3681
4371
    }
3682
4372
  case 3:
3683
 
    free((unsigned char*)pages);
 
4373
    my_free((uchar*)pages, MYF(0));
3684
4374
  case 2:
3685
4375
    my_munmap((char*)data, (size_t)file_length);
3686
4376
  case 1:
3698
4388
 
3699
4389
  if (memcmp(data, tc_log_magic, sizeof(tc_log_magic)))
3700
4390
  {
3701
 
    sql_print_error(_("Bad magic header in tc log"));
 
4391
    sql_print_error("Bad magic header in tc log");
3702
4392
    goto err1;
3703
4393
  }
3704
4394
 
3708
4398
  */
3709
4399
  if (data[sizeof(tc_log_magic)] != total_ha_2pc)
3710
4400
  {
3711
 
    sql_print_error(_("Recovery failed! You must enable "
 
4401
    sql_print_error("Recovery failed! You must enable "
3712
4402
                    "exactly %d storage engines that support "
3713
 
                    "two-phase commit protocol"),
 
4403
                    "two-phase commit protocol",
3714
4404
                    data[sizeof(tc_log_magic)]);
3715
4405
    goto err1;
3716
4406
  }
3722
4412
  for ( ; p < end_p ; p++)
3723
4413
  {
3724
4414
    for (my_xid *x=p->start; x < p->end; x++)
3725
 
      if (*x && my_hash_insert(&xids, (unsigned char *)x))
 
4415
      if (*x && my_hash_insert(&xids, (uchar *)x))
3726
4416
        goto err2; // OOM
3727
4417
  }
3728
4418
 
3736
4426
err2:
3737
4427
  hash_free(&xids);
3738
4428
err1:
3739
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4429
  sql_print_error("Crash recovery failed. Either correct the problem "
3740
4430
                  "(if it's, for example, out of memory error) and restart, "
3741
 
                  "or delete tc log and start drizzled with "
3742
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4431
                  "or delete tc log and start mysqld with "
 
4432
                  "--tc-heuristic-recover={commit|rollback}");
3743
4433
  return 1;
3744
4434
}
3745
4435
#endif
3766
4456
  if (!tc_heuristic_recover)
3767
4457
    return 0;
3768
4458
 
3769
 
  sql_print_information(_("Heuristic crash recovery mode"));
 
4459
  sql_print_information("Heuristic crash recovery mode");
3770
4460
  if (ha_recover(0))
3771
 
    sql_print_error(_("Heuristic crash recovery failed"));
3772
 
  sql_print_information(_("Please restart mysqld without --tc-heuristic-recover"));
 
4461
    sql_print_error("Heuristic crash recovery failed");
 
4462
  sql_print_information("Please restart mysqld without --tc-heuristic-recover");
3773
4463
  return 1;
3774
4464
}
3775
4465
 
3776
4466
/****** transaction coordinator log for 2pc - binlog() based solution ******/
3777
 
#define TC_LOG_BINLOG DRIZZLE_BIN_LOG
 
4467
#define TC_LOG_BINLOG MYSQL_BIN_LOG
3778
4468
 
3779
4469
/**
3780
4470
  @todo
3810
4500
    return 1;
3811
4501
  }
3812
4502
 
3813
 
  if ((error= find_log_pos(&log_info, NULL, 1)))
 
4503
  if ((error= find_log_pos(&log_info, NullS, 1)))
3814
4504
  {
3815
4505
    if (error != LOG_INFO_EOF)
3816
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4506
      sql_print_error("find_log_pos() failed (error: %d)", error);
3817
4507
    else
3818
4508
      error= 0;
3819
4509
    goto err;
3837
4527
 
3838
4528
    if (error !=  LOG_INFO_EOF)
3839
4529
    {
3840
 
      sql_print_error(_("find_log_pos() failed (error: %d)"), error);
 
4530
      sql_print_error("find_log_pos() failed (error: %d)", error);
3841
4531
      goto err;
3842
4532
    }
3843
4533
 
3851
4541
        ev->get_type_code() == FORMAT_DESCRIPTION_EVENT &&
3852
4542
        ev->flags & LOG_EVENT_BINLOG_IN_USE_F)
3853
4543
    {
3854
 
      sql_print_information(_("Recovering after a crash using %s"), opt_name);
 
4544
      sql_print_information("Recovering after a crash using %s", opt_name);
3855
4545
      error= recover(&log, (Format_description_log_event *)ev);
3856
4546
    }
3857
4547
    else
3930
4620
    if (ev->get_type_code() == XID_EVENT)
3931
4621
    {
3932
4622
      Xid_log_event *xev=(Xid_log_event *)ev;
3933
 
      unsigned char *x= (unsigned char *) memdup_root(&mem_root, (unsigned char*) &xev->xid,
 
4623
      uchar *x= (uchar *) memdup_root(&mem_root, (uchar*) &xev->xid,
3934
4624
                                      sizeof(xev->xid));
3935
4625
      if (! x)
3936
4626
        goto err2;
3950
4640
  free_root(&mem_root, MYF(0));
3951
4641
  hash_free(&xids);
3952
4642
err1:
3953
 
  sql_print_error(_("Crash recovery failed. Either correct the problem "
 
4643
  sql_print_error("Crash recovery failed. Either correct the problem "
3954
4644
                  "(if it's, for example, out of memory error) and restart, "
3955
4645
                  "or delete (or rename) binary log and start mysqld with "
3956
 
                  "--tc-heuristic-recover={commit|rollback}"));
 
4646
                  "--tc-heuristic-recover={commit|rollback}");
3957
4647
  return 1;
3958
4648
}
3959
4649
 
3982
4672
 
3983
4673
mysql_declare_plugin(binlog)
3984
4674
{
3985
 
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
 
4675
  MYSQL_STORAGE_ENGINE_PLUGIN,
3986
4676
  "binlog",
3987
4677
  "1.0",
3988
4678
  "MySQL AB",