Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | 5x 3x 3x 1x 2x 99x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 11x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 15x 15x 9x 6x 9x 8x 8x 2x 6x 2x 4x 4x 8x 5x 3x 3x 3x 5x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 11x 11x 2x 2x 13x 2x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 12x 12x 7x 7x 5x 5x 11x 11x 18x 18x 4x 18x | /**
* @typedef { 'Emerg' | 'Alert' | 'Crit' | 'Err' | 'Warn' | 'Notice' | 'Info' | 'Debug' } Severity
*/
import { StructuredData } from "./StructuredData.js";
export class SyslogStmt {
static get sevNum() {
return {
Emerg: 0,
Alert: 1,
Crit: 2,
Err: 3,
Warn: 4,
Notice: 5,
Info: 6,
Debug: 7,
};
}
static get sevStr() {
return [
"Emerg",
"Alert",
"Crit",
"Err",
"Warn",
"Notice",
"Info",
"Debug",
];
}
static get facNum() {
return {
kern: 0,
user: 1,
mail: 2,
daemon: 3,
auth: 4,
syslog: 5,
lpr: 6,
news: 7,
uucp: 8,
cron: 9,
authpriv: 10,
ftp: 11,
ntp: 12,
audit: 13,
alert: 14,
clock: 15,
local0: 16,
local1: 17,
local2: 18,
local3: 19,
local4: 20,
local5: 21,
local6: 22,
local7: 23,
};
}
static get facStr() {
return [
"kern",
"user",
"mail",
"daemon",
"auth",
"syslog",
"lpr",
"news",
"uucp",
"cron",
"authpriv",
"ftp",
"ntp",
"audit",
"alert",
"clock",
"local0",
"local1",
"local2",
"local3",
"local4",
"local5",
"local6",
"local7",
];
}
static sev = this.sevNum;
static get NILVALUE() {
return "-";
}
#facility = 16;
#severity = 1;
#timestamp = new Date();
#version = 1;
#hostname = SyslogStmt.NILVALUE;
#appname = SyslogStmt.NILVALUE;
#procId = SyslogStmt.NILVALUE;
#msgId = SyslogStmt.NILVALUE;
#structuredData = new StructuredData();
#msg = "";
/**
* PRI値を取得する。
* @returns {number}
*/
get pri() {
return this.#facility * 8 + this.#severity;
}
/**
* タイムスタンプと構造化データを覗いて複製する。
* 引数で新しいメッセージを設定する。
* 申し訳ないがMSGにバイナリがRFC5424的には許されるが、ここでは対応してない(勘弁して欲しい)
* @param {string} msg
* @returns {SyslogStmt}
**/
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;
//structuredDataとmsgはコピーしない。
result.#msg = msg;
return result;
}
/**
* 現在のインスタンスのコピーを作成する。
* @returns {SyslogStmt}
*/
clone() {
const result = this.gen();
result.#structuredData = this.#structuredData;
result.#timestamp = this.#timestamp;
return result;
}
/**
* 重大度を設定する。
* @param {string | number} severity
* @returns {SyslogStmt}
*/
sev(severity) {
Iif (typeof severity === "string" && SyslogStmt.sevNum.hasOwnProperty(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;
}
/**
* ファシリティを設定する。
* @param {string | number} facility
* @returns {SyslogStmt}
*/
fac(facility) {
Iif (typeof facility === "string" && SyslogStmt.facNum.hasOwnProperty(facility)) {
this.#facility = SyslogStmt.facNum[facility];
} else if (Number.isInteger(facility) && 0 <= facility && facility <= 23) {
this.#facility = facility;
} else {
throw new Error(`Invalid facility: ${facility}`);
}
return this;
}
/**
* バージョンを設定する。とりあえず0か1しか設定しない。
* @param {number} version
* @returns {SyslogStmt}
*/
ver(version) {
Iif (Number.isInteger(version) && 0 <= version && version <= 1) {
this.#version = version;
} else {
throw new Error(`Invalid version: ${version}`);
}
return this;
}
/**
* タイムスタンプを設定する。現在時刻はログ生成時に自動設定される。
* 引数がDate型の場合はそのインスタンスを使用、そうでない場合はDateコンストラクタに渡す。
* @param {*} timestamp
* @returns {SyslogStmt}
*/
time(timestamp) {
if (timestamp instanceof Date) {
this.#timestamp = timestamp;
} else {
this.#timestamp = new Date(timestamp);
Eif (isNaN(this.#timestamp)) {
throw new Error(`Invalid timestamp: ${timestamp}`);
}
}
return this;
}
/**
* ホスト名を設定する。
* @param {string} hostname 空文字、null、undefined を与えられると、NILVALUEとして解釈します。
* @returns {SyslogStmt}
*/
host(hostname) {
const regExp = /^[\x21-\x7E]{1,255}$/;
hostname = this.#nilOrString(hostname);
Iif (typeof hostname !== "string") {
throw new Error(`Invalid hostname: ${hostname}`);
}
if (!regExp.test(hostname) && hostname !== "-") {
throw new Error(`Invalid hostname: ${hostname}`);
}
this.#hostname = hostname;
return this;
}
/**
* アプリケーション名を設定する。
* @param {string} appname 空文字、null、undefined を与えられると、NILVALUEとして解釈します。
* @returns {SyslogStmt}
*/
app(appname) {
const regExp = /^[\x21-\x7E]{1,48}$/;
appname = this.#nilOrString(appname);
Iif (typeof appname !== "string") {
throw new Error(`Invalid appname: ${appname}`);
}
if (!regExp.test(appname) && appname !== "-") {
throw new Error(`Invalid appname: ${appname}`);
}
this.#appname = appname;
return this;
}
/**
* プロセスIDを設定する。
* @param {string} procId 空文字、null、undefined を与えられると、NILVALUEとして解釈します。
* @returns {SyslogStmt}
*/
proc(procId) {
const regExp = /^[\x21-\x7E]{1,128}$/;
procId = this.#nilOrString(procId);
Iif (typeof procId !== "string") {
throw new Error(`Invalid procId: ${procId}`);
}
if (!regExp.test(procId) && procId !== "-") {
throw new Error(`Invalid procId: ${procId}`);
}
this.#procId = procId;
return this;
}
/**
* メッセージIDを設定する。
* @param {string} msgId 空文字、null、undefined を与えられると、NILVALUEとして解釈します。
* @returns {SyslogStmt}
*/
msgId(msgId) {
const regExp = /^[\x21-\x7E]{1,32}$/;
msgId = this.#nilOrString(msgId);
Iif (typeof msgId !== "string") {
throw new Error(`Invalid msgId: ${msgId}`);
}
if (!regExp.test(msgId) && msgId !== "-") {
throw new Error(`Invalid msgId: ${msgId}`);
}
this.#msgId = msgId;
return this;
}
/**
* 引数で指定した構造化データを設定する。
* 文字列が渡された場合は、そのまま設定する。
* StructuredDataが渡された場合は、toString()を実行してから設定する。
* @param {string | StructuredData} structuredData stringは"-"のみ許可。null、undefinedはNILVALUEとして扱う。
* @returns {SyslogStmt}
*/
sd(structuredData) {
structuredData = this.#nilOrString(structuredData);
if (typeof structuredData === 'string') {
Eif (structuredData !== SyslogStmt.NILVALUE) {
throw new Error(`Invalid structuredData: ${structuredData}`);
}
this.#structuredData = structuredData;
} else if (structuredData instanceof StructuredData) {
this.#structuredData = structuredData.toString();
} else E{
throw new Error(`Invalid structuredData: ${structuredData}`);
}
return this;
}
/**
* RFC5424形式でログを出力する。
* @returns {string}
*/
toRFC5424() {
const msg = this.#msg ? " ".concat("\uFEFF", this.#msg) : "";
return `${this.#header()} ${this.#structuredData.toString()}${msg}`;
}
/**
* [重大度] タイムスタンプ メッセージの形式でログを出力する
* @returns {string}
*/
toSimple() {
const sevStr = this.constructor.sevStr[this.#severity] ?? "Unknown";
return `[${sevStr}] ${this.#timestamp.toISOString()} ${this.#msg}`;
}
/**
* ログの出力形式を指定して出力する
* @param { "rfc5424" | "simple" } format
* @returns {string}
*/
toString(format) {
if (format === "simple") {
return this.toSimple();
}
return this.toRFC5424();
}
get str() { return this.toRFC5424() };
/**
* ログレベルをemergに設定する。
* @returns {SyslogStmt}
*/
emerg() {
return this.sev(0);
}
/**
* ログレベルをcritに設定する。
* @returns {SyslogStmt}
*/
crit() {
return this.sev(1);
}
/**
* ログレベルをalertに設定する。
* @returns {SyslogStmt}
*/
alert() {
return this.sev(2);
}
/**
* ログレベルをerrに設定する。
* @returns {SyslogStmt}
*/
err() {
return this.sev(3);
}
/**
* ログレベルをwarnに設定する。
* @returns {SyslogStmt}
*/
warn() {
return this.sev(4);
}
/**
* ログレベルをnoticeに設定する。
* @returns {SyslogStmt}
*/
notice() {
return this.sev(5);
}
/**
* ログレベルをinfoに設定する。
* @returns {SyslogStmt}
*/
info() {
return this.sev(6);
}
/**
* ログレベルをdebugに設定する。
* @returns {SyslogStmt}
*/
debug() {
return this.sev(7);
}
/**
* 指定したログレベルが現在の重大度以上であるかを判定する。
* @param {number} level
* @returns {boolean}
*/
isOutput(level) {
return this.#severity <= level;
}
/**
* ログのメッセージ中の制御文字をエスケープする。
* 対象:ANSI制御文字の0x00-0x1F (LF:0x0A, TAB:0x09 を除く) および 0x7F
* UTF8の\u200B-\u200F、\uFEFF\uFFFE\uFFFF
* 例:"h\x00\u200B\uFEFFtest" -> "h\\x00\\u200B\\uFEFFtest"
* @param {string} str
* @returns {string}
*/
static escapeControlChars(str) {
// 0x00-0x1F (TAB:0x09,LF:0x0A,CR:0x0D を除く) および 0x7F を対象にする
// \x00-\x08: NUL~BS
// \x0B-\x0C: VT, FF
// \x0E-\x1F: SO~US
// \x7F: DEL
// \u200B-\u200F,\uFEFF, \uFFFE, \uFFFF:非文字
return str.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\u200B-\u200F\uFEFF\uFFFE\uFFFF]/g, (match) => {
const char = match.charCodeAt(0);
if (char <= 127) {
const code = char.toString(16).toUpperCase().padStart(2, '0');
return `\\x${code}`;
} else {
const code = char.toString(16).toUpperCase().padStart(4, '0');
return `\\u${code}`;
}
});
}
/**
* ヘッダーを生成する。
* @returns {string}
*/
#header() {
const timestamp = this.#timestamp instanceof Date
? this.#timestamp.toISOString()
: SyslogStmt.NILVALUE;
return `<${this.pri}> ${this.#version} ${timestamp} ${this.#hostname} ${this.#appname} ${this.#procId} ${this.#msgId}`;
}
/**
* undefined, null, 空文字を NILVALUE("-") に置換する
* @param {*} src
* @returns
*/
#nilOrString(src) {
let result = src ?? "";
if (result === "") {
result = SyslogStmt.NILVALUE;
}
return result;
}
}
|