Bug fixes; docs
parent
578d620223
commit
30eba00774
|
@ -1,13 +1,44 @@
|
|||
USING: help.markup help.syntax ;
|
||||
IN: io.encodings
|
||||
|
||||
ABOUT: "encodings"
|
||||
|
||||
ARTICLE: "encodings" "I/O encodings"
|
||||
"Many streams deal with bytes, rather than Unicode code points, at some level. The translation between these two things is specified by an encoding. To abstract this away from the programmer, Factor provides a system where these streams are associated with an encoding which is always used when the stream is read from or written to. For most purposes, an encoding descriptor consisting of a symbol is all that is needed when initializing a stream."
|
||||
"To make an encoded stream directly (something which is normally handled by the appropriate stream constructor), use the following words:"
|
||||
{ $subsection "encodings-constructors" }
|
||||
{ $subsection "encodings-descriptors" }
|
||||
{ $subsection "encodings-string" }
|
||||
{ $subsection "encodings-protocol" } ;
|
||||
|
||||
ARTICLE: "encodings-constructors" "Constructing an encoded stream"
|
||||
{ $subsection <encoder> }
|
||||
{ $subsection <decoder> }
|
||||
{ $subsection <encoder-duplex> }
|
||||
"To encode or decode a string, use"
|
||||
{ $subsection encode-string }
|
||||
! { $subsection decode-string }
|
||||
;
|
||||
{ $subsection <encoder-duplex> } ;
|
||||
|
||||
HELP: <encoder> ( stream encoding -- newstream )
|
||||
{ $values { "stream" "an output stream" }
|
||||
{ "encoding" "an encoding descriptor" }
|
||||
{ "newstream" "an encoded output stream" } }
|
||||
{ $description "Wraps the given stream in a new stream using the given encoding for all output. The encoding descriptor can either be a class or an instance of something conforming to the " { $link "encodings-protocol" } "." } ;
|
||||
|
||||
HELP: <decoder> ( stream encoding -- newstream )
|
||||
{ $values { "stream" "an input stream" }
|
||||
{ "encoding" "an encoding descriptor" }
|
||||
{ "newstream" "an encoded output stream" } }
|
||||
{ $description "Wraps the given stream in a new stream using the given encoding for all input. The encoding descriptor can either be a class or an instance of something conforming to the " { $link "encodings-protocol" } "." } ;
|
||||
|
||||
HELP: <encoder-duplex> ( stream-in stream-out encoding -- duplex )
|
||||
{ $values { "stream-in" "an input stream" }
|
||||
{ "stream-out" "an output stream" }
|
||||
{ "encoding" "an encoding descriptor" }
|
||||
{ "duplex" "an encoded duplex stream" } }
|
||||
{ $description "Wraps the given streams in an encoder or decoder stream, and puts them together in a duplex stream for input and output. If either input stream is already encoded, that encoding is stripped off before it is reencoded. The encoding descriptor must conform to the " { $link "encodings-protocol" } "." } ;
|
||||
|
||||
{ <encoder> <decoder> <encoder-duplex> } related-words
|
||||
|
||||
ARTICLE: "encodings-descriptors" "Encoding descriptors"
|
||||
"An encoding descriptor is something which can be used for input or output streams to encode or decode files. It must conform to the " { $link "encodings-protocol" } ". Encodings which you can use include:"
|
||||
{ $vocab-link "io.encodings.utf8" }
|
||||
{ $vocab-link "io.encodings.ascii" }
|
||||
{ $vocab-link "io.encodings.binary" }
|
||||
{ $vocab-link "io.encodings.utf16" } ;
|
||||
|
|
|
@ -54,7 +54,7 @@ GENERIC: decode-step ( buf byte ch state encoding -- buf ch state )
|
|||
decode-read-loop ;
|
||||
|
||||
TUPLE: decoded code cr ;
|
||||
: <decoder> ( stream decoding-class -- decoded-stream )
|
||||
: <decoder> ( stream encoding -- newstream )
|
||||
dup binary eq? [ drop ] [
|
||||
construct-empty { set-delegate set-decoded-code }
|
||||
decoded construct
|
||||
|
@ -126,7 +126,7 @@ TUPLE: encode-error ;
|
|||
: encode-error ( -- * ) \ encode-error construct-empty throw ;
|
||||
|
||||
TUPLE: encoded code ;
|
||||
: <encoder> ( stream encoding-class -- encoded-stream )
|
||||
: <encoder> ( stream encoding -- newstream )
|
||||
dup binary eq? [ drop ] [
|
||||
construct-empty { set-delegate set-encoded-code }
|
||||
encoded construct
|
||||
|
@ -153,7 +153,7 @@ INSTANCE: encoded plain-writer
|
|||
: redecode ( stream encoding -- newstream )
|
||||
over decoded? [ >r delegate r> ] when <decoder> ;
|
||||
|
||||
: <encoder-duplex> ( stream-in stream-out encoding -- duplex-stream )
|
||||
: <encoder-duplex> ( stream-in stream-out encoding -- duplex )
|
||||
tuck reencode >r redecode r> <duplex-stream> ;
|
||||
|
||||
! The null encoding does nothing
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
! Copyright (C) 2005, 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: io.backend io.nonblocking io.unix.backend io.files io
|
||||
unix unix.stat unix.time kernel math continuations math.bitfields
|
||||
byte-arrays alien combinators combinators.cleave calendar ;
|
||||
unix unix.stat unix.time kernel math continuations math.bitfields
|
||||
byte-arrays alien combinators combinators.cleave calendar
|
||||
io.encodings.binary ;
|
||||
|
||||
IN: io.unix.files
|
||||
|
||||
|
@ -60,8 +61,8 @@ M: unix-io delete-directory ( path -- )
|
|||
|
||||
: (copy-file) ( from to -- )
|
||||
dup parent-directory make-directories
|
||||
<file-writer> [
|
||||
swap <file-reader> [
|
||||
binary <file-writer> [
|
||||
swap binary <file-reader> [
|
||||
swap stream-copy
|
||||
] with-disposal
|
||||
] with-disposal ;
|
||||
|
|
|
@ -1,116 +1,10 @@
|
|||
<<<<<<< HEAD:extra/logging/server/server.factor
|
||||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: namespaces kernel io calendar sequences io.files
|
||||
io.sockets continuations prettyprint assocs math.parser
|
||||
words debugger math combinators concurrency.messaging
|
||||
threads arrays init math.ranges strings io.encodings.utf8 ;
|
||||
IN: logging.server
|
||||
|
||||
: log-root ( -- string )
|
||||
\ log-root get "logs" resource-path or ;
|
||||
|
||||
: log-path ( service -- path )
|
||||
log-root swap path+ ;
|
||||
|
||||
: log# ( path n -- path' )
|
||||
number>string ".log" append path+ ;
|
||||
|
||||
SYMBOL: log-files
|
||||
|
||||
: open-log-stream ( service -- stream )
|
||||
log-path
|
||||
dup make-directories
|
||||
1 log# utf8 <file-appender> ;
|
||||
|
||||
: log-stream ( service -- stream )
|
||||
log-files get [ open-log-stream ] cache ;
|
||||
|
||||
: multiline-header 20 CHAR: - <string> ; foldable
|
||||
|
||||
: (write-message) ( msg word-name level multi? -- )
|
||||
[
|
||||
"[" write multiline-header write "] " write
|
||||
] [
|
||||
"[" write now (timestamp>rfc3339) "] " write
|
||||
] if
|
||||
write bl write ": " write print ;
|
||||
|
||||
: write-message ( msg word-name level -- )
|
||||
rot [ empty? not ] subset {
|
||||
{ [ dup empty? ] [ 3drop ] }
|
||||
{ [ dup length 1 = ] [ first -rot f (write-message) ] }
|
||||
{ [ t ] [
|
||||
[ first -rot f (write-message) ] 3keep
|
||||
1 tail -rot [ t (write-message) ] 2curry each
|
||||
] }
|
||||
} cond ;
|
||||
|
||||
: (log-message) ( msg -- )
|
||||
#! msg: { msg word-name level service }
|
||||
first4 log-stream [ write-message flush ] with-stream* ;
|
||||
|
||||
: try-dispose ( stream -- )
|
||||
[ dispose ] curry [ error. ] recover ;
|
||||
|
||||
: close-log ( service -- )
|
||||
log-files get delete-at*
|
||||
[ try-dispose ] [ drop ] if ;
|
||||
|
||||
: (close-logs) ( -- )
|
||||
log-files get
|
||||
dup values [ try-dispose ] each
|
||||
clear-assoc ;
|
||||
|
||||
: keep-logs 10 ;
|
||||
|
||||
: ?delete-file ( path -- )
|
||||
dup exists? [ delete-file ] [ drop ] if ;
|
||||
|
||||
: delete-oldest keep-logs log# ?delete-file ;
|
||||
|
||||
: ?rename-file ( old new -- )
|
||||
over exists? [ rename-file ] [ 2drop ] if ;
|
||||
|
||||
: advance-log ( path n -- )
|
||||
[ 1- log# ] 2keep log# ?rename-file ;
|
||||
|
||||
: rotate-log ( service -- )
|
||||
dup close-log
|
||||
log-path
|
||||
dup delete-oldest
|
||||
keep-logs 1 [a,b] [ advance-log ] with each ;
|
||||
|
||||
: (rotate-logs) ( -- )
|
||||
(close-logs)
|
||||
log-root directory [ drop rotate-log ] assoc-each ;
|
||||
|
||||
: log-server-loop ( -- )
|
||||
receive unclip {
|
||||
{ "log-message" [ (log-message) ] }
|
||||
{ "rotate-logs" [ drop (rotate-logs) ] }
|
||||
{ "close-logs" [ drop (close-logs) ] }
|
||||
} case log-server-loop ;
|
||||
|
||||
: log-server ( -- )
|
||||
[ [ log-server-loop ] [ error. (close-logs) ] recover t ]
|
||||
"Log server" spawn-server
|
||||
"log-server" set-global ;
|
||||
|
||||
[
|
||||
H{ } clone log-files set-global
|
||||
log-server
|
||||
] "logging" add-init-hook
|
||||
|
||||
USE: multiline
|
||||
! Need to resolve this merge conflict
|
||||
<"
|
||||
! Copyright (C) 2008 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: namespaces kernel io calendar sequences io.files
|
||||
io.sockets continuations prettyprint assocs math.parser
|
||||
words debugger math combinators concurrency.messaging
|
||||
threads arrays init math.ranges strings calendar.format ;
|
||||
threads arrays init math.ranges strings calendar.format
|
||||
io.encodings.ascii ;
|
||||
IN: logging.server
|
||||
|
||||
: log-root ( -- string )
|
||||
|
@ -127,7 +21,7 @@ SYMBOL: log-files
|
|||
: open-log-stream ( service -- stream )
|
||||
log-path
|
||||
dup make-directories
|
||||
1 log# <file-appender> ;
|
||||
1 log# ascii <file-appender> ;
|
||||
|
||||
: log-stream ( service -- stream )
|
||||
log-files get [ open-log-stream ] cache ;
|
||||
|
@ -207,4 +101,3 @@ SYMBOL: log-files
|
|||
H{ } clone log-files set-global
|
||||
log-server
|
||||
] "logging" add-init-hook
|
||||
"> drop
|
||||
|
|
Loading…
Reference in New Issue