home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / log.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  5KB  |  157 lines

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16.  
  17. /* Logging of isamcommands and records on logfile */
  18.  
  19. #include "isamdef.h"
  20. #if defined(MSDOS) || defined(__WIN__)
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #ifndef __WIN__
  24. #include <process.h>
  25. #endif
  26. #endif
  27. #ifdef VMS
  28. #include <processes.h>
  29. #endif
  30.  
  31. #ifdef THREAD
  32. #undef GETPID
  33. #define GETPID() (log_type == 1 ? getpid() : (long) my_thread_id());
  34. #else
  35. #define GETPID() getpid()
  36. #endif
  37.  
  38.     /* Activate logging if flag is 1 and reset logging if flag is 0 */
  39.  
  40. static int log_type=0;
  41.  
  42. int nisam_log(int activate_log)
  43. {
  44.   int error=0;
  45.   char buff[FN_REFLEN];
  46.   DBUG_ENTER("nisam_log");
  47.  
  48.   log_type=activate_log;
  49.   if (activate_log)
  50.   {
  51.     if (nisam_log_file < 0)
  52.     {
  53.       if ((nisam_log_file = my_create(fn_format(buff,nisam_log_filename,
  54.                         "",".log",4),
  55.                       0,(O_RDWR | O_BINARY | O_APPEND),MYF(0)))
  56.       < 0)
  57.     DBUG_RETURN(1);
  58.     }
  59.   }
  60.   else if (nisam_log_file >= 0)
  61.   {
  62.     error=my_close(nisam_log_file,MYF(0));
  63.     nisam_log_file= -1;
  64.   }
  65.   DBUG_RETURN(error);
  66. }
  67.  
  68.  
  69.     /* Logging of records and commands on logfile */
  70.     /* All logs starts with command(1) dfile(2) process(4) result(2) */
  71.  
  72. void _nisam_log(enum nisam_log_commands command, N_INFO *info, const byte *buffert, uint length)
  73. {
  74.   char buff[11];
  75.   int error,old_errno;
  76.   ulong pid=(ulong) GETPID();
  77.   old_errno=my_errno;
  78.   bzero(buff,sizeof(buff));
  79.   buff[0]=(char) command;
  80.   int2store(buff+1,info->dfile);
  81.   int4store(buff+3,pid);
  82.   int2store(buff+9,length);
  83.  
  84.   pthread_mutex_lock(&THR_LOCK_isam);
  85.   error=my_lock(nisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  86.   VOID(my_write(nisam_log_file,buff,sizeof(buff),MYF(0)));
  87.   VOID(my_write(nisam_log_file,buffert,length,MYF(0)));
  88.   if (!error)
  89.     error=my_lock(nisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  90.   pthread_mutex_unlock(&THR_LOCK_isam);
  91.   my_errno=old_errno;
  92. }
  93.  
  94.  
  95. void _nisam_log_command(enum nisam_log_commands command, N_INFO *info, const byte *buffert, uint length, int result)
  96. {
  97.   char buff[9];
  98.   int error,old_errno;
  99.   ulong pid=(ulong) GETPID();
  100.  
  101.   old_errno=my_errno;
  102.   buff[0]=(char) command;
  103.   int2store(buff+1,info->dfile);
  104.   int4store(buff+3,pid);
  105.   int2store(buff+7,result);
  106.   pthread_mutex_lock(&THR_LOCK_isam);
  107.   error=my_lock(nisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  108.   VOID(my_write(nisam_log_file,buff,sizeof(buff),MYF(0)));
  109.   if (buffert)
  110.     VOID(my_write(nisam_log_file,buffert,length,MYF(0)));
  111.   if (!error)
  112.     error=my_lock(nisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  113.   pthread_mutex_unlock(&THR_LOCK_isam);
  114.   my_errno=old_errno;
  115. }
  116.  
  117.  
  118. void _nisam_log_record(enum nisam_log_commands command, N_INFO *info, const byte *record, ulong filepos, int result)
  119. {
  120.   char buff[17],*pos;
  121.   int error,old_errno;
  122.   uint length;
  123.   ulong pid=(ulong) GETPID();
  124.  
  125.   old_errno=my_errno;
  126.   if (!info->s->base.blobs)
  127.     length=info->s->base.reclength;
  128.   else
  129.     length=info->s->base.reclength+ _calc_total_blob_length(info,record);
  130.   buff[0]=(char) command;
  131.   int2store(buff+1,info->dfile);
  132.   int4store(buff+3,pid);
  133.   int2store(buff+7,result);
  134.   int4store(buff+9,filepos);
  135.   int4store(buff+13,length);
  136.   pthread_mutex_lock(&THR_LOCK_isam);
  137.   error=my_lock(nisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  138.   VOID(my_write(nisam_log_file,buff,sizeof(buff),MYF(0)));
  139.   VOID(my_write(nisam_log_file,(byte*) record,info->s->base.reclength,MYF(0)));
  140.   if (info->s->base.blobs)
  141.   {
  142.     N_BLOB *blob,*end;
  143.  
  144.     for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
  145.      blob != end ;
  146.      blob++)
  147.     {
  148.       bmove(&pos,record+blob->offset+blob->pack_length,sizeof(char*));
  149.       VOID(my_write(nisam_log_file,pos,blob->length,MYF(0)));
  150.     }
  151.   }
  152.   if (!error)
  153.     error=my_lock(nisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE));
  154.   pthread_mutex_unlock(&THR_LOCK_isam);
  155.   my_errno=old_errno;
  156. }
  157.