log.h 23 KB
Newer Older
Marc Alff's avatar
Marc Alff committed
1
/* Copyright (C) 2005 MySQL AB, 2008-2009 Sun Microsystems, Inc
2 3 4

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef LOG_H
#define LOG_H

19 20 21
#include "unireg.h"                    // REQUIRED: for other includes
#include "handler.h"                            /* my_xid */

22
class Relay_log_info;
23 24 25

class Format_description_log_event;

26 27 28
bool trans_has_updated_trans_table(const THD* thd);
bool stmt_has_updated_trans_table(const THD *thd);
bool use_trans_cache(const THD* thd, bool is_transactional);
29 30 31
bool ending_trans(THD* thd, const bool all);
bool trans_has_updated_non_trans_table(const THD* thd);
bool stmt_has_updated_non_trans_table(const THD* thd);
32

33 34 35 36 37 38 39 40 41 42 43 44 45
/*
  Transaction Coordinator log - a base abstract class
  for two different implementations
*/
class TC_LOG
{
  public:
  int using_heuristic_recover();
  TC_LOG() {}
  virtual ~TC_LOG() {}

  virtual int open(const char *opt_name)=0;
  virtual void close()=0;
unknown's avatar
unknown committed
46
  virtual int log_xid(THD *thd, my_xid xid)=0;
47 48 49 50 51
  virtual void unlog(ulong cookie, my_xid xid)=0;
};

class TC_LOG_DUMMY: public TC_LOG // use it to disable the logging
{
unknown's avatar
unknown committed
52 53
public:
  TC_LOG_DUMMY() {}
54 55
  int open(const char *opt_name)        { return 0; }
  void close()                          { }
unknown's avatar
unknown committed
56
  int log_xid(THD *thd, my_xid xid)         { return 1; }
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  void unlog(ulong cookie, my_xid xid)  { }
};

#ifdef HAVE_MMAP
class TC_LOG_MMAP: public TC_LOG
{
  public:                // only to keep Sun Forte on sol9x86 happy
  typedef enum {
    POOL,                 // page is in pool
    ERROR,                // last sync failed
    DIRTY                 // new xids added since last sync
  } PAGE_STATE;

  private:
  typedef struct st_page {
    struct st_page *next; // page a linked in a fifo queue
    my_xid *start, *end;  // usable area of a page
    my_xid *ptr;          // next xid will be written here
    int size, free;       // max and current number of free xid slots on the page
    int waiters;          // number of waiters on condition
    PAGE_STATE state;     // see above
Marc Alff's avatar
Marc Alff committed
78 79
    mysql_mutex_t lock; // to access page data or control structure
    mysql_cond_t  cond; // to wait for a sync
80 81 82 83 84 85 86 87 88 89 90 91 92 93
  } PAGE;

  char logname[FN_REFLEN];
  File fd;
  my_off_t file_length;
  uint npages, inited;
  uchar *data;
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
  /*
    note that, e.g. LOCK_active is only used to protect
    'active' pointer, to protect the content of the active page
    one has to use active->lock.
    Same for LOCK_pool and LOCK_sync
  */
Marc Alff's avatar
Marc Alff committed
94 95
  mysql_mutex_t LOCK_active, LOCK_pool, LOCK_sync;
  mysql_cond_t COND_pool, COND_active;
96 97 98 99 100

  public:
  TC_LOG_MMAP(): inited(0) {}
  int open(const char *opt_name);
  void close();
unknown's avatar
unknown committed
101
  int log_xid(THD *thd, my_xid xid);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  void unlog(ulong cookie, my_xid xid);
  int recover();

  private:
  void get_active_from_pool();
  int sync();
  int overflow();
};
#else
#define TC_LOG_MMAP TC_LOG_DUMMY
#endif

extern TC_LOG *tc_log;
extern TC_LOG_MMAP tc_log_mmap;
extern TC_LOG_DUMMY tc_log_dummy;

/* log info errors */
#define LOG_INFO_EOF -1
#define LOG_INFO_IO  -2
#define LOG_INFO_INVALID -3
#define LOG_INFO_SEEK -4
#define LOG_INFO_MEM -6
#define LOG_INFO_FATAL -7
#define LOG_INFO_IN_USE -8
126 127
#define LOG_INFO_EMFILE -9

128 129 130 131 132 133

/* bitmap to SQL_LOG::close() */
#define LOG_CLOSE_INDEX		1
#define LOG_CLOSE_TO_BE_OPENED	2
#define LOG_CLOSE_STOP_EVENT	4

134 135 136 137 138 139 140 141 142 143 144 145 146
/* 
  Maximum unique log filename extension.
  Note: setting to 0x7FFFFFFF due to atol windows 
        overflow/truncate.
 */
#define MAX_LOG_UNIQUE_FN_EXT 0x7FFFFFFF

/* 
   Number of warnings that will be printed to error log
   before extension number is exhausted.
*/
#define LOG_WARN_UNIQUE_FN_EXT_LEFT 1000

147
class Relay_log_info;
148

Marc Alff's avatar
Marc Alff committed
149 150 151 152
#ifdef HAVE_PSI_INTERFACE
extern PSI_mutex_key key_LOG_INFO_lock;
#endif

153 154 155 156 157 158
typedef struct st_log_info
{
  char log_file_name[FN_REFLEN];
  my_off_t index_file_offset, index_file_start_offset;
  my_off_t pos;
  bool fatal; // if the purge happens to give us a negative offset
Marc Alff's avatar
Marc Alff committed
159
  mysql_mutex_t lock;
unknown's avatar
unknown committed
160 161 162 163 164
  st_log_info()
    : index_file_offset(0), index_file_start_offset(0),
      pos(0), fatal(0)
    {
      log_file_name[0] = '\0';
Marc Alff's avatar
Marc Alff committed
165
      mysql_mutex_init(key_LOG_INFO_lock, &lock, MY_MUTEX_INIT_FAST);
unknown's avatar
unknown committed
166
    }
Marc Alff's avatar
Marc Alff committed
167
  ~st_log_info() { mysql_mutex_destroy(&lock);}
168 169
} LOG_INFO;

170 171 172 173 174 175
/*
  Currently we have only 3 kinds of logging functions: old-fashioned
  logs, stdout and csv logging routines.
*/
#define MAX_LOG_HANDLERS_NUM 3

176 177 178 179
/* log event handler flags */
#define LOG_NONE       1
#define LOG_FILE       2
#define LOG_TABLE      4
180

181 182 183
class Log_event;
class Rows_log_event;

184 185
enum enum_log_type { LOG_UNKNOWN, LOG_NORMAL, LOG_BIN };
enum enum_log_state { LOG_OPENED, LOG_CLOSED, LOG_TO_BE_OPENED };
186 187 188 189 190 191

/*
  TODO use mmap instead of IO_CACHE for binlog
  (mmap+fsync is two times faster than write+fsync)
*/

192 193 194 195 196 197 198 199 200 201
class MYSQL_LOG
{
public:
  MYSQL_LOG();
  void init_pthread_objects();
  void cleanup();
  bool open(const char *log_name,
            enum_log_type log_type,
            const char *new_name,
            enum cache_type io_cache_type_arg);
202 203 204 205
  bool init_and_set_log_file_name(const char *log_name,
                                  const char *new_name,
                                  enum_log_type log_type_arg,
                                  enum cache_type io_cache_type_arg);
206 207 208
  void init(enum_log_type log_type_arg,
            enum cache_type io_cache_type_arg);
  void close(uint exiting);
209
  inline bool is_open() { return log_state != LOG_CLOSED; }
210 211 212 213 214
  const char *generate_name(const char *log_name, const char *suffix,
                            bool strip_ext, char *buff);
  int generate_new_name(char *new_name, const char *log_name);
 protected:
  /* LOCK_log is inited by init_pthread_objects() */
Marc Alff's avatar
Marc Alff committed
215
  mysql_mutex_t LOCK_log;
216 217
  char *name;
  char log_file_name[FN_REFLEN];
unknown's avatar
unknown committed
218
  char time_buff[20], db[NAME_LEN + 1];
219 220
  bool write_error, inited;
  IO_CACHE log_file;
221 222
  enum_log_type log_type;
  volatile enum_log_state log_state;
223 224 225 226
  enum cache_type io_cache_type;
  friend class Log_event;
};

227
class MYSQL_QUERY_LOG: public MYSQL_LOG
228 229
{
public:
230 231
  MYSQL_QUERY_LOG() : last_time(0) {}
  void reopen_file();
232 233 234 235 236 237
  bool write(time_t event_time, const char *user_host,
             uint user_host_len, int thread_id,
             const char *command_type, uint command_type_len,
             const char *sql_text, uint sql_text_len);
  bool write(THD *thd, time_t current_time, time_t query_start_arg,
             const char *user_host, uint user_host_len,
238
             ulonglong query_utime, ulonglong lock_utime, bool is_command,
239 240 241 242 243 244 245
             const char *sql_text, uint sql_text_len);
  bool open_slow_log(const char *log_name)
  {
    char buf[FN_REFLEN];
    return open(generate_name(log_name, "-slow.log", 0, buf), LOG_NORMAL, 0,
                WRITE_CACHE);
  }
246 247 248 249 250 251
  bool open_query_log(const char *log_name)
  {
    char buf[FN_REFLEN];
    return open(generate_name(log_name, ".log", 0, buf), LOG_NORMAL, 0,
                WRITE_CACHE);
  }
252

253 254
private:
  time_t last_time;
255 256 257
};

class MYSQL_BIN_LOG: public TC_LOG, private MYSQL_LOG
258 259 260
{
 private:
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
Marc Alff's avatar
Marc Alff committed
261 262 263 264
  mysql_mutex_t LOCK_index;
  mysql_mutex_t LOCK_prep_xids;
  mysql_cond_t  COND_prep_xids;
  mysql_cond_t update_cond;
265 266
  ulonglong bytes_written;
  IO_CACHE index_file;
267
  char index_file_name[FN_REFLEN];
268
  /*
269
    purge_file is a temp file used in purge_logs so that the index file
270 271 272 273
    can be updated before deleting files from disk, yielding better crash
    recovery. It is created on demand the first time purge_logs is called
    and then reused for subsequent calls. It is cleaned up in cleanup().
  */
274 275
  IO_CACHE purge_index_file;
  char purge_index_file_name[FN_REFLEN];
276 277 278 279 280 281 282 283 284 285 286
  /*
     The max size before rotation (usable only if log_type == LOG_BIN: binary
     logs and relay logs).
     For a binlog, max_size should be max_binlog_size.
     For a relay log, it should be max_relay_log_size if this is non-zero,
     max_binlog_size otherwise.
     max_size is set in init(), and dynamically changed (when one does SET
     GLOBAL MAX_BINLOG_SIZE|MAX_RELAY_LOG_SIZE) by fix_max_binlog_size and
     fix_max_relay_log_size).
  */
  ulong max_size;
287
  long prepared_xids; /* for tc log - number of xids to remember */
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  // current file sequence number for load data infile binary logging
  uint file_id;
  uint open_count;				// For replication
  int readers_count;
  bool need_start_event;
  /*
    no_auto_events means we don't want any of these automatic events :
    Start/Rotate/Stop. That is, in 4.x when we rotate a relay log, we don't
    want a Rotate_log event to be written to the relay log. When we start a
    relay log etc. So in 4.x this is 1 for relay logs, 0 for binlogs.
    In 5.0 it's 0 for relay logs too!
  */
  bool no_auto_events;

  ulonglong m_table_map_version;

304 305 306 307 308 309 310 311 312 313 314 315
  /* pointer to the sync period variable, for binlog this will be
     sync_binlog_period, for relay log this will be
     sync_relay_log_period
  */
  uint *sync_period_ptr;
  uint sync_counter;

  inline uint get_sync_period()
  {
    return *sync_period_ptr;
  }

316
  int write_to_file(IO_CACHE *cache);
317 318 319 320 321 322 323
  /*
    This is used to start writing to a new log file. The difference from
    new_file() is locking. new_file_without_locking() does not acquire
    LOCK_log.
  */
  void new_file_without_locking();
  void new_file_impl(bool need_lock);
324 325

public:
326
  MYSQL_LOG::generate_name;
327
  MYSQL_LOG::is_open;
328 329 330

  /* This is relay log */
  bool is_relay_log;
Andrei Elkin's avatar
Andrei Elkin committed
331
  ulong signal_cnt;  // update of the counter is checked by heartbeat
332 333 334 335 336 337 338 339 340 341 342
  /*
    These describe the log's format. This is used only for relay logs.
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
    necessary to have 2 distinct objects, because the I/O thread may be reading
    events in a different format from what the SQL thread is reading (consider
    the case of a master which has been upgraded from 5.0 to 5.1 without doing
    RESET MASTER, or from 4.x to 5.0).
  */
  Format_description_log_event *description_event_for_exec,
    *description_event_for_queue;

343
  MYSQL_BIN_LOG(uint *sync_period);
344
  /*
345
    note that there's no destructor ~MYSQL_BIN_LOG() !
346 347 348 349 350 351
    The reason is that we don't want it to be automatically called
    on exit() - but only during the correct shutdown process
  */

  int open(const char *opt_name);
  void close();
unknown's avatar
unknown committed
352
  int log_xid(THD *thd, my_xid xid);
353 354 355 356 357
  void unlog(ulong cookie, my_xid xid);
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
#if !defined(MYSQL_CLIENT)
  bool is_table_mapped(TABLE *table) const
  {
358
    return table->s->table_map_version == table_map_version();
359 360
  }

361 362 363
  ulonglong table_map_version() const { return m_table_map_version; }
  void update_table_map_version() { ++m_table_map_version; }

364 365 366
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event,
                                       bool is_transactional);
  int remove_pending_rows_event(THD *thd, bool is_transactional);
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

#endif /* !defined(MYSQL_CLIENT) */
  void reset_bytes_written()
  {
    bytes_written = 0;
  }
  void harvest_bytes_written(ulonglong* counter)
  {
#ifndef DBUG_OFF
    char buf1[22],buf2[22];
#endif
    DBUG_ENTER("harvest_bytes_written");
    (*counter)+=bytes_written;
    DBUG_PRINT("info",("counter: %s  bytes_written: %s", llstr(*counter,buf1),
		       llstr(bytes_written,buf2)));
    bytes_written=0;
    DBUG_VOID_RETURN;
  }
  void set_max_size(ulong max_size_arg);
  void signal_update();
Andrei Elkin's avatar
Andrei Elkin committed
387 388
  void wait_for_update_relay_log(THD* thd);
  int  wait_for_update_bin_log(THD* thd, const struct timespec * timeout);
389
  void set_need_start_event() { need_start_event = 1; }
390
  void init(bool no_auto_events_arg, ulong max_size);
391 392 393 394 395 396 397
  void init_pthread_objects();
  void cleanup();
  bool open(const char *log_name,
            enum_log_type log_type,
            const char *new_name,
	    enum cache_type io_cache_type_arg,
	    bool no_auto_events_arg, ulong max_size,
398 399
            bool null_created,
            bool need_mutex);
400
  bool open_index_file(const char *index_file_name_arg,
401
                       const char *log_name, bool need_mutex);
402 403
  /* Use this to start writing a new log file */
  void new_file();
404

405
  bool write(Log_event* event_info);
406 407
  bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event, bool incident);
  bool write_incident(THD *thd, bool lock);
408

409
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
410 411
  void set_write_error(THD *thd);
  bool check_write_error(THD *thd);
412

413
  void start_union_events(THD *thd, query_id_t query_id_param);
414 415 416 417 418 419 420 421 422 423 424 425 426 427
  void stop_union_events(THD *thd);
  bool is_query_in_union(THD *thd, query_id_t query_id_param);

  /*
    v stands for vector
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
  */
  bool appendv(const char* buf,uint len,...);
  bool append(Log_event* ev);

  void make_log_name(char* buf, const char* log_ident);
  bool is_active(const char* log_file_name);
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
  void rotate_and_purge(uint flags);
428 429 430 431 432 433 434 435 436 437 438 439 440 441
  /**
     Flush binlog cache and synchronize to disk.

     This function flushes events in binlog cache to binary log file,
     it will do synchronizing according to the setting of system
     variable 'sync_binlog'. If file is synchronized, @c synced will
     be set to 1, otherwise 0.

     @param[out] synced if not NULL, set to 1 if file is synchronized, otherwise 0

     @retval 0 Success
     @retval other Failure
  */
  bool flush_and_sync(bool *synced);
442 443 444 445
  int purge_logs(const char *to_log, bool included,
                 bool need_mutex, bool need_update_threads,
                 ulonglong *decrease_log_space);
  int purge_logs_before_date(time_t purge_time);
446
  int purge_first_log(Relay_log_info* rli, bool included);
447 448 449 450 451 452 453 454 455 456
  int set_purge_index_file_name(const char *base_file_name);
  int open_purge_index_file(bool destroy);
  bool is_inited_purge_index_file();
  int close_purge_index_file();
  int clean_purge_index_file();
  int sync_purge_index_file();
  int register_purge_index_entry(const char* entry);
  int register_create_index_entry(const char* entry);
  int purge_index_entry(THD *thd, ulonglong *decrease_log_space,
                        bool need_mutex);
457 458 459 460 461 462 463 464
  bool reset_logs(THD* thd);
  void close(uint exiting);

  // iterating through the log index file
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
		   bool need_mutex);
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
  int get_current_log(LOG_INFO* linfo);
unknown's avatar
unknown committed
465
  int raw_get_current_log(LOG_INFO* linfo);
466 467 468 469
  uint next_file_id();
  inline char* get_index_fname() { return index_file_name;}
  inline char* get_log_fname() { return log_file_name; }
  inline char* get_name() { return name; }
Marc Alff's avatar
Marc Alff committed
470
  inline mysql_mutex_t* get_log_lock() { return &LOCK_log; }
471 472
  inline IO_CACHE* get_log_file() { return &log_file; }

Marc Alff's avatar
Marc Alff committed
473 474
  inline void lock_index() { mysql_mutex_lock(&LOCK_index);}
  inline void unlock_index() { mysql_mutex_unlock(&LOCK_index);}
475 476 477 478
  inline IO_CACHE *get_index_file() { return &index_file;}
  inline uint32 get_open_count() { return open_count; }
};

479 480 481
class Log_event_handler
{
public:
unknown's avatar
unknown committed
482
  Log_event_handler() {}
483 484 485 486 487
  virtual bool init()= 0;
  virtual void cleanup()= 0;

  virtual bool log_slow(THD *thd, time_t current_time,
                        time_t query_start_arg, const char *user_host,
488 489
                        uint user_host_len, ulonglong query_utime,
                        ulonglong lock_utime, bool is_command,
490 491 492
                        const char *sql_text, uint sql_text_len)= 0;
  virtual bool log_error(enum loglevel level, const char *format,
                         va_list args)= 0;
493
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
494 495
                           uint user_host_len, int thread_id,
                           const char *command_type, uint command_type_len,
496 497
                           const char *sql_text, uint sql_text_len,
                           CHARSET_INFO *client_cs)= 0;
498 499 500 501
  virtual ~Log_event_handler() {}
};


502 503 504
int check_if_log_table(uint db_len, const char *db, uint table_name_len,
                       const char *table_name, uint check_if_opened);

505 506 507 508 509 510 511 512 513 514 515 516
class Log_to_csv_event_handler: public Log_event_handler
{
  friend class LOGGER;

public:
  Log_to_csv_event_handler();
  ~Log_to_csv_event_handler();
  virtual bool init();
  virtual void cleanup();

  virtual bool log_slow(THD *thd, time_t current_time,
                        time_t query_start_arg, const char *user_host,
517 518
                        uint user_host_len, ulonglong query_utime,
                        ulonglong lock_utime, bool is_command,
519 520 521
                        const char *sql_text, uint sql_text_len);
  virtual bool log_error(enum loglevel level, const char *format,
                         va_list args);
522
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
523 524
                           uint user_host_len, int thread_id,
                           const char *command_type, uint command_type_len,
525
                           const char *sql_text, uint sql_text_len,
526 527 528
                           CHARSET_INFO *client_cs);

  int activate_log(THD *thd, uint log_type);
529 530 531
};


532 533 534 535
/* type of the log table */
#define QUERY_LOG_SLOW 1
#define QUERY_LOG_GENERAL 2

536 537
class Log_to_file_event_handler: public Log_event_handler
{
538 539
  MYSQL_QUERY_LOG mysql_log;
  MYSQL_QUERY_LOG mysql_slow_log;
540 541 542 543 544 545 546 547 548
  bool is_initialized;
public:
  Log_to_file_event_handler(): is_initialized(FALSE)
  {}
  virtual bool init();
  virtual void cleanup();

  virtual bool log_slow(THD *thd, time_t current_time,
                        time_t query_start_arg, const char *user_host,
549 550
                        uint user_host_len, ulonglong query_utime,
                        ulonglong lock_utime, bool is_command,
551 552 553
                        const char *sql_text, uint sql_text_len);
  virtual bool log_error(enum loglevel level, const char *format,
                         va_list args);
554
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
555 556
                           uint user_host_len, int thread_id,
                           const char *command_type, uint command_type_len,
557 558
                           const char *sql_text, uint sql_text_len,
                           CHARSET_INFO *client_cs);
559 560
  void flush();
  void init_pthread_objects();
unknown's avatar
unknown committed
561 562
  MYSQL_QUERY_LOG *get_mysql_slow_log() { return &mysql_slow_log; }
  MYSQL_QUERY_LOG *get_mysql_log() { return &mysql_log; }
563 564 565 566 567 568
};


/* Class which manages slow, general and error log event handlers */
class LOGGER
{
Marc Alff's avatar
Marc Alff committed
569
  mysql_rwlock_t LOCK_logger;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
  /* flag to check whether logger mutex is initialized */
  uint inited;

  /* available log handlers */
  Log_to_csv_event_handler *table_log_handler;
  Log_to_file_event_handler *file_log_handler;

  /* NULL-terminated arrays of log handlers */
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];

public:

  bool is_log_tables_initialized;

  LOGGER() : inited(0), table_log_handler(NULL),
             file_log_handler(NULL), is_log_tables_initialized(FALSE)
  {}
Marc Alff's avatar
Marc Alff committed
589 590 591
  void lock_shared() { mysql_rwlock_rdlock(&LOCK_logger); }
  void lock_exclusive() { mysql_rwlock_wrlock(&LOCK_logger); }
  void unlock() { mysql_rwlock_unlock(&LOCK_logger); }
592
  bool is_log_table_enabled(uint log_table_type);
593 594
  bool log_command(THD *thd, enum enum_server_command command);

595 596 597 598 599 600 601 602 603
  /*
    We want to initialize all log mutexes as soon as possible,
    but we cannot do it in constructor, as safe_mutex relies on
    initialization, performed by MY_INIT(). This why this is done in
    this function.
  */
  void init_base();
  void init_log_tables();
  bool flush_logs(THD *thd);
604 605
  bool flush_slow_log();
  bool flush_general_log();
unknown's avatar
unknown committed
606 607 608 609
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
  void cleanup_base();
  /* Free memory. Nothing could be logged after this function is called */
  void cleanup_end();
610 611 612
  bool error_log_print(enum loglevel level, const char *format,
                      va_list args);
  bool slow_log_print(THD *thd, const char *query, uint query_length,
613
                      ulonglong current_utime);
614 615
  bool general_log_print(THD *thd,enum enum_server_command command,
                         const char *format, va_list args);
616 617
  bool general_log_write(THD *thd, enum enum_server_command command,
                         const char *query, uint query_length);
618 619

  /* we use this function to setup all enabled log event handlers */
620 621 622 623 624 625
  int set_handlers(uint error_log_printer,
                   uint slow_log_printer,
                   uint general_log_printer);
  void init_error_log(uint error_log_printer);
  void init_slow_log(uint slow_log_printer);
  void init_general_log(uint general_log_printer);
626 627
  void deactivate_log_handler(THD* thd, uint log_type);
  bool activate_log_handler(THD* thd, uint log_type);
628
  MYSQL_QUERY_LOG *get_slow_log_file_handler() const
629 630 631 632 633
  { 
    if (file_log_handler)
      return file_log_handler->get_mysql_slow_log();
    return NULL;
  }
634
  MYSQL_QUERY_LOG *get_log_file_handler() const
635 636 637 638 639 640
  { 
    if (file_log_handler)
      return file_log_handler->get_mysql_log();
    return NULL;
  }
};
641 642

enum enum_binlog_format {
643 644 645 646
  BINLOG_FORMAT_MIXED= 0, ///< statement if safe, otherwise row - autodetected
  BINLOG_FORMAT_STMT=  1, ///< statement-based
  BINLOG_FORMAT_ROW=   2, ///< row-based
  BINLOG_FORMAT_UNSPEC=3  ///< thd_binlog_format() returns it when binlog is closed
647 648 649
};
extern TYPELIB binlog_format_typelib;

650
int query_error_code(THD *thd, bool not_killed);
651
uint purge_log_get_error_code(int res);
652

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
int vprint_msg_to_log(enum loglevel level, const char *format, va_list args);
void sql_print_error(const char *format, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
void sql_print_warning(const char *format, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
void sql_print_information(const char *format, ...)
  ATTRIBUTE_FORMAT(printf, 1, 2);
typedef void (*sql_print_message_func)(const char *format, ...)
  ATTRIBUTE_FORMAT(printf, 1, 2);
extern sql_print_message_func sql_print_message_handlers[];

int error_log_print(enum loglevel level, const char *format,
                    va_list args);

bool slow_log_print(THD *thd, const char *query, uint query_length,
                    ulonglong current_utime);

bool general_log_print(THD *thd, enum enum_server_command command,
                       const char *format,...);

bool general_log_write(THD *thd, enum enum_server_command command,
                       const char *query, uint query_length);

void sql_perror(const char *message);
bool flush_error_log();

File open_binlog(IO_CACHE *log, const char *log_file_name,
                 const char **errmsg);

char *make_log_name(char *buff, const char *name, const char* log_ext);

extern MYSQL_PLUGIN_IMPORT MYSQL_BIN_LOG mysql_bin_log;
extern LOGGER logger;

685
#endif /* LOG_H */