]> gitweb.factorcode.org Git - factor.git/blob - extra/syslog/syslog.factor
factor: trim using lists
[factor.git] / extra / syslog / syslog.factor
1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors calendar calendar.format io io.encodings.utf8
5 io.sockets io.streams.byte-array literals math math.parser
6 namespaces sequences ;
7
8 IN: syslog
9
10 ! RFC 3164 (http://www.faqs.org/rfcs/rfc3164.html)
11
12 ! The first part is called the PRI, the second part is the
13 ! HEADER, and the third part is the MSG.  The total length of
14 ! the packet MUST be 1024 bytes or less.  There is no minimum
15 ! length of the syslog message although sending a syslog packet
16 ! with no contents is worthless and SHOULD NOT be transmitted.
17
18 CONSTANT: EMERGENCY  0 ! system is unusable
19 CONSTANT: ALERT      1 ! action must be taken immediately
20 CONSTANT: CRITICAL   2 ! critical conditions
21 CONSTANT: ERROR      3 ! error conditions
22 CONSTANT: WARNING    4 ! warning conditions
23 CONSTANT: NOTICE     5 ! normal but significant condition
24 CONSTANT: INFO       6 ! informational
25 CONSTANT: DEBUG      7 ! debug-level messages
26
27 CONSTANT: LOCAL0    16
28 CONSTANT: LOCAL1    17
29 CONSTANT: LOCAL2    18
30 CONSTANT: LOCAL3    19
31 CONSTANT: LOCAL4    20
32 CONSTANT: LOCAL5    21
33 CONSTANT: LOCAL6    22
34 CONSTANT: LOCAL7    23
35
36 SYMBOL: syslog-facility
37 LOCAL0 syslog-facility set-global
38
39 SYMBOL: syslog-server
40 "127.0.0.1" 514 <inet4> syslog-server set-global
41
42 <PRIVATE
43
44 : dd ( timestamp -- )
45     day>> number>string 2 CHAR: \s pad-head write ;
46
47 : write-priority ( level -- )
48     "<" write
49     syslog-facility get-global 8 * + number>string write
50     ">" write ;
51
52 : write-mdhms ( timestamp -- )
53     { MONTH " " dd " " hh ":" mm ":" ss } formatted ;
54
55 : write-timestamp ( -- )
56     now write-mdhms " " write ;
57
58 : write-hostname ( -- )
59     host-name write " " write ;
60
61 : write-syslog ( message level -- )
62     write-priority write-timestamp write-hostname write nl ;
63
64 PRIVATE>
65
66 : syslog ( message level -- )
67     utf8 [ write-syslog ] with-byte-writer
68     1024 short head
69     syslog-server get-global
70     $[ f 0 <inet4> <datagram> ]
71     send ;
72
73 : log-debug ( message -- ) DEBUG syslog ;
74
75 : log-info ( message -- ) INFO syslog ;
76
77 : log-warning ( message -- ) WARNING syslog ;
78
79 : log-error ( message -- ) ERROR syslog ;
80
81 : log-critical ( message -- ) CRITICAL syslog ;
82
83 : log-alert ( message -- ) ALERT syslog ;
84
85
86 ! Must contain only visible (printing) characters
87 ! "<PRI>TIMESTAMP HOSTNAME"
88
89 ! Example:
90 ! <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8
91
92 ! relays should handle cases:
93 ! 1) Valid PRI and TIMESTAMP
94 ! 2) Valid PRI but no TIMESTAMP or invalid TIMESTAMP
95 ! 3) No PRI or unidentifiable PRI