mi_log.c 4.67 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2001, 2004 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
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.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   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.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13 14 15
   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 */

unknown's avatar
unknown committed
16 17 18 19
/*
  Logging of MyISAM commands and records on logfile for debugging
  The log can be examined with help of the myisamlog command.
*/
unknown's avatar
unknown committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include "myisamdef.h"
#if defined(MSDOS) || defined(__WIN__)
#include <fcntl.h>
#ifndef __WIN__
#include <process.h>
#endif
#endif
#ifdef VMS
#include <processes.h>
#endif

#undef GETPID					/* For HPUX */
#ifdef THREAD
unknown's avatar
unknown committed
34
#define GETPID() (log_type == 1 ? (long) myisam_pid : (long) my_thread_dbug_id())
unknown's avatar
unknown committed
35
#else
unknown's avatar
unknown committed
36
#define GETPID() myisam_pid
unknown's avatar
unknown committed
37 38 39 40 41
#endif

	/* Activate logging if flag is 1 and reset logging if flag is 0 */

static int log_type=0;
unknown's avatar
unknown committed
42
ulong myisam_pid=0;
unknown's avatar
unknown committed
43 44 45 46 47 48 49 50 51 52

int mi_log(int activate_log)
{
  int error=0;
  char buff[FN_REFLEN];
  DBUG_ENTER("mi_log");

  log_type=activate_log;
  if (activate_log)
  {
unknown's avatar
unknown committed
53 54
    if (!myisam_pid)
      myisam_pid=(ulong) getpid();
unknown's avatar
unknown committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    if (myisam_log_file < 0)
    {
      if ((myisam_log_file = my_create(fn_format(buff,myisam_log_filename,
						"",".log",4),
				      0,(O_RDWR | O_BINARY | O_APPEND),MYF(0)))
	  < 0)
	DBUG_RETURN(my_errno);
    }
  }
  else if (myisam_log_file >= 0)
  {
    error=my_close(myisam_log_file,MYF(0)) ? my_errno : 0 ;
    myisam_log_file= -1;
  }
  DBUG_RETURN(error);
}


	/* Logging of records and commands on logfile */
	/* All logs starts with command(1) dfile(2) process(4) result(2) */

76
void _myisam_log(enum myisam_log_commands command, MI_INFO *info,
77
		 const uchar *buffert, uint length)
unknown's avatar
unknown committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
  char buff[11];
  int error,old_errno;
  ulong pid=(ulong) GETPID();
  old_errno=my_errno;
  bzero(buff,sizeof(buff));
  buff[0]=(char) command;
  mi_int2store(buff+1,info->dfile);
  mi_int4store(buff+3,pid);
  mi_int2store(buff+9,length);

  pthread_mutex_lock(&THR_LOCK_myisam);
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
  VOID(my_write(myisam_log_file,buffert,length,MYF(0)));
  if (!error)
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  pthread_mutex_unlock(&THR_LOCK_myisam);
  my_errno=old_errno;
}


void _myisam_log_command(enum myisam_log_commands command, MI_INFO *info,
101
			 const uchar *buffert, uint length, int result)
unknown's avatar
unknown committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
  char buff[9];
  int error,old_errno;
  ulong pid=(ulong) GETPID();

  old_errno=my_errno;
  buff[0]=(char) command;
  mi_int2store(buff+1,info->dfile);
  mi_int4store(buff+3,pid);
  mi_int2store(buff+7,result);
  pthread_mutex_lock(&THR_LOCK_myisam);
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
  if (buffert)
    VOID(my_write(myisam_log_file,buffert,length,MYF(0)));
  if (!error)
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  pthread_mutex_unlock(&THR_LOCK_myisam);
  my_errno=old_errno;
}


void _myisam_log_record(enum myisam_log_commands command, MI_INFO *info,
125
			const uchar *record, my_off_t filepos, int result)
unknown's avatar
unknown committed
126 127 128 129 130 131 132 133 134 135
{
  char buff[21],*pos;
  int error,old_errno;
  uint length;
  ulong pid=(ulong) GETPID();

  old_errno=my_errno;
  if (!info->s->base.blobs)
    length=info->s->base.reclength;
  else
136
    length=info->s->base.reclength+ _mi_calc_total_blob_length(info,record);
unknown's avatar
unknown committed
137 138 139 140 141 142 143 144 145
  buff[0]=(char) command;
  mi_int2store(buff+1,info->dfile);
  mi_int4store(buff+3,pid);
  mi_int2store(buff+7,result);
  mi_sizestore(buff+9,filepos);
  mi_int4store(buff+17,length);
  pthread_mutex_lock(&THR_LOCK_myisam);
  error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  VOID(my_write(myisam_log_file,buff,sizeof(buff),MYF(0)));
146
  VOID(my_write(myisam_log_file,(uchar*) record,info->s->base.reclength,MYF(0)));
unknown's avatar
unknown committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  if (info->s->base.blobs)
  {
    MI_BLOB *blob,*end;

    for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
	 blob != end ;
	 blob++)
    {
      memcpy_fixed(&pos,record+blob->offset+blob->pack_length,sizeof(char*));
      VOID(my_write(myisam_log_file,pos,blob->length,MYF(0)));
    }
  }
  if (!error)
    error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  pthread_mutex_unlock(&THR_LOCK_myisam);
  my_errno=old_errno;
}