2005-03-18 21:41:13 -05:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-04-22 20:09:46 -04:00
|
|
|
IN: streams
|
|
|
|
USING: kernel namespaces stdio strings unparser ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-03-18 21:41:13 -05:00
|
|
|
! A simple logging framework.
|
2005-04-22 20:09:46 -04:00
|
|
|
SYMBOL: log-stream
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: log ( msg -- )
|
2005-03-18 21:41:13 -05:00
|
|
|
#! Log a message to the log stream, either stdio or a file.
|
2005-04-22 20:09:46 -04:00
|
|
|
log-stream get dup [
|
2005-03-18 21:41:13 -05:00
|
|
|
tuck stream-print stream-flush
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-22 20:09:46 -04:00
|
|
|
: with-logging ( file quot -- )
|
2005-03-18 21:41:13 -05:00
|
|
|
#! Calls to log inside quot will output to a file.
|
2005-04-22 20:09:46 -04:00
|
|
|
[ swap <file-writer> log-stream set call ] with-scope ;
|
2005-03-18 21:41:13 -05:00
|
|
|
|
|
|
|
! Helpful words.
|
|
|
|
|
|
|
|
: log-error ( error -- ) "Error: " swap cat2 log ;
|
|
|
|
|
|
|
|
: log-client ( client-stream -- )
|
2005-04-22 20:09:46 -04:00
|
|
|
[
|
|
|
|
"Accepted connection from " %
|
|
|
|
( dup ) client-stream-host %
|
|
|
|
! CHAR: : ,
|
|
|
|
! client-stream-port unparse %
|
|
|
|
] make-string log ;
|