|
From: Brian W. <bw...@st...> - 2002-09-12 02:10:21
|
Hi,
I have been playing with my Adjustable Logging patch.
I now have it working for 3.2.0b3, but I am stuck on
the configure step. I have added the following to
configure.in:
AC_MSG_CHECKING(if fcntl can lock files?)
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
struct flock flk;],
[flk.l_type = F_RDLCK | F_WRLCK;
flk.l_start = 0;
flk.l_whence = SEEK_SET | SEEK_END;
flk.l_len = 0;
flk.l_pid = getpid();
fcntl( 7, F_SETLKW | F_SETLK, &flk );],
[AC_MSG_RESULT(yes);AC_DEFINE(__FILE_LOCKING__)],
[AC_MSG_RESULT(no)])
This is producing a correct looking configure script
and it looks like it it is running OK, ( I am getting
the "Yes" and "No" responses as expected ) but I have
no idea how to propogate my AC_DEFINE down to the
C++ code.
Also, I decided to put all the file locking
stuff in a separate C++ class with an interface which
looks like this:
class LockedFile
{
public:
LockedFile();
// Destructor - will close the file if it hasn't
// been closed already
~LockedFile();
// This method opens a file, The path and mode parameters
// are the same as those accepted by the standard fopen
// function. It will return false if it fails to open
// the file, in which case it will set the global
// value errno as per the fopen function
//
// By deafult it will wait for the lock, but it can
// be told to not wait
int Open( const char* path, const char* mode, int waitForLock );
int Open( const char* path, const char* mode );
// This method will close the file if it hasn't been
// closed already
void Close();
// This allows an object of this type to be used
// at any point where a FILE* is expected
operator FILE*();
};
which I have put in htlib/FileLocking.[h|cc], and
I have modified htlib/Makefile.am accordingly.
This means that all the evil mucking about
with #defines is limited to a single place, and the
functionality is available to anyone else who
needs it. If the file locking is not available, it
just calls "fopen".
Sample USage:
#include "FileLocking.h"
LockedFile lkdf;
if ( lkdf.Open("/path/to/file", "w" ) )
{
fprintf( lkdf, "Hello Wordl\n" );
lkdf.Close();
}
Are there any problems with doing this? Are there
any conventions I should be following that I am not?
Regs
Brian
-------------------------
Brian White
Step Two Designs Pty Ltd
Knowledge Management Consultancy, SGML & XML
Phone: +612-93197901
Web: http://www.steptwo.com.au/
Email: bw...@st...
Content Management Requirements Toolkit
112 CMS requirements, ready to cut-and-paste
|