Move logging code to io.logging

db4
Slava Pestov 2008-02-07 01:02:26 -06:00
parent 78abc143d6
commit 831b712f84
6 changed files with 77 additions and 62 deletions

View File

@ -0,0 +1 @@
Slava Pestov

View File

@ -0,0 +1,26 @@
IN: io.logging
USING: help.markup help.syntax io ;
HELP: log-stream
{ $var-description "Holds an output stream for logging messages." }
{ $see-also log-error log-client with-logging } ;
HELP: log-message
{ $values { "str" "a string" } }
{ $description "Logs a message to the log stream. If " { $link log-stream } " is not set, logs to the " { $link stdio } " stream." }
{ $see-also log-error log-client } ;
HELP: log-error
{ $values { "str" "a string" } }
{ $description "Logs an error message." }
{ $see-also log-message log-client } ;
HELP: log-client
{ $values { "client" "a client socket stream" } }
{ $description "Logs an incoming client connection." }
{ $see-also log-message log-error } ;
HELP: with-logging
{ $values { "service" "a string or " { $link f } } { "quot" "a quotation" } }
{ $description "Calls the quotation in a new dynamic scope where the " { $link log-stream } " is set to a file stream appending to a log file (if " { $snippet "service" } " is not " { $link f } ") or the " { $link stdio } " stream at the time " { $link with-logging } " is called (if " { $snippet "service" } " is " { $link f } ")." } ;

View File

@ -0,0 +1,47 @@
! Copyright (C) 2003, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: namespaces kernel io calendar sequences io.files
io.sockets continuations prettyprint ;
IN: io.logging
SYMBOL: log-stream
: to-log-stream ( quot -- )
log-stream get swap with-stream* ; inline
: log-message ( str -- )
[
"[" write now timestamp>string write "] " write
print flush
] to-log-stream ;
: log-error ( str -- ) "Error: " swap append log-message ;
: log-client ( client -- )
"Accepted connection from "
swap client-stream-addr unparse append log-message ;
: log-file ( service -- path )
".log" append resource-path ;
: with-log-stream ( stream quot -- )
log-stream get [ nip call ] [
log-stream swap with-variable
] if ; inline
: with-log-file ( file quot -- )
>r <file-appender> r>
[ with-log-stream ] curry
with-disposal ; inline
: with-log-stdio ( quot -- )
stdio get swap with-log-stream ; inline
: with-logging ( service quot -- )
over [
>r log-file
"Writing log messages to " write dup print flush r>
with-log-file
] [
nip with-log-stdio
] if ; inline

View File

@ -0,0 +1 @@
Basic logging framework for server applications

View File

@ -1,29 +1,6 @@
USING: help help.syntax help.markup io ;
IN: io.server
HELP: log-stream
{ $var-description "Holds an output stream for logging messages." }
{ $see-also log-error log-client with-logging } ;
HELP: log-message
{ $values { "str" "a string" } }
{ $description "Logs a message to the log stream. If " { $link log-stream } " is not set, logs to the " { $link stdio } " stream." }
{ $see-also log-error log-client } ;
HELP: log-error
{ $values { "str" "a string" } }
{ $description "Logs an error message." }
{ $see-also log-message log-client } ;
HELP: log-client
{ $values { "client" "a client socket stream" } }
{ $description "Logs an incoming client connection." }
{ $see-also log-message log-error } ;
HELP: with-logging
{ $values { "service" "a string or " { $link f } } { "quot" "a quotation" } }
{ $description "Calls the quotation in a new dynamic scope where the " { $link log-stream } " is set to a file stream appending to a log file (if " { $snippet "service" } " is not " { $link f } ") or the " { $link stdio } " stream at the time " { $link with-logging } " is called (if " { $snippet "service" } " is " { $link f } ")." } ;
HELP: with-client
{ $values { "quot" "a quotation" } { "client" "a client socket stream" } }
{ $description "Logs a client connection and spawns a new thread that calls the quotation, with the " { $link stdio } " stream set to the client stream. If the quotation throws an error, the client connection is closed, and the error is printed to the " { $link stdio } " stream at the time the thread was spawned." } ;

View File

@ -1,49 +1,12 @@
! Copyright (C) 2003, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: io io.sockets io.files continuations kernel math
math.parser namespaces parser sequences strings
USING: io io.sockets io.files io.logging continuations kernel
math math.parser namespaces parser sequences strings
prettyprint debugger quotations calendar qualified ;
QUALIFIED: concurrency
IN: io.server
SYMBOL: log-stream
: with-log-stream ( quot -- )
log-stream get swap with-stream* ; inline
: log-message ( str -- )
[
"[" write now timestamp>string write "] " write
print flush
] with-log-stream ;
: log-error ( str -- ) "Error: " swap append log-message ;
: log-client ( client -- )
"Accepted connection from "
swap client-stream-addr unparse append log-message ;
: log-file ( service -- path )
".log" append resource-path ;
: with-log-file ( file quot -- )
>r <file-appender> r>
[ log-stream swap with-variable ] curry
with-disposal ; inline
: with-log-stdio ( quot -- )
stdio get log-stream rot with-variable ; inline
: with-logging ( service quot -- )
over [
>r log-file
"Writing log messages to " write dup print flush r>
with-log-file
] [
nip with-log-stdio
] if ; inline
: with-client ( quot client -- )
dup log-client
[ swap with-stream ] 2curry concurrency:spawn drop ; inline