(write-file-append, (write-log-file or (open-locked for Logs

Started by CaveGuy, February 16, 2003, 01:34:27 PM

Previous topic - Next topic

CaveGuy

I often find myself with multiple newlisps running at the

same time shareing a common log or audit file.



The following mutterings may shed some insite on what

improvments I would like to see :)



(write-log file-name "log string" delay) would be very nice :)



It would wait for file if locked, lock and open it or create it,

append "log string", close and unlock it.



If the wait exceded 'delay concider it lockedup and force it ?



Here we are back at the same old file locking problem.



Currentily (open "file.tmp" "a") returns nil if

"File.tmp" is not present, it would be nice if

open "append" created the file if not presient,

like the "write" function does.



something like this would work if we had an

open-exclusive :)



(define (IT:printlog logname printlogstr , fh timeout)

   (setq loop 10  ; these should be input parameters

           delay 50)    ; now I look at ithis closer.

   (while (and (> loop 0)

                   (not (setq fh (open-exclusive logname "w"))))

          (sleep delay)

          (dec 'timeout))  

   (if (> timeout 0)

      (begin

         (seek fh -1) ; open-exclusive "append" would eliminate this.

         (write-line printlogstr  fh)

         (close fh) ) )  )



Currentily I am using something like this:



(define (IT:printlog logname printlogstr timeout delay , fh timeout)

   (setq loop timeout

           locked (append logname ".lock"))

   # this gets around the open append failing on a new file.

   (if (not (and (file? logname) (file? locked)))

      (write-file logname (append now "n"))

   # wait for exsiting lock to release

   (while (and (> loop 0) (file? locked))

      (sleep delay)

      (dec 'loop))

   # rename file if lock released on its own

   # or kill it off for taking too long

   (if (> loop 0)

     (rename-file logname locked)

     (begin

        (delete-file locked)

        (rename-file logname locked)))

    (setq fh (open logname "a"))))

    (write-line printstr  fh)

    (close fh)

    (rename locked logname)

    ) ; end define  



That is a lot of application level overhead just to

append a transaction to file that is moving in time.



How about:

(open file "exclusive" lock-list)

or

(write-file-append file data lock-list)

or

(open file "exclusive" lock-list)



(setq lock-list '(loop-count loop-delay force-lock-flag))



The force-lock-flag could be used to override a hung

lock.



Enough BS Ideas for now, back to shoveling snow......
Bob the Caveguy aka Lord High Fixer.