Index

lognestmonster / 0a557d5

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
5828 Dec 2018 00:068b6e345Allow compaction of log filesJoshua17010N

Blob @ lognestmonster / src / queue.js

application/javascript3234 bytesdownload raw
1const statement = require("./statement.js");
2const event = require("./event.js");
3
4const FileHandler = require("./fileHandler.js");
5
6class Queue {
7 constructor(name, location, config) {
8 if (typeof name != "string") throw new Error(`Expected string, got ${typeof name} for name`);
9 if (typeof location != "string") throw new Error(`Expected string, got ${typeof location} for location`);
10 this.name = name;
11 this.timestamp = new Date();
12 this.location = location;
13 this.filename = this.location + "/" + this.timestamp.toISOString().replace(/\:/g, "-").replace(/\./g, "-") + ".log";
14 this.queue = [];
15
16 this.compact = false;
17 this.fileStarted = false;
18
19 if (config) {
20 if (config.compact) {
21 if (typeof config.compact != "boolean") throw new Error(`Expected boolean, got ${typeof config.compact} for config.compact`);
22 this.compact = config.compact;
23 }
24 }
25
26 FileHandler.AppendFile(this.filename, ""); // touch the file
27
28 return this;
29 }
30
31 push() {
32 if (arguments.length >= 3) this.queue.push(new statement(arguments[0], arguments[1], arguments[2]));
33 else if (arguments.length > 0) {
34 if (arguments[0] instanceof event) this.queue.push(arguments[0].statements); // object is an Event
35 else if (arguments[0] instanceof statement) this.queue.push(arguments[0]); // object is a Statement
36 else throw new TypeError("Expected an Event or Statement");
37 } else throw new Error("Expected at least one argument");
38 return this;
39 }
40
41 write() {
42 let recursiveCompactFunction;
43 if (this.compact) {
44 recursiveCompactFunction = function(object) {
45 if (Array.isArray(object)) {
46 for (let i = 0; i < object.length; i++) {
47 object[i] = recursiveCompactFunction(object[i]);
48 }
49 return object;
50 } else {
51 let fixed = {};
52 fixed.tt = object.timestamp;
53 fixed.v = object.verbosity;
54 fixed.t = object.tag;
55 fixed.m = object.message;
56 return fixed;
57 }
58 }
59 }
60
61 if (arguments.length > 0) {
62 let toWrite;
63 if (arguments.length >= 3) toWrite = new statement(arguments[0], arguments[1], arguments[2]);
64 else {
65 if (arguments[0] instanceof event) toWrite = arguments[0].statements; // object is an Event
66 else if (arguments[0] instanceof statement) toWrite = arguments[0]; // object is a Statement
67 else throw new TypeError("Expected an Event or Statement");
68 }
69
70 if (this.compact) {
71 toWrite = recursiveCompactFunction(toWrite);
72 }
73
74 let appendString = "";
75 if (this.fileStarted) appendString += "\n";
76 appendString += JSON.stringify(toWrite);
77 if (!this.fileStarted) this.fileStarted = true;
78 FileHandler.AppendFile(this.filename, appendString);
79 } else {
80 let queueLength = this.queue.length;
81 if (queueLength > 0) {
82 let appendString = "";
83 let queue = this;
84 let toWrite = this.queue;
85 if (this.compact) {
86 toWrite = recursiveCompactFunction(toWrite);
87 }
88 toWrite.forEach(function (item) {
89 if (queue.fileStarted) appendString += "\n";
90 appendString += JSON.stringify(item);
91 if (!queue.fileStarted) queue.fileStarted = true;
92 });
93 this.queue = this.queue.slice(queueLength);
94 FileHandler.AppendFile(this.filename, appendString);
95 }
96 }
97 return this;
98 }
99}
100
101module.exports = Queue;
102