This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (108 loc) · 3.34 KB
/
index.js
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
125
126
127
128
129
130
131
132
var util = require('util');
var events = require('events');
/**
EventLogger can be used for simple logging and audit logging - but also as a timer for test and debugging
use EventLogger.events to retrieve the logged events
*/
module.exports = EventLogger = function(name, emitEvents) {
this.emitEvents = emitEvents || true;
if(this.emitEvents === true) {
events.EventEmitter.call(this);
}
name = name || '';
if(name) {
this.name = name;
}
this.events = [];
this.started = new Date().getTime();
this.lastEvent = this.started;
this.event({ event: 'started', type: 'system' });
};
util.inherits(EventLogger, events.EventEmitter);
module.exports = EventLogger;
/**
End the logger and calculate total time (ms) of all events
*/
EventLogger.prototype.end = function() {
this.event({ event: 'ended', type: 'system' });
this.ended = new Date().getTime();
this.total = this.ended - this.started;
if(this.emitEvents === true) {
this.emit('logger ended');
}
return this.total;
};
/**
This is for logging events with exact elapsed time
*/
EventLogger.prototype.startEvent = function(data) {
var event = this.event(data);
event.end = this.endEvent;
event.emit = this.emit;
if(this.emitEvents === true) {
this.emit('event started', data);
}
return event;
};
/**
End the event - you can add more data to the event object
*/
EventLogger.prototype.endEvent = function(data) {
// Get the time before updating the event object
// What we are logging are done now and the time to calculate and build the event object,
// shouldn't be counted as time spent on the logged event.
var tmpTime = new Date().getTime();
// Make sure we have an object
data = data || {};
if(typeof data !== 'object') {
this.event = data;
} else {
for(var i in data) {
this[i] = data[i];
}
}
// Calculate the timers
this.ended = tmpTime;
this.elapsed = tmpTime - this.time;
this.elapsedTotal += this.elapsed;
// Don't wait for the garbage collector...
delete tmpTime;
delete this.end;
if(this.emitEvents === true) {
this.emit('event ended', data);
}
return this;
};
/**
Log an event and assume elapsed time is since last event
This is more than enough for audit logging as we don't need to know the exact time it took
*/
EventLogger.prototype.event = function(data) {
// Start by making sure the event gets into the event array in the right order
var newLength = this.events.push({});
// Get the time before building the event object
// What we are logging are done now and the time to calculate and build the event object,
// shouldn't be counted as time spent on the logged event.
var tmpTime = new Date().getTime();
// Make sure the lastEvent are as much up to date as possible,
// by copying it's value and setting it to the new emediatly
var lastEvent = this.lastEvent;
this.lastEvent = tmpTime;
// Make sure we have an object
data = data || {};
if(typeof data !== 'object') {
data = { event: data };
}
// Calculate the timers
data.time = tmpTime;
data.elapsed = tmpTime - lastEvent;
data.elapsedTotal = tmpTime - this.started;
// Don't wait for the garbage collector...
delete tmpTime;
delete lastEvent;
if(this.emitEvents === true) {
this.emit('event', data);
}
// Set the event data and return it
return this.events[newLength - 1] = data;
};