/
ZnLogEvent.class.st
124 lines (102 loc) · 2.69 KB
/
ZnLogEvent.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"
I am ZnLogEvent, the base class of a log events emitted by elements of the Zinc HTTP Components framework.
I add a timestamp and a simple id attribute. The id can wrap around and should only be used to distinguish between events that have the same timestamp.
ZnLogEvents are distributed as Announcement through a singleton Announcer that I maintain.
I have a small convenience API to log to the Transcript or open a simple GUI on the emitted log events.
"
Class {
#name : #ZnLogEvent,
#superclass : #Announcement,
#instVars : [
'timestamp',
'id'
],
#classVars : [
'IdCounter',
'LogEventAnnouncer'
],
#category : #'Zinc-HTTP-Logging'
}
{ #category : #accessing }
ZnLogEvent class >> announcer [
^ LogEventAnnouncer ifNil: [ LogEventAnnouncer := Announcer new ]
]
{ #category : #'class initialization' }
ZnLogEvent class >> initialize [
IdCounter := 0.
self environment
at: #SessionManager
ifPresent: [ :manager | manager default registerNetworkClassNamed: self name ]
ifAbsent: [ Smalltalk addToStartUpList: self ]
]
{ #category : #convenience }
ZnLogEvent class >> logToTranscript [
self stopLoggingToTranscript.
^ self announcer when: ZnLogEvent do: [ :event | self crTrace: event ].
]
{ #category : #accessing }
ZnLogEvent class >> nextId [
"This should be thread safe because SmallInteger
arithmetic primitives cannot be interrupted"
^ IdCounter := IdCounter + 1
]
{ #category : #convenience }
ZnLogEvent class >> open [
^ self announcer inspect
]
{ #category : #'system startup' }
ZnLogEvent class >> startUp [
"Reset the id counter"
IdCounter := 0
]
{ #category : #convenience }
ZnLogEvent class >> stopLoggingToTranscript [
self announcer unsubscribe: self
]
{ #category : #accessing }
ZnLogEvent >> announcer [
^ self class announcer
]
{ #category : #actions }
ZnLogEvent >> emit [
self announcer announce: self
]
{ #category : #accessing }
ZnLogEvent >> id [
^ id
]
{ #category : #initialize }
ZnLogEvent >> initialize [
super initialize.
timestamp := DateAndTime now.
id := self nextId
]
{ #category : #accessing }
ZnLogEvent >> nextId [
^ self class nextId
]
{ #category : #printing }
ZnLogEvent >> printContentsOn: stream [
"Subclasses should implement this to add output"
]
{ #category : #printing }
ZnLogEvent >> printOn: stream [
timestamp printYMDOn: stream.
stream space.
timestamp printHMSOn: stream.
stream space.
id \\ 1000 printOn: stream base: 10 length: 3 padded: true.
self printContentsOn: stream
]
{ #category : #beacon }
ZnLogEvent >> printOneLineContentsOn: stream [
self printContentsOn: stream
]
{ #category : #beacon }
ZnLogEvent >> printOneLineOn: stream [
self printOn: stream
]
{ #category : #accessing }
ZnLogEvent >> timestamp [
^ timestamp
]