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-06-19 17:50:35 -04:00
|
|
|
IN: io
|
2005-08-31 21:06:13 -04:00
|
|
|
USING: io kernel math namespaces parser sequences strings ;
|
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
|
|
|
|
2005-08-31 21:06:13 -04:00
|
|
|
: log-message ( msg -- )
|
2005-03-18 21:41:13 -05:00
|
|
|
#! Log a message to the log stream, either stdio or a file.
|
2005-08-31 21:06:13 -04:00
|
|
|
log-stream get [ stdio get ] unless*
|
|
|
|
[ stream-print ] keep stream-flush ;
|
2005-03-18 21:41:13 -05:00
|
|
|
|
2005-08-31 21:06:13 -04:00
|
|
|
: log-error ( error -- ) "Error: " swap append log-message ;
|
2005-03-18 21:41:13 -05:00
|
|
|
|
|
|
|
: log-client ( client-stream -- )
|
2005-04-22 20:09:46 -04:00
|
|
|
[
|
|
|
|
"Accepted connection from " %
|
2005-04-24 23:02:19 -04:00
|
|
|
dup client-stream-host %
|
|
|
|
CHAR: : ,
|
2005-08-31 21:06:13 -04:00
|
|
|
client-stream-port #
|
|
|
|
] "" make log-message ;
|
2005-04-24 23:02:19 -04:00
|
|
|
|
|
|
|
: with-log-file ( file quot -- )
|
|
|
|
#! Calls to log inside quot will output to a file.
|
|
|
|
[ swap <file-writer> log-stream set call ] with-scope ;
|
|
|
|
|
|
|
|
: with-logging ( quot -- )
|
|
|
|
#! Calls to log inside quot will output to stdio.
|
|
|
|
[ stdio get log-stream set call ] with-scope ;
|