~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/log.h

Removed/replaced DBUG symbols and standardized TRUE/FALSE

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
 
 */
19
 
 
20
 
#ifndef DRIZZLE_SERVER_LOG_H
21
 
#define DRIZZLE_SERVER_LOG_H
22
 
 
23
 
#include <mysys/iocache.h>
 
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 */
 
15
 
 
16
#ifndef LOG_H
 
17
#define LOG_H
24
18
 
25
19
class Relay_log_info;
26
20
 
47
41
{
48
42
public:
49
43
  TC_LOG_DUMMY() {}
50
 
  int open(const char *opt_name __attribute__((unused)))
 
44
  int open(const char *opt_name __attribute__((__unused__)))
51
45
  { return 0; }
52
46
  void close(void)                          { }
53
 
  int log_xid(THD *thd __attribute__((unused)),
54
 
              my_xid xid __attribute__((unused)))         { return 1; }
55
 
  void unlog(ulong cookie __attribute__((unused)),
56
 
             my_xid xid __attribute__((unused)))  { }
 
47
  int log_xid(THD *thd __attribute__((__unused__)),
 
48
              my_xid xid __attribute__((__unused__)))         { return 1; }
 
49
  void unlog(ulong cookie __attribute__((__unused__)),
 
50
             my_xid xid __attribute__((__unused__)))  { }
57
51
};
58
52
 
59
53
#ifdef HAVE_MMAP
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
158
152
/* log event handler flags */
159
153
#define LOG_NONE       1
160
154
#define LOG_FILE       2
 
155
#define LOG_TABLE      4
161
156
 
162
157
class Log_event;
163
158
class Rows_log_event;
170
165
  (mmap+fsync is two times faster than write+fsync)
171
166
*/
172
167
 
173
 
class DRIZZLE_LOG
 
168
class MYSQL_LOG
174
169
{
175
170
public:
176
 
  DRIZZLE_LOG();
 
171
  MYSQL_LOG();
177
172
  void init_pthread_objects();
178
173
  void cleanup();
179
174
  bool open(const char *log_name,
182
177
            enum cache_type io_cache_type_arg);
183
178
  void init(enum_log_type log_type_arg,
184
179
            enum cache_type io_cache_type_arg);
185
 
  void close(uint32_t exiting);
 
180
  void close(uint exiting);
186
181
  inline bool is_open() { return log_state != LOG_CLOSED; }
187
182
  const char *generate_name(const char *log_name, const char *suffix,
188
183
                            bool strip_ext, char *buff);
201
196
  friend class Log_event;
202
197
};
203
198
 
204
 
class DRIZZLE_BIN_LOG: public TC_LOG, private DRIZZLE_LOG
 
199
class MYSQL_QUERY_LOG: public MYSQL_LOG
 
200
{
 
201
public:
 
202
  MYSQL_QUERY_LOG() : last_time(0) {}
 
203
  void reopen_file();
 
204
  bool write(time_t event_time, const char *user_host,
 
205
             uint user_host_len, int thread_id,
 
206
             const char *command_type, uint command_type_len,
 
207
             const char *sql_text, uint sql_text_len);
 
208
  bool write(THD *thd, time_t current_time, time_t query_start_arg,
 
209
             const char *user_host, uint user_host_len,
 
210
             ulonglong query_utime, ulonglong lock_utime, bool is_command,
 
211
             const char *sql_text, uint sql_text_len);
 
212
  bool open_slow_log(const char *log_name)
 
213
  {
 
214
    char buf[FN_REFLEN];
 
215
    return open(generate_name(log_name, "-slow.log", 0, buf), LOG_NORMAL, 0,
 
216
                WRITE_CACHE);
 
217
  }
 
218
  bool open_query_log(const char *log_name)
 
219
  {
 
220
    char buf[FN_REFLEN];
 
221
    return open(generate_name(log_name, ".log", 0, buf), LOG_NORMAL, 0,
 
222
                WRITE_CACHE);
 
223
  }
 
224
 
 
225
private:
 
226
  time_t last_time;
 
227
};
 
228
 
 
229
class MYSQL_BIN_LOG: public TC_LOG, private MYSQL_LOG
205
230
{
206
231
 private:
207
232
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
209
234
  pthread_mutex_t LOCK_prep_xids;
210
235
  pthread_cond_t  COND_prep_xids;
211
236
  pthread_cond_t update_cond;
212
 
  uint64_t bytes_written;
 
237
  ulonglong bytes_written;
213
238
  IO_CACHE index_file;
214
239
  char index_file_name[FN_REFLEN];
215
240
  /*
225
250
  ulong max_size;
226
251
  long prepared_xids; /* for tc log - number of xids to remember */
227
252
  // current file sequence number for load data infile binary logging
228
 
  uint32_t file_id;
229
 
  uint32_t open_count;                          // For replication
 
253
  uint file_id;
 
254
  uint open_count;                              // For replication
230
255
  int readers_count;
231
256
  bool need_start_event;
232
257
  /*
238
263
  */
239
264
  bool no_auto_events;
240
265
 
241
 
  uint64_t m_table_map_version;
 
266
  ulonglong m_table_map_version;
242
267
 
243
268
  int write_to_file(IO_CACHE *cache);
244
269
  /*
250
275
  void new_file_impl(bool need_lock);
251
276
 
252
277
public:
253
 
  DRIZZLE_LOG::generate_name;
254
 
  DRIZZLE_LOG::is_open;
 
278
  MYSQL_LOG::generate_name;
 
279
  MYSQL_LOG::is_open;
255
280
  /*
256
281
    These describe the log's format. This is used only for relay logs.
257
282
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
263
288
  Format_description_log_event *description_event_for_exec,
264
289
    *description_event_for_queue;
265
290
 
266
 
  DRIZZLE_BIN_LOG();
 
291
  MYSQL_BIN_LOG();
267
292
  /*
268
 
    note that there's no destructor ~DRIZZLE_BIN_LOG() !
 
293
    note that there's no destructor ~MYSQL_BIN_LOG() !
269
294
    The reason is that we don't want it to be automatically called
270
295
    on exit() - but only during the correct shutdown process
271
296
  */
275
300
  int log_xid(THD *thd, my_xid xid);
276
301
  void unlog(ulong cookie, my_xid xid);
277
302
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
278
 
  bool is_table_mapped(Table *table) const
 
303
#if !defined(MYSQL_CLIENT)
 
304
  bool is_table_mapped(TABLE *table) const
279
305
  {
280
306
    return table->s->table_map_version == table_map_version();
281
307
  }
282
308
 
283
 
  uint64_t table_map_version() const { return m_table_map_version; }
 
309
  ulonglong table_map_version() const { return m_table_map_version; }
284
310
  void update_table_map_version() { ++m_table_map_version; }
285
311
 
286
312
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event);
287
313
 
 
314
#endif /* !defined(MYSQL_CLIENT) */
288
315
  void reset_bytes_written()
289
316
  {
290
317
    bytes_written = 0;
291
318
  }
292
 
  void harvest_bytes_written(uint64_t* counter)
 
319
  void harvest_bytes_written(ulonglong* counter)
293
320
  {
 
321
    char buf1[22],buf2[22];
294
322
    (*counter)+=bytes_written;
295
323
    bytes_written=0;
296
324
    return;
327
355
    v stands for vector
328
356
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
329
357
  */
330
 
  bool appendv(const char* buf,uint32_t len,...);
 
358
  bool appendv(const char* buf,uint len,...);
331
359
  bool append(Log_event* ev);
332
360
 
333
361
  void make_log_name(char* buf, const char* log_ident);
334
362
  bool is_active(const char* log_file_name);
335
363
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
336
 
  void rotate_and_purge(uint32_t flags);
 
364
  void rotate_and_purge(uint flags);
337
365
  bool flush_and_sync();
338
366
  int purge_logs(const char *to_log, bool included,
339
367
                 bool need_mutex, bool need_update_threads,
340
 
                 uint64_t *decrease_log_space);
 
368
                 ulonglong *decrease_log_space);
341
369
  int purge_logs_before_date(time_t purge_time);
342
370
  int purge_first_log(Relay_log_info* rli, bool included);
343
371
  bool reset_logs(THD* thd);
344
 
  void close(uint32_t exiting);
 
372
  void close(uint exiting);
345
373
 
346
374
  // iterating through the log index file
347
375
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
349
377
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
350
378
  int get_current_log(LOG_INFO* linfo);
351
379
  int raw_get_current_log(LOG_INFO* linfo);
352
 
  uint32_t next_file_id();
 
380
  uint next_file_id();
353
381
  inline char* get_index_fname() { return index_file_name;}
354
382
  inline char* get_log_fname() { return log_file_name; }
355
383
  inline char* get_name() { return name; }
359
387
  inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
360
388
  inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
361
389
  inline IO_CACHE *get_index_file() { return &index_file;}
362
 
  inline uint32_t get_open_count() { return open_count; }
 
390
  inline uint32 get_open_count() { return open_count; }
363
391
};
364
392
 
365
393
class Log_event_handler
369
397
  virtual bool init()= 0;
370
398
  virtual void cleanup()= 0;
371
399
 
 
400
  virtual bool log_slow(THD *thd, time_t current_time,
 
401
                        time_t query_start_arg, const char *user_host,
 
402
                        uint user_host_len, ulonglong query_utime,
 
403
                        ulonglong lock_utime, bool is_command,
 
404
                        const char *sql_text, uint sql_text_len)= 0;
372
405
  virtual bool log_error(enum loglevel level, const char *format,
373
406
                         va_list args)= 0;
 
407
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
408
                           uint user_host_len, int thread_id,
 
409
                           const char *command_type, uint command_type_len,
 
410
                           const char *sql_text, uint sql_text_len,
 
411
                           CHARSET_INFO *client_cs)= 0;
374
412
  virtual ~Log_event_handler() {}
375
413
};
376
414
 
377
415
 
 
416
/* type of the log table */
 
417
#define QUERY_LOG_SLOW 1
 
418
#define QUERY_LOG_GENERAL 2
 
419
 
 
420
class Log_to_file_event_handler: public Log_event_handler
 
421
{
 
422
  MYSQL_QUERY_LOG mysql_log;
 
423
  MYSQL_QUERY_LOG mysql_slow_log;
 
424
  bool is_initialized;
 
425
public:
 
426
  Log_to_file_event_handler(): is_initialized(false)
 
427
  {}
 
428
  virtual bool init();
 
429
  virtual void cleanup();
 
430
 
 
431
  virtual bool log_slow(THD *thd, time_t current_time,
 
432
                        time_t query_start_arg, const char *user_host,
 
433
                        uint user_host_len, ulonglong query_utime,
 
434
                        ulonglong lock_utime, bool is_command,
 
435
                        const char *sql_text, uint sql_text_len);
 
436
  virtual bool log_error(enum loglevel level, const char *format,
 
437
                         va_list args);
 
438
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
439
                           uint user_host_len, int thread_id,
 
440
                           const char *command_type, uint command_type_len,
 
441
                           const char *sql_text, uint sql_text_len,
 
442
                           CHARSET_INFO *client_cs);
 
443
  void flush();
 
444
  void init_pthread_objects();
 
445
  MYSQL_QUERY_LOG *get_mysql_slow_log() { return &mysql_slow_log; }
 
446
  MYSQL_QUERY_LOG *get_mysql_log() { return &mysql_log; }
 
447
};
 
448
 
 
449
 
378
450
/* Class which manages slow, general and error log event handlers */
379
451
class LOGGER
380
452
{
381
453
  rw_lock_t LOCK_logger;
382
454
  /* flag to check whether logger mutex is initialized */
383
 
  uint32_t inited;
 
455
  uint inited;
 
456
 
 
457
  /* available log handlers */
 
458
  Log_to_file_event_handler *file_log_handler;
384
459
 
385
460
  /* NULL-terminated arrays of log handlers */
386
461
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
462
  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
463
  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
387
464
 
388
465
public:
389
466
 
390
 
  LOGGER() : inited(0)
 
467
  LOGGER() : inited(0), file_log_handler(NULL)
391
468
  {}
392
469
  void lock_shared() { rw_rdlock(&LOCK_logger); }
393
470
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
394
471
  void unlock() { rw_unlock(&LOCK_logger); }
 
472
  bool is_log_table_enabled(uint log_table_type);
395
473
  bool log_command(THD *thd, enum enum_server_command command);
396
474
 
397
475
  /*
408
486
  void cleanup_end();
409
487
  bool error_log_print(enum loglevel level, const char *format,
410
488
                      va_list args);
 
489
  bool slow_log_print(THD *thd, const char *query, uint query_length,
 
490
                      ulonglong current_utime);
 
491
  bool general_log_print(THD *thd,enum enum_server_command command,
 
492
                         const char *format, va_list args);
 
493
  bool general_log_write(THD *thd, enum enum_server_command command,
 
494
                         const char *query, uint query_length);
 
495
 
411
496
  /* 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);
 
497
  int set_handlers(uint error_log_printer,
 
498
                   uint slow_log_printer,
 
499
                   uint general_log_printer);
 
500
  void init_error_log(uint error_log_printer);
 
501
  void init_slow_log(uint slow_log_printer);
 
502
  void init_general_log(uint general_log_printer);
 
503
  void deactivate_log_handler(THD* thd, uint log_type);
 
504
  bool activate_log_handler(THD* thd, uint log_type);
 
505
  MYSQL_QUERY_LOG *get_slow_log_file_handler()
 
506
  { 
 
507
    if (file_log_handler)
 
508
      return file_log_handler->get_mysql_slow_log();
 
509
    return NULL;
 
510
  }
 
511
  MYSQL_QUERY_LOG *get_log_file_handler()
 
512
  { 
 
513
    if (file_log_handler)
 
514
      return file_log_handler->get_mysql_log();
 
515
    return NULL;
 
516
  }
414
517
};
415
518
 
416
519
enum enum_binlog_format {
430
533
};
431
534
extern TYPELIB binlog_format_typelib;
432
535
 
433
 
int vprint_msg_to_log(enum loglevel level, const char *format, va_list args);
434
 
void sql_print_error(const char *format, ...) __attribute__((format(printf, 1, 2)));
435
 
void sql_print_warning(const char *format, ...) __attribute__((format(printf, 1, 2)));
436
 
void sql_print_information(const char *format, ...)
437
 
  __attribute__((format(printf, 1, 2)));
438
 
typedef void (*sql_print_message_func)(const char *format, ...)
439
 
  __attribute__((format(printf, 1, 2)));
440
 
extern sql_print_message_func sql_print_message_handlers[];
441
 
 
442
 
int error_log_print(enum loglevel level, const char *format,
443
 
                    va_list args);
444
 
 
445
 
#endif /* DRIZZLE_SERVER_LOG_H */
 
536
#endif /* LOG_H */