jlogger  Hex Artifact Content

Artifact b7706830acd9bced70c939b97f08f9b3f0c30a5d35427578e3c9d9bb2e3ae8cb:

  • File logging/log.lua — part of check-in [ad75102062] at 2021-10-21 10:47:36 on branch rewrite — Reorganizing and rewriting

    The starts of a redesign that hopefully will be better for using with a
    configuration file mechanism, as well as being more flexible and extensible. (user: llmII size: 1328)


0000: 28 75 73 65 20 66 75 67 75 65 29 0a 28 75 73 65  (use fugue).(use
0010: 20 6c 6f 67 67 69 6e 67 2f 73 69 6e 6b 73 29 0a   logging/sinks).
0020: 28 75 73 65 20 6c 6f 67 67 69 6e 67 2f 62 75 66  (use logging/buf
0030: 66 65 72 29 0a 0a 23 20 6c 6f 67 20 23 23 23 23  fer)..# log ####
0040: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0050: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0060: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0070: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0080: 23 23 23 23 0a 23 20 41 20 6c 6f 67 20 69 73 20  ####.# A log is 
0090: 61 20 73 69 6e 6b 2c 20 73 75 63 68 20 74 68 61  a sink, such tha
00a0: 74 20 61 73 20 74 68 65 79 20 72 65 71 75 69 72  t as they requir
00b0: 65 20 61 20 73 69 6e 6b 20 73 6f 20 61 73 20 74  e a sink so as t
00c0: 6f 20 64 72 61 69 6e 20 69 6e 70 75 74 20 69 6e  o drain input in
00d0: 74 6f 2c 0a 23 20 74 68 65 79 20 6d 61 79 20 61  to,.# they may a
00e0: 6c 73 6f 20 62 65 20 75 73 65 64 20 61 73 20 74  lso be used as t
00f0: 68 65 20 73 69 6e 6b 20 6f 66 20 61 20 61 6e 6f  he sink of a ano
0100: 74 68 65 72 20 6c 6f 67 2c 20 66 6f 72 20 63 6f  ther log, for co
0110: 6d 70 6f 73 69 74 69 6f 6e 0a 23 20 70 75 72 70  mposition.# purp
0120: 6f 73 65 73 2e 0a 28 64 65 66 70 72 6f 74 6f 20  oses..(defproto 
0130: 25 6c 6f 67 20 25 73 69 6e 6b 0a 20 20 62 75 66  %log %sink.  buf
0140: 66 65 72 20 20 20 20 20 20 20 20 20 7b 7d 0a 20  fer         {}. 
0150: 20 73 69 6e 6b 20 20 20 20 20 20 20 20 20 20 20   sink           
0160: 7b 3a 69 6e 69 74 3f 20 74 72 75 65 7d 0a 20 20  {:init? true}.  
0170: 66 69 6c 74 65 72 20 20 20 20 20 20 20 20 20 7b  filter         {
0180: 3a 69 6e 69 74 3f 20 74 72 75 65 7d 0a 20 20 64  :init? true}.  d
0190: 72 61 69 6e 20 20 20 20 20 20 20 20 20 20 7b 3a  rain          {:
01a0: 69 6e 69 74 3f 20 74 72 75 65 7d 0a 20 20 66 6f  init? true}.  fo
01b0: 72 6d 61 74 74 65 72 20 20 20 20 20 20 7b 3a 69  rmatter      {:i
01c0: 6e 69 74 3f 20 74 72 75 65 7d 0a 20 20 6e 61 6d  nit? true}.  nam
01d0: 65 2d 66 6f 72 6d 61 74 74 65 72 20 7b 3a 69 6e  e-formatter {:in
01e0: 69 74 3f 20 74 72 75 65 7d 0a 20 20 73 65 74 74  it? true}.  sett
01f0: 69 6e 67 73 20 20 20 20 20 20 20 7b 3a 69 6e 69  ings       {:ini
0200: 74 3f 20 74 72 75 65 7d 0a 20 20 73 74 61 74 65  t? true}.  state
0210: 20 20 20 20 20 20 20 20 20 20 7b 7d 29 0a 0a 28            {})..(
0220: 64 65 66 6e 2d 20 72 75 6e 2d 61 63 74 69 6f 6e  defn- run-action
0230: 73 20 5b 73 65 6c 66 20 6d 65 74 68 6f 64 20 26  s [self method &
0240: 6f 70 74 20 64 61 74 61 5d 0a 20 20 28 65 61 63  opt data].  (eac
0250: 68 20 61 63 74 69 6f 6e 20 28 64 72 61 69 6e 20  h action (drain 
0260: 73 65 6c 66 29 0a 20 20 20 20 28 77 69 74 68 2d  self).    (with-
0270: 73 6c 6f 74 73 20 25 6c 6f 67 20 73 65 6c 66 0a  slots %log self.
0280: 20 20 20 20 20 20 28 6d 61 74 63 68 20 61 63 74        (match act
0290: 69 6f 6e 0a 20 20 20 20 20 20 20 20 5b 3a 6f 70  ion.        [:op
02a0: 65 6e 20 74 72 75 65 5d 20 20 20 20 20 28 6f 70  en true]     (op
02b0: 65 6e 20 73 69 6e 6b 20 28 6e 61 6d 65 2d 66 6f  en sink (name-fo
02c0: 72 6d 61 74 74 65 72 20 73 65 6c 66 29 29 0a 20  rmatter self)). 
02d0: 20 20 20 20 20 20 20 5b 3a 63 6c 6f 73 65 20 74         [:close t
02e0: 72 75 65 5d 20 20 20 20 28 63 6c 6f 73 65 20 73  rue]    (close s
02f0: 69 6e 6b 29 0a 20 20 20 20 20 20 20 20 5b 3a 77  ink).        [:w
0300: 72 69 74 65 20 3a 62 75 66 66 65 72 5d 20 28 77  rite :buffer] (w
0310: 72 69 74 65 20 78 20 28 66 6f 72 6d 61 74 74 65  rite x (formatte
0320: 72 20 73 65 6c 66 20 64 61 74 61 29 29 0a 20 20  r self data)).  
0330: 20 20 20 20 20 20 5b 3a 77 72 69 74 65 20 3a 73        [:write :s
0340: 69 6e 6b 5d 20 20 20 28 66 6c 75 73 68 20 62 75  ink]   (flush bu
0350: 66 66 65 72 20 73 69 6e 6b 29 29 29 29 29 0a 0a  ffer sink)))))..
0360: 28 64 65 66 6d 65 74 68 6f 64 20 5f 69 6e 69 74  (defmethod _init
0370: 20 25 6c 6f 67 20 5b 73 65 6c 66 5d 0a 20 20 28   %log [self].  (
0380: 77 69 74 68 2d 73 6c 6f 74 73 20 25 6c 6f 67 20  with-slots %log 
0390: 73 65 6c 66 0a 20 20 20 20 28 73 65 74 20 28 40  self.    (set (@
03a0: 20 62 75 66 66 65 72 29 20 28 3a 6e 65 77 20 62   buffer) (:new b
03b0: 75 66 66 65 72 29 29 0a 20 20 20 20 28 73 65 74  uffer)).    (set
03c0: 20 28 40 20 73 74 61 74 65 29 20 40 7b 7d 29 29   (@ state) @{}))
03d0: 29 0a 0a 28 64 65 66 6d 65 74 68 6f 64 20 77 72  )..(defmethod wr
03e0: 69 74 65 20 25 6c 6f 67 20 5b 73 65 6c 66 20 64  ite %log [self d
03f0: 61 74 61 5d 0a 20 20 28 77 68 65 6e 20 28 6e 6f  ata].  (when (no
0400: 74 20 28 66 69 6c 74 65 72 20 64 61 74 61 29 29  t (filter data))
0410: 0a 20 20 20 20 28 6c 65 74 20 5b 7b 3a 6c 65 76  .    (let [{:lev
0420: 65 6c 20 6c 76 6c 20 3a 63 6f 6e 74 65 78 74 20  el lvl :context 
0430: 63 74 78 20 3a 64 61 74 61 20 64 7d 20 64 61 74  ctx :data d} dat
0440: 61 5d 0a 20 20 20 20 20 20 28 72 75 6e 2d 61 63  a].      (run-ac
0450: 74 69 6f 6e 73 20 73 65 6c 66 20 3a 77 72 69 74  tions self :writ
0460: 65 20 64 61 74 61 29 29 29 29 0a 0a 28 64 65 66  e data))))..(def
0470: 6d 65 74 68 6f 64 20 66 6c 75 73 68 20 25 6c 6f  method flush %lo
0480: 67 20 5b 73 65 6c 66 5d 0a 20 20 28 72 75 6e 2d  g [self].  (run-
0490: 61 63 74 69 6f 6e 73 20 73 65 6c 66 20 3a 66 6c  actions self :fl
04a0: 75 73 68 29 29 0a 0a 28 64 65 66 6d 65 74 68 6f  ush))..(defmetho
04b0: 64 20 63 6c 6f 73 65 20 25 6c 6f 67 20 5b 73 65  d close %log [se
04c0: 6c 66 5d 0a 20 20 28 72 75 6e 2d 61 63 74 69 6f  lf].  (run-actio
04d0: 6e 73 20 73 65 6c 66 20 3a 63 6c 6f 73 65 29 29  ns self :close))
04e0: 0a 23 20 65 6e 64 20 6c 6f 67 20 23 23 23 23 23  .# end log #####
04f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0500: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0510: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0520: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 0a  ###############.