source: branches/home/psmith/restructure/src/nio-logger/nio-logger.lisp

Last change on this file was 109, checked in by psmith, 18 years ago

Remove +process-jobs-inline+ as can't work like this. Added timeout mechanism

File size: 3.1 KB
Line 
1#|
2Copyright (c) 2007
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
133. The name of the author may not be used to endorse or promote products
14   derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26|#
27
28(in-package :nio-logger)
29
30(declaim (optimize (debug 3) (speed 3) (space 0)))
31
32
33
34(defmacro with-line-from-tailed-file ((line filename delay) &rest body)
35  `(with-open-file (in ,filename :direction :input)
36     (loop for ,line = (read-line in nil nil) do
37          (if ,line
38              (progn ,@body)
39              (progn
40;               (format t "read nil~%")
41                (sleep ,delay))))))
42
43
44(defun callback(result)
45#+nio-debug  (nio-utils:format-log t "Result of remote-log ~A~%" result)
46)
47
48
49;;Tail the given log and write to remote logger
50;;e.g. (tail-log "/var/log/httpd/access_log" "192.168.1.1")
51(defun tail-log(filename ip-address)
52  (sleep 4)
53  (nio:add-connection (nio:node ip-address 16323))
54  (with-line-from-tailed-file (text filename 1)
55    (let ((rpc (format nil "(nio-logger:remote-log \"~A\")" (cl-base64:string-to-base64-string text))))
56#+nio-debug      (nio-utils:format-log t "nio-logger::tail-log Submitting job~A~%" rpc)
57      (nio:with-connected-nodes (node)
58#+nio-debug     (nio-utils:format-log t "Toplevel sending ~A to ~A~%" rpc node)
59        (nio-yarpc:remote-execute (nio:active-conn node) rpc #'callback)))))
60
61;Runs a multithreaded system with an IO thread dealing with IO only and a 'job'  thread taking and executing jobs
62
63(defparameter +log-file-name+ "./out")
64
65(defun run-logging-server(listen-ip out-file &optional (allowed-ips "ips.txt"))
66  (setf  +log-file-name+ out-file)
67  (nio:load-ips allowed-ips)
68  (sb-thread:make-thread #'(lambda()(nio:start-server 'nio-yarpc:yarpc-state-machine :host listen-ip :port 16323 :accept-connection 'nio:check-ip)) :name "nio-server")
69  (loop
70    ;;block waiting for jobs
71     (nio-yarpc:run-job)))
72
73(nio-yarpc:defremote remote-log(base64-str)
74  (with-open-file (out +log-file-name+ :direction :output :if-exists :append)
75    (nio-utils:format-log out "~A~%" (cl-base64:base64-string-to-string base64-str)))
76  t)
77
Note: See TracBrowser for help on using the repository browser.