~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log.h

  • Committer: Brian Aker
  • Date: 2008-09-05 22:16:26 UTC
  • mto: This revision was merged to the branch mainline in revision 383.
  • Revision ID: brian@tangent.org-20080905221626-nc631ypag04am60c
Big, fat, UTF-8 patch. This fixes some of the oddities around only one
charset.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* Copyright (C) 2005 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
15
 
20
16
#ifndef DRIZZLE_SERVER_LOG_H
21
17
#define DRIZZLE_SERVER_LOG_H
22
18
 
23
 
#include <mysys/iocache.h>
24
 
 
25
19
class Relay_log_info;
26
20
 
27
21
class Format_description_log_event;
39
33
 
40
34
  virtual int open(const char *opt_name)=0;
41
35
  virtual void close()=0;
42
 
  virtual int log_xid(Session *session, my_xid xid)=0;
 
36
  virtual int log_xid(THD *thd, my_xid xid)=0;
43
37
  virtual void unlog(ulong cookie, my_xid xid)=0;
44
38
};
45
39
 
50
44
  int open(const char *opt_name __attribute__((unused)))
51
45
  { return 0; }
52
46
  void close(void)                          { }
53
 
  int log_xid(Session *session __attribute__((unused)),
 
47
  int log_xid(THD *thd __attribute__((unused)),
54
48
              my_xid xid __attribute__((unused)))         { return 1; }
55
49
  void unlog(ulong cookie __attribute__((unused)),
56
50
             my_xid xid __attribute__((unused)))  { }
81
75
  char logname[FN_REFLEN];
82
76
  File fd;
83
77
  my_off_t file_length;
84
 
  uint32_t npages, inited;
85
 
  unsigned char *data;
 
78
  uint npages, inited;
 
79
  uchar *data;
86
80
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
87
81
  /*
88
82
    note that, e.g. LOCK_active is only used to protect
97
91
  TC_LOG_MMAP(): inited(0) {}
98
92
  int open(const char *opt_name);
99
93
  void close();
100
 
  int log_xid(Session *session, my_xid xid);
 
94
  int log_xid(THD *thd, my_xid xid);
101
95
  void unlog(ulong cookie, my_xid xid);
102
96
  int recover();
103
97
 
182
176
            enum cache_type io_cache_type_arg);
183
177
  void init(enum_log_type log_type_arg,
184
178
            enum cache_type io_cache_type_arg);
185
 
  void close(uint32_t exiting);
 
179
  void close(uint exiting);
186
180
  inline bool is_open() { return log_state != LOG_CLOSED; }
187
181
  const char *generate_name(const char *log_name, const char *suffix,
188
182
                            bool strip_ext, char *buff);
201
195
  friend class Log_event;
202
196
};
203
197
 
 
198
class DRIZZLE_QUERY_LOG: public DRIZZLE_LOG
 
199
{
 
200
public:
 
201
  DRIZZLE_QUERY_LOG() : last_time(0) {}
 
202
  void reopen_file();
 
203
  bool write(time_t event_time, const char *user_host,
 
204
             uint user_host_len, int thread_id,
 
205
             const char *command_type, uint command_type_len,
 
206
             const char *sql_text, uint sql_text_len);
 
207
  bool write(THD *thd, time_t current_time, time_t query_start_arg,
 
208
             const char *user_host, uint user_host_len,
 
209
             uint64_t query_utime, uint64_t lock_utime, bool is_command,
 
210
             const char *sql_text, uint sql_text_len);
 
211
  bool open_slow_log(const char *log_name)
 
212
  {
 
213
    char buf[FN_REFLEN];
 
214
    return open(generate_name(log_name, "-slow.log", 0, buf), LOG_NORMAL, 0,
 
215
                WRITE_CACHE);
 
216
  }
 
217
  bool open_query_log(const char *log_name)
 
218
  {
 
219
    char buf[FN_REFLEN];
 
220
    return open(generate_name(log_name, ".log", 0, buf), LOG_NORMAL, 0,
 
221
                WRITE_CACHE);
 
222
  }
 
223
 
 
224
private:
 
225
  time_t last_time;
 
226
};
 
227
 
204
228
class DRIZZLE_BIN_LOG: public TC_LOG, private DRIZZLE_LOG
205
229
{
206
230
 private:
225
249
  ulong max_size;
226
250
  long prepared_xids; /* for tc log - number of xids to remember */
227
251
  // current file sequence number for load data infile binary logging
228
 
  uint32_t file_id;
229
 
  uint32_t open_count;                          // For replication
 
252
  uint file_id;
 
253
  uint open_count;                              // For replication
230
254
  int readers_count;
231
255
  bool need_start_event;
232
256
  /*
272
296
 
273
297
  int open(const char *opt_name);
274
298
  void close();
275
 
  int log_xid(Session *session, my_xid xid);
 
299
  int log_xid(THD *thd, my_xid xid);
276
300
  void unlog(ulong cookie, my_xid xid);
277
301
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
 
302
#if !defined(DRIZZLE_CLIENT)
278
303
  bool is_table_mapped(Table *table) const
279
304
  {
280
305
    return table->s->table_map_version == table_map_version();
283
308
  uint64_t table_map_version() const { return m_table_map_version; }
284
309
  void update_table_map_version() { ++m_table_map_version; }
285
310
 
286
 
  int flush_and_set_pending_rows_event(Session *session, Rows_log_event* event);
 
311
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event);
287
312
 
 
313
#endif /* !defined(DRIZZLE_CLIENT) */
288
314
  void reset_bytes_written()
289
315
  {
290
316
    bytes_written = 0;
297
323
  }
298
324
  void set_max_size(ulong max_size_arg);
299
325
  void signal_update();
300
 
  void wait_for_update_relay_log(Session* session);
301
 
  int  wait_for_update_bin_log(Session* session, const struct timespec * timeout);
 
326
  void wait_for_update_relay_log(THD* thd);
 
327
  int  wait_for_update_bin_log(THD* thd, const struct timespec * timeout);
302
328
  void set_need_start_event() { need_start_event = 1; }
303
329
  void init(bool no_auto_events_arg, ulong max_size);
304
330
  void init_pthread_objects();
315
341
  void new_file();
316
342
 
317
343
  bool write(Log_event* event_info); // binary log write
318
 
  bool write(Session *session, IO_CACHE *cache, Log_event *commit_event);
 
344
  bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
319
345
 
320
346
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
321
347
 
322
 
  void start_union_events(Session *session, query_id_t query_id_param);
323
 
  void stop_union_events(Session *session);
324
 
  bool is_query_in_union(Session *session, query_id_t query_id_param);
 
348
  void start_union_events(THD *thd, query_id_t query_id_param);
 
349
  void stop_union_events(THD *thd);
 
350
  bool is_query_in_union(THD *thd, query_id_t query_id_param);
325
351
 
326
352
  /*
327
353
    v stands for vector
328
354
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
329
355
  */
330
 
  bool appendv(const char* buf,uint32_t len,...);
 
356
  bool appendv(const char* buf,uint len,...);
331
357
  bool append(Log_event* ev);
332
358
 
333
359
  void make_log_name(char* buf, const char* log_ident);
334
360
  bool is_active(const char* log_file_name);
335
361
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
336
 
  void rotate_and_purge(uint32_t flags);
 
362
  void rotate_and_purge(uint flags);
337
363
  bool flush_and_sync();
338
364
  int purge_logs(const char *to_log, bool included,
339
365
                 bool need_mutex, bool need_update_threads,
340
366
                 uint64_t *decrease_log_space);
341
367
  int purge_logs_before_date(time_t purge_time);
342
368
  int purge_first_log(Relay_log_info* rli, bool included);
343
 
  bool reset_logs(Session* session);
344
 
  void close(uint32_t exiting);
 
369
  bool reset_logs(THD* thd);
 
370
  void close(uint exiting);
345
371
 
346
372
  // iterating through the log index file
347
373
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
349
375
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
350
376
  int get_current_log(LOG_INFO* linfo);
351
377
  int raw_get_current_log(LOG_INFO* linfo);
352
 
  uint32_t next_file_id();
 
378
  uint next_file_id();
353
379
  inline char* get_index_fname() { return index_file_name;}
354
380
  inline char* get_log_fname() { return log_file_name; }
355
381
  inline char* get_name() { return name; }
369
395
  virtual bool init()= 0;
370
396
  virtual void cleanup()= 0;
371
397
 
 
398
  virtual bool log_slow(THD *thd, time_t current_time,
 
399
                        time_t query_start_arg, const char *user_host,
 
400
                        uint user_host_len, uint64_t query_utime,
 
401
                        uint64_t lock_utime, bool is_command,
 
402
                        const char *sql_text, uint sql_text_len)= 0;
372
403
  virtual bool log_error(enum loglevel level, const char *format,
373
404
                         va_list args)= 0;
 
405
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
406
                           uint user_host_len, int thread_id,
 
407
                           const char *command_type, uint command_type_len,
 
408
                           const char *sql_text, uint sql_text_len,
 
409
                           const CHARSET_INFO * const client_cs)= 0;
374
410
  virtual ~Log_event_handler() {}
375
411
};
376
412
 
377
413
 
 
414
/* type of the log table */
 
415
#define QUERY_LOG_SLOW 1
 
416
#define QUERY_LOG_GENERAL 2
 
417
 
 
418
class Log_to_file_event_handler: public Log_event_handler
 
419
{
 
420
  DRIZZLE_QUERY_LOG mysql_log;
 
421
  DRIZZLE_QUERY_LOG mysql_slow_log;
 
422
  bool is_initialized;
 
423
public:
 
424
  Log_to_file_event_handler(): is_initialized(false)
 
425
  {}
 
426
  virtual bool init();
 
427
  virtual void cleanup();
 
428
 
 
429
  virtual bool log_slow(THD *thd, time_t current_time,
 
430
                        time_t query_start_arg, const char *user_host,
 
431
                        uint user_host_len, uint64_t query_utime,
 
432
                        uint64_t lock_utime, bool is_command,
 
433
                        const char *sql_text, uint sql_text_len);
 
434
  virtual bool log_error(enum loglevel level, const char *format,
 
435
                         va_list args);
 
436
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
437
                           uint user_host_len, int thread_id,
 
438
                           const char *command_type, uint command_type_len,
 
439
                           const char *sql_text, uint sql_text_len,
 
440
                           const CHARSET_INFO * const client_cs);
 
441
  void flush();
 
442
  void init_pthread_objects();
 
443
  DRIZZLE_QUERY_LOG *get_mysql_slow_log() { return &mysql_slow_log; }
 
444
  DRIZZLE_QUERY_LOG *get_mysql_log() { return &mysql_log; }
 
445
};
 
446
 
 
447
 
378
448
/* Class which manages slow, general and error log event handlers */
379
449
class LOGGER
380
450
{
381
451
  rw_lock_t LOCK_logger;
382
452
  /* flag to check whether logger mutex is initialized */
383
 
  uint32_t inited;
 
453
  uint inited;
 
454
 
 
455
  /* available log handlers */
 
456
  Log_to_file_event_handler *file_log_handler;
384
457
 
385
458
  /* NULL-terminated arrays of log handlers */
386
459
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
460
  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
461
  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
387
462
 
388
463
public:
389
464
 
390
 
  LOGGER() : inited(0)
 
465
  LOGGER() : inited(0), file_log_handler(NULL)
391
466
  {}
392
467
  void lock_shared() { rw_rdlock(&LOCK_logger); }
393
468
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
394
469
  void unlock() { rw_unlock(&LOCK_logger); }
395
 
  bool log_command(Session *session, enum enum_server_command command);
 
470
  bool log_command(THD *thd, enum enum_server_command command);
396
471
 
397
472
  /*
398
473
    We want to initialize all log mutexes as soon as possible,
401
476
    this function.
402
477
  */
403
478
  void init_base();
404
 
  bool flush_logs(Session *session);
 
479
  bool flush_logs(THD *thd);
405
480
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
406
481
  void cleanup_base();
407
482
  /* Free memory. Nothing could be logged after this function is called */
408
483
  void cleanup_end();
409
484
  bool error_log_print(enum loglevel level, const char *format,
410
485
                      va_list args);
 
486
  bool slow_log_print(THD *thd, const char *query, uint query_length,
 
487
                      uint64_t current_utime);
 
488
  bool general_log_print(THD *thd,enum enum_server_command command,
 
489
                         const char *format, va_list args);
 
490
  bool general_log_write(THD *thd, enum enum_server_command command,
 
491
                         const char *query, uint query_length);
 
492
 
411
493
  /* we use this function to setup all enabled log event handlers */
412
 
  int set_handlers(uint32_t error_log_printer);
413
 
  void init_error_log(uint32_t error_log_printer);
 
494
  int set_handlers(uint error_log_printer,
 
495
                   uint slow_log_printer,
 
496
                   uint general_log_printer);
 
497
  void init_error_log(uint error_log_printer);
 
498
  void init_slow_log(uint slow_log_printer);
 
499
  void init_general_log(uint general_log_printer);
 
500
  void deactivate_log_handler(THD* thd, uint log_type);
 
501
  bool activate_log_handler(THD* thd, uint log_type);
 
502
  DRIZZLE_QUERY_LOG *get_slow_log_file_handler()
 
503
  { 
 
504
    if (file_log_handler)
 
505
      return file_log_handler->get_mysql_slow_log();
 
506
    return NULL;
 
507
  }
 
508
  DRIZZLE_QUERY_LOG *get_log_file_handler()
 
509
  { 
 
510
    if (file_log_handler)
 
511
      return file_log_handler->get_mysql_log();
 
512
    return NULL;
 
513
  }
414
514
};
415
515
 
416
516
enum enum_binlog_format {
442
542
int error_log_print(enum loglevel level, const char *format,
443
543
                    va_list args);
444
544
 
445
 
void sql_perror(const char *message);
446
 
 
 
545
bool slow_log_print(THD *thd, const char *query, uint query_length,
 
546
                    uint64_t current_utime);
 
547
 
 
548
bool general_log_print(THD *thd, enum enum_server_command command,
 
549
                       const char *format,...);
 
550
 
 
551
bool general_log_write(THD *thd, enum enum_server_command command,
 
552
                       const char *query, uint query_length);
447
553
 
448
554
#endif /* DRIZZLE_SERVER_LOG_H */