factor/library/io/logging.factor

31 lines
918 B
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: io
USING: io kernel math namespaces parser sequences strings ;
2004-07-16 02:26:21 -04:00
! A simple logging framework.
SYMBOL: log-stream
2004-07-16 02:26:21 -04:00
: log-message ( msg -- )
#! Log a message to the log stream, either stdio or a file.
log-stream get [ stdio get ] unless*
[ stream-print ] keep stream-flush ;
: log-error ( error -- ) "Error: " swap append log-message ;
: log-client ( client-stream -- )
[
"Accepted connection from " %
2005-04-24 23:02:19 -04:00
dup client-stream-host %
CHAR: : ,
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 ;