diff --git a/src/js/src/js/Que.js b/src/js/src/js/Que.js index ca936e5..e6c12ad 100644 --- a/src/js/src/js/Que.js +++ b/src/js/src/js/Que.js @@ -12,11 +12,6 @@ this.origin = base || document; } this.raw = this.origin.querySelectorAll(selector); - /* - this.list = asList; - this.get = asNode; - this.que = reQue; - */ } function asList() { diff --git a/src/js/src/js/SyslogStmt.js b/src/js/src/js/SyslogStmt.js new file mode 100644 index 0000000..ae17e0b --- /dev/null +++ b/src/js/src/js/SyslogStmt.js @@ -0,0 +1,117 @@ +/** + * @typedef { 'Emerg' | 'Alert' | 'Crit' | 'Err' | 'Warn' | 'Notice' | 'Info' | 'Debug' } Severity + */ + +export class SyslogStmt { + static sevNum = { + Emerg: 0, + Alert: 1, + Crit: 2, + Err: 3, + Warn: 4, + Notice: 5, + Info: 6, + Debug: 7, + }; + static sev = this.sevNum; + + #facility = 16; + #severity = 1; + #timestamp = new Date(); + #version = 1; + #hostname = "-"; + #appname = "-"; + #procId = "-"; + #msgId = "-"; + #structuredData = "-"; + #msg = ""; + + get pri() { + return this.#facility * 8 + this.#severity; + } + + /** clone with message */ + gen(msg) { + const result = new SyslogStmt(); + result.#severity = this.#severity; + result.#facility = this.#facility; + result.#version = this.#version; + result.#hostname = this.#hostname; + result.#appname = this.#appname; + result.#procId = this.#procId; + result.#msgId = this.#msgId; + result.#structuredData = this.#structuredData; + result.#msg = msg; + return result; + } + + sev(severity) { + if (sevNum.hasOwn(severity)) { + this.#severity = SyslogStmt.sevNum[severity]; + } else if (Number.isInteger(severity) && 0 <= severity && severity <= 7) { + this.#severity = severity; + } else { + throw new Error(`Invalid severity: ${severity}`); + } + return this; + } + + fac(facility) { + this.#facility = facility; + return this; + } + + time(timestamp) { + if (timestamp instanceof Date) { + this.#timestamp = timestamp; + } else { + this.#timestamp = new Date(timestamp); + } + return this; + } + + ver(version) { + this.#version = version; + return this; + } + + host(hostname) { + this.#hostname = hostname; + return this; + } + + app(appname) { + this.#appname = appname; + return this; + } + + proc(procId) { + this.#procId = procId; + return this; + } + + msgId(msgId) { + this.#msgId = msgId; + return this; + } + + sd(structuredData) { + this.#structuredData = structuredData; + } + + toRFC5424() { + const msg = this.#msg ? " " + "\uFEFF" + this.#msg : ""; + return `${this.#header()} ${this.#structuredData}${msg}`; + } + + get str() { return this.toRFC5424() }; + + #header() { + const timestamp = + this.#timestamp instanceof Date + ? this.#timestamp.toISOString() + : "-"; + + return `<${this.pri}> ${this.#version} ${timestamp} ${this.#hostname} ${this.#appname} ${this.#procId} ${this.#msgId}`; + } +} diff --git a/src/js/src/main/app.html b/src/js/src/main/app.html index 0891300..f61d22f 100644 --- a/src/js/src/main/app.html +++ b/src/js/src/main/app.html @@ -11,7 +11,7 @@ * 何が何でもロガーを動かすためにapp.htmlの中で定義する。 */ class CoreLogger { - #buffer= []; + #buffer = []; #volume; #severity; #isReady = false; @@ -21,41 +21,42 @@ constructor(logVolume, severity = 5) { //Warn以上を表示 this.#volume = logVolume - this.#severity = severity; + this.#severity = severity; this.#initializeBuffer(); } - + // 各if文の数字はSyslogのSeverity値が由来 err = (msg) => { - if(this.#severity < 3) return; + if (this.#severity < 3) return; this.#log("Err", msg); return this; }; warn = (msg) => { - if(this.#severity < 4) return; - this.#log("Warn", msg); + if (this.#severity < 4) return; + this.#log("Warn", msg); return this; }; - + notice = (msg) => { - if(this.#severity < 5) return; - this.#log("Notice", msg); - return this;}; - + if (this.#severity < 5) return; + this.#log("Notice", msg); + return this; + }; + info = (msg) => { - if(this.#severity < 6) return; + if (this.#severity < 6) return; this.#log("Info", msg); return this; }; debug = (msg) => { - if(this.#severity < 7) return; + if (this.#severity < 7) return; this.#log("Debug", msg); return this; }; - + /** * 指定のSeverity値でログを出力する。 * @@ -65,33 +66,50 @@ * @param {string} msg */ #log(severity, msg) { - const now = new Date().toISOString(); - const content = `[${severity}] ${now} ${msg}`; - this.#buffer[this.#index++] = content; + this.#buffer[this.#index++] = { + severity, + timestamp: new Date(), // Dateオブジェクトのまま格納 + msg + }; + this.#index = this.#index % this.#volume; + } + + + log(msg) { + this.#buffer[this.#index++] = msg; this.#index = this.#index % this.#volume; } takeover(kernelLogReceiver) { this.#kernelLogReceiver = kernelLogReceiver; + this.#isReady = true; } flush() { - for(let index = 0; index < this.#volume; index++) { + for (let index = 0; index < this.#volume; index++) { const src = (this.#index + index) % this.#volume; - if(this.#buffer[src] === null ) { + const entry = this.#buffer[src]; + if (entry === null || entry === undefined) { continue; } - - if(this.#isReady) { - this.#kernelLogReceiver.receive(this.#buffer[src]) + // flushのタイミングで文字列化 + let formattedMsg; + if (typeof entry === 'object' && entry !== null && 'severity' in entry) { + const timeStr = entry.timestamp.toISOString(); + formattedMsg = `[${entry.severity}] ${timeStr} ${entry.msg}`; } else { - window.console.log(this.#buffer[src]) + formattedMsg = String(entry); + } + if (this.#isReady) { + this.#kernelLogReceiver.receive(formattedMsg); + } else { + window.console.log(formattedMsg); } } } #initializeBuffer() { - for(let index = 0; index < this._volume; index++) { + for (let index = 0; index < this.#volume; index++) { const target = (this.#index + index) % this.#volume; this.#buffer[target] = null; } diff --git a/src/js/src/main/app.js b/src/js/src/main/app.js index 1f10b8c..c5696d4 100644 --- a/src/js/src/main/app.js +++ b/src/js/src/main/app.js @@ -1,17 +1,18 @@ -import { Que } from "/solitude/resources/js/Que.js"; -import { graftSolitude } from "/solitude/resources/js/solitude.js" +import { Que } from "../js/Que.js"; +import { graftSolitude } from "..//js/solitude.js" +import { SyslogStmt } from "../js/SyslogStmt.js"; window.addEventListener('load', main(document)); function main(document) { - const kernelLogger = window.kernelLogger + const kernelLogger = window.kernelLogger kernelLogger.info("main started"); graftSolitude(window); webconfig(window); const mysolitude = window.solitude; mysolitude.apps = {}; Object.assign(mysolitude.apps, window.tmp.solitudeapps); - + //アプリケーション毎に初期化操作 initializeSolitudeApps(mysolitude.apps) @@ -32,8 +33,33 @@ toggleMenuItem(Que("#main").get()); }); + + + // app.js の main 関数内など + mysolitude.message.addListener(function (message) { + if (message.type === "log_receiver_ready") { + // kernelLoggerにログを引き渡すレシーバーを設定 + kernelLogger.takeover({ + receive: receive, + }); + + function receive(msg) { + mysolitude.message.fire({ + type: "log", + message: msg + }) + } + + // 溜まっていたログをフラッシュする + kernelLogger.flush(); + } + }); + + + + const b = new SyslogStmt(); + kernelLogger.log(b.gen("SyslogStmt test2").str); kernelLogger.debug("flush !"); - kernelLogger.flush(); // 以下、宣言 function toggleMenuItem(element) { @@ -101,11 +127,11 @@ //something wrong; break; } - if(app) { + if (app) { app.instanceID = ++appInstanceID; app.load(); - kernelLogger.info("loaded ".concat(name, ", instanceID ", app.instanceID )); - } + kernelLogger.info("loaded ".concat(name, ", instanceID ", app.instanceID)); + } } @@ -141,26 +167,26 @@ const app = this; mount.que("li.init").get().addEventListener("click", function () { - if(isInitialized(app.instanceID)) { - return; - } + if (isInitialized(app.instanceID)) { + return; + } addNewTab(app); - addNewContent(app); + addNewContent(app); kernelLogger.info(app.name + " is initialized"); }); - mount.que("li.init").get().classList.remove("init"); + mount.que("li.init").get().classList.remove("init"); } function isInitialized(appInstanceID) { //アプリのタブが出ているかでロード済みかを判断 let result = false; Que("#main-tabs li").list().forEach(elem => { - if(appInstanceID == elem.dataset.soloAppId) { + if (appInstanceID == elem.dataset.soloAppId) { result = true; } }); - return result; + return result; } diff --git a/src/js/src/simplelog/app.js b/src/js/src/simplelog/app.js index 69ee3d0..14b309d 100644 --- a/src/js/src/simplelog/app.js +++ b/src/js/src/simplelog/app.js @@ -1,15 +1,19 @@ (function () { const sol = window.solitude || graftSolitude(window); + sol.message.addListener(receptLog); + sol.message.fire({ + type: "log_receiver_ready", + }); function receptLog(sign) { - if(sign.type != "log") + if (sign.type != "log") return; - if(sign.level) { - log("[" + sign.level +"]: " + sign.msg); + if (sign.level) { + log("[" + sign.level + "]: " + sign.msg); } else { - log(sign.message); + log(sign.message); } }