@logtape/file 0.11.0 → 0.12.0-dev.181
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/deno.json +37 -0
- package/dist/_virtual/rolldown_runtime.cjs +30 -0
- package/dist/filesink.base.cjs +81 -0
- package/dist/filesink.base.d.cts +98 -0
- package/dist/filesink.base.d.cts.map +1 -0
- package/dist/filesink.base.d.ts +98 -0
- package/dist/filesink.base.d.ts.map +1 -0
- package/dist/filesink.base.js +80 -0
- package/dist/filesink.base.js.map +1 -0
- package/dist/filesink.deno.cjs +67 -0
- package/dist/filesink.deno.d.cts +40 -0
- package/dist/filesink.deno.d.cts.map +1 -0
- package/dist/filesink.deno.d.ts +40 -0
- package/dist/filesink.deno.d.ts.map +1 -0
- package/dist/filesink.deno.js +66 -0
- package/dist/filesink.deno.js.map +1 -0
- package/dist/filesink.node.cjs +60 -0
- package/{esm/file/filesink.node.js → dist/filesink.node.d.cts} +12 -19
- package/dist/filesink.node.d.cts.map +1 -0
- package/{types/file → dist}/filesink.node.d.ts +11 -5
- package/dist/filesink.node.d.ts.map +1 -0
- package/dist/filesink.node.js +58 -0
- package/dist/filesink.node.js.map +1 -0
- package/dist/mod.cjs +15 -0
- package/dist/mod.d.cts +3 -0
- package/dist/mod.d.ts +3 -0
- package/dist/mod.js +3 -0
- package/filesink.base.ts +170 -0
- package/filesink.deno.ts +69 -0
- package/filesink.jsr.ts +52 -0
- package/filesink.node.ts +64 -0
- package/filesink.test.ts +224 -0
- package/mod.ts +7 -0
- package/package.json +37 -25
- package/tsdown.config.ts +22 -0
- package/esm/file/filesink.base.js +0 -85
- package/esm/file/mod.js +0 -1
- package/esm/package.json +0 -3
- package/script/file/filesink.base.js +0 -89
- package/script/file/filesink.node.js +0 -55
- package/script/file/mod.js +0 -6
- package/script/package.json +0 -3
- package/types/_dnt.test_shims.d.ts.map +0 -1
- package/types/deps/jsr.io/@david/which-runtime/0.2.1/mod.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_is_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assert_throws.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_common/assert_path.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_common/constants.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_common/from_file_url.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_common/normalize.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_common/normalize_string.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/_os.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/join.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/posix/_util.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/posix/from_file_url.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/posix/join.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/posix/normalize.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/windows/_util.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/windows/from_file_url.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/windows/join.d.ts.map +0 -1
- package/types/deps/jsr.io/@std/path/1.1.0/windows/normalize.d.ts.map +0 -1
- package/types/file/filesink.base.d.ts +0 -94
- package/types/file/filesink.base.d.ts.map +0 -1
- package/types/file/filesink.node.d.ts.map +0 -1
- package/types/file/filesink.test.d.ts.map +0 -1
- package/types/file/mod.d.ts +0 -3
- package/types/file/mod.d.ts.map +0 -1
- package/types/logtape/fixtures.d.ts.map +0 -1
- package/types/logtape/level.d.ts.map +0 -1
- package/types/logtape/record.d.ts.map +0 -1
package/deno.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@logtape/file",
|
|
3
|
+
"version": "0.12.0-dev.181+e4f33b9d",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"exports": "./mod.ts",
|
|
6
|
+
"imports": {
|
|
7
|
+
"#filesink": "./filesink.jsr.ts"
|
|
8
|
+
},
|
|
9
|
+
"exclude": [
|
|
10
|
+
"coverage/",
|
|
11
|
+
"npm/",
|
|
12
|
+
".dnt-import-map.json"
|
|
13
|
+
],
|
|
14
|
+
"tasks": {
|
|
15
|
+
"build": "pnpm build",
|
|
16
|
+
"test": "deno test --allow-read --allow-write --allow-env=TEMP,TMP,TMPDIR",
|
|
17
|
+
"test:node": {
|
|
18
|
+
"dependencies": [
|
|
19
|
+
"build"
|
|
20
|
+
],
|
|
21
|
+
"command": "node --experimental-transform-types --test"
|
|
22
|
+
},
|
|
23
|
+
"test:bun": {
|
|
24
|
+
"dependencies": [
|
|
25
|
+
"build"
|
|
26
|
+
],
|
|
27
|
+
"command": "bun test"
|
|
28
|
+
},
|
|
29
|
+
"test-all": {
|
|
30
|
+
"dependencies": [
|
|
31
|
+
"test",
|
|
32
|
+
"test:node",
|
|
33
|
+
"test:bun"
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(exports, '__toESM', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return __toESM;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const __logtape_logtape = require_rolldown_runtime.__toESM(require("@logtape/logtape"));
|
|
3
|
+
|
|
4
|
+
//#region filesink.base.ts
|
|
5
|
+
/**
|
|
6
|
+
* Get a platform-independent file sink.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam TFile The type of the file descriptor.
|
|
9
|
+
* @param path A path to the file to write to.
|
|
10
|
+
* @param options The options for the sink and the file driver.
|
|
11
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
12
|
+
* object that closes the file when disposed.
|
|
13
|
+
*/
|
|
14
|
+
function getBaseFileSink(path, options) {
|
|
15
|
+
const formatter = options.formatter ?? __logtape_logtape.defaultTextFormatter;
|
|
16
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
17
|
+
let fd = options.lazy ? null : options.openSync(path);
|
|
18
|
+
const sink = (record) => {
|
|
19
|
+
if (fd === null) fd = options.openSync(path);
|
|
20
|
+
options.writeSync(fd, encoder.encode(formatter(record)));
|
|
21
|
+
options.flushSync(fd);
|
|
22
|
+
};
|
|
23
|
+
sink[Symbol.dispose] = () => {
|
|
24
|
+
if (fd !== null) options.closeSync(fd);
|
|
25
|
+
};
|
|
26
|
+
return sink;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get a platform-independent rotating file sink.
|
|
30
|
+
*
|
|
31
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
32
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
33
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
34
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
35
|
+
*
|
|
36
|
+
* @param path A path to the file to write to.
|
|
37
|
+
* @param options The options for the sink and the file driver.
|
|
38
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
39
|
+
* object that closes the file when disposed.
|
|
40
|
+
*/
|
|
41
|
+
function getBaseRotatingFileSink(path, options) {
|
|
42
|
+
const formatter = options.formatter ?? __logtape_logtape.defaultTextFormatter;
|
|
43
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
44
|
+
const maxSize = options.maxSize ?? 1024 * 1024;
|
|
45
|
+
const maxFiles = options.maxFiles ?? 5;
|
|
46
|
+
let offset = 0;
|
|
47
|
+
try {
|
|
48
|
+
const stat = options.statSync(path);
|
|
49
|
+
offset = stat.size;
|
|
50
|
+
} catch {}
|
|
51
|
+
let fd = options.openSync(path);
|
|
52
|
+
function shouldRollover(bytes) {
|
|
53
|
+
return offset + bytes.length > maxSize;
|
|
54
|
+
}
|
|
55
|
+
function performRollover() {
|
|
56
|
+
options.closeSync(fd);
|
|
57
|
+
for (let i = maxFiles - 1; i > 0; i--) {
|
|
58
|
+
const oldPath = `${path}.${i}`;
|
|
59
|
+
const newPath = `${path}.${i + 1}`;
|
|
60
|
+
try {
|
|
61
|
+
options.renameSync(oldPath, newPath);
|
|
62
|
+
} catch (_) {}
|
|
63
|
+
}
|
|
64
|
+
options.renameSync(path, `${path}.1`);
|
|
65
|
+
offset = 0;
|
|
66
|
+
fd = options.openSync(path);
|
|
67
|
+
}
|
|
68
|
+
const sink = (record) => {
|
|
69
|
+
const bytes = encoder.encode(formatter(record));
|
|
70
|
+
if (shouldRollover(bytes)) performRollover();
|
|
71
|
+
options.writeSync(fd, bytes);
|
|
72
|
+
options.flushSync(fd);
|
|
73
|
+
offset += bytes.length;
|
|
74
|
+
};
|
|
75
|
+
sink[Symbol.dispose] = () => options.closeSync(fd);
|
|
76
|
+
return sink;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
exports.getBaseFileSink = getBaseFileSink;
|
|
81
|
+
exports.getBaseRotatingFileSink = getBaseRotatingFileSink;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Sink, StreamSinkOptions } from "@logtape/logtape";
|
|
2
|
+
|
|
3
|
+
//#region filesink.base.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for the {@link getBaseFileSink} function.
|
|
7
|
+
*/
|
|
8
|
+
type FileSinkOptions = StreamSinkOptions & {
|
|
9
|
+
/**
|
|
10
|
+
* If `true`, the file is not opened until the first write. Defaults to `false`.
|
|
11
|
+
*/
|
|
12
|
+
lazy?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A platform-specific file sink driver.
|
|
16
|
+
* @typeParam TFile The type of the file descriptor.
|
|
17
|
+
*/
|
|
18
|
+
interface FileSinkDriver<TFile> {
|
|
19
|
+
/**
|
|
20
|
+
* Open a file for appending and return a file descriptor.
|
|
21
|
+
* @param path A path to the file to open.
|
|
22
|
+
*/
|
|
23
|
+
openSync(path: string): TFile;
|
|
24
|
+
/**
|
|
25
|
+
* Write a chunk of data to the file.
|
|
26
|
+
* @param fd The file descriptor.
|
|
27
|
+
* @param chunk The data to write.
|
|
28
|
+
*/
|
|
29
|
+
writeSync(fd: TFile, chunk: Uint8Array): void;
|
|
30
|
+
/**
|
|
31
|
+
* Flush the file to ensure that all data is written to the disk.
|
|
32
|
+
* @param fd The file descriptor.
|
|
33
|
+
*/
|
|
34
|
+
flushSync(fd: TFile): void;
|
|
35
|
+
/**
|
|
36
|
+
* Close the file.
|
|
37
|
+
* @param fd The file descriptor.
|
|
38
|
+
*/
|
|
39
|
+
closeSync(fd: TFile): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get a platform-independent file sink.
|
|
43
|
+
*
|
|
44
|
+
* @typeParam TFile The type of the file descriptor.
|
|
45
|
+
* @param path A path to the file to write to.
|
|
46
|
+
* @param options The options for the sink and the file driver.
|
|
47
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
48
|
+
* object that closes the file when disposed.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Options for the {@link getBaseRotatingFileSink} function.
|
|
53
|
+
*/
|
|
54
|
+
interface RotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
55
|
+
/**
|
|
56
|
+
* The maximum bytes of the file before it is rotated. 1 MiB by default.
|
|
57
|
+
*/
|
|
58
|
+
maxSize?: number;
|
|
59
|
+
/**
|
|
60
|
+
* The maximum number of files to keep. 5 by default.
|
|
61
|
+
*/
|
|
62
|
+
maxFiles?: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A platform-specific rotating file sink driver.
|
|
66
|
+
*/
|
|
67
|
+
interface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {
|
|
68
|
+
/**
|
|
69
|
+
* Get the size of the file.
|
|
70
|
+
* @param path A path to the file.
|
|
71
|
+
* @returns The `size` of the file in bytes, in an object.
|
|
72
|
+
*/
|
|
73
|
+
statSync(path: string): {
|
|
74
|
+
size: number;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Rename a file.
|
|
78
|
+
* @param oldPath A path to the file to rename.
|
|
79
|
+
* @param newPath A path to be renamed to.
|
|
80
|
+
*/
|
|
81
|
+
renameSync(oldPath: string, newPath: string): void;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get a platform-independent rotating file sink.
|
|
85
|
+
*
|
|
86
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
87
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
88
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
89
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
90
|
+
*
|
|
91
|
+
* @param path A path to the file to write to.
|
|
92
|
+
* @param options The options for the sink and the file driver.
|
|
93
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
94
|
+
* object that closes the file when disposed.
|
|
95
|
+
*/
|
|
96
|
+
//#endregion
|
|
97
|
+
export { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions };
|
|
98
|
+
//# sourceMappingURL=filesink.base.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.base.d.cts","names":[],"sources":["../filesink.base.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;AAWiB,KAXL,eAAA,GAAkB,iBAWC,GAAA;EAAA;;;EAYV,IAAS,CAAA,EAAA,OAAA;CAAU;;AAYnB;AAqCrB;;AAAsD,UA7DrC,cA6DqC,CAAA,KAAA,CAAA,CAAA;EAAe;AAAhB;AAerD;;EAAuC,QAA+B,CAAA,IAAA,EAAA,MAAA,CAAA,EAvE5C,KAuE4C;EAAK;AAAN;;;;gBAhErD,cAAc;;;;;gBAMd;;;;;gBAMA;;;;;;;;;;;;;;;UAqCC,uBAAA,SAAgC,KAAK;;;;;;;;;;;;;UAerC,sCAAsC,eAAe"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Sink, StreamSinkOptions } from "@logtape/logtape";
|
|
2
|
+
|
|
3
|
+
//#region filesink.base.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for the {@link getBaseFileSink} function.
|
|
7
|
+
*/
|
|
8
|
+
type FileSinkOptions = StreamSinkOptions & {
|
|
9
|
+
/**
|
|
10
|
+
* If `true`, the file is not opened until the first write. Defaults to `false`.
|
|
11
|
+
*/
|
|
12
|
+
lazy?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A platform-specific file sink driver.
|
|
16
|
+
* @typeParam TFile The type of the file descriptor.
|
|
17
|
+
*/
|
|
18
|
+
interface FileSinkDriver<TFile> {
|
|
19
|
+
/**
|
|
20
|
+
* Open a file for appending and return a file descriptor.
|
|
21
|
+
* @param path A path to the file to open.
|
|
22
|
+
*/
|
|
23
|
+
openSync(path: string): TFile;
|
|
24
|
+
/**
|
|
25
|
+
* Write a chunk of data to the file.
|
|
26
|
+
* @param fd The file descriptor.
|
|
27
|
+
* @param chunk The data to write.
|
|
28
|
+
*/
|
|
29
|
+
writeSync(fd: TFile, chunk: Uint8Array): void;
|
|
30
|
+
/**
|
|
31
|
+
* Flush the file to ensure that all data is written to the disk.
|
|
32
|
+
* @param fd The file descriptor.
|
|
33
|
+
*/
|
|
34
|
+
flushSync(fd: TFile): void;
|
|
35
|
+
/**
|
|
36
|
+
* Close the file.
|
|
37
|
+
* @param fd The file descriptor.
|
|
38
|
+
*/
|
|
39
|
+
closeSync(fd: TFile): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get a platform-independent file sink.
|
|
43
|
+
*
|
|
44
|
+
* @typeParam TFile The type of the file descriptor.
|
|
45
|
+
* @param path A path to the file to write to.
|
|
46
|
+
* @param options The options for the sink and the file driver.
|
|
47
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
48
|
+
* object that closes the file when disposed.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Options for the {@link getBaseRotatingFileSink} function.
|
|
53
|
+
*/
|
|
54
|
+
interface RotatingFileSinkOptions extends Omit<FileSinkOptions, "lazy"> {
|
|
55
|
+
/**
|
|
56
|
+
* The maximum bytes of the file before it is rotated. 1 MiB by default.
|
|
57
|
+
*/
|
|
58
|
+
maxSize?: number;
|
|
59
|
+
/**
|
|
60
|
+
* The maximum number of files to keep. 5 by default.
|
|
61
|
+
*/
|
|
62
|
+
maxFiles?: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A platform-specific rotating file sink driver.
|
|
66
|
+
*/
|
|
67
|
+
interface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {
|
|
68
|
+
/**
|
|
69
|
+
* Get the size of the file.
|
|
70
|
+
* @param path A path to the file.
|
|
71
|
+
* @returns The `size` of the file in bytes, in an object.
|
|
72
|
+
*/
|
|
73
|
+
statSync(path: string): {
|
|
74
|
+
size: number;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Rename a file.
|
|
78
|
+
* @param oldPath A path to the file to rename.
|
|
79
|
+
* @param newPath A path to be renamed to.
|
|
80
|
+
*/
|
|
81
|
+
renameSync(oldPath: string, newPath: string): void;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get a platform-independent rotating file sink.
|
|
85
|
+
*
|
|
86
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
87
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
88
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
89
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
90
|
+
*
|
|
91
|
+
* @param path A path to the file to write to.
|
|
92
|
+
* @param options The options for the sink and the file driver.
|
|
93
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
94
|
+
* object that closes the file when disposed.
|
|
95
|
+
*/
|
|
96
|
+
//#endregion
|
|
97
|
+
export { FileSinkDriver, FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions };
|
|
98
|
+
//# sourceMappingURL=filesink.base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.base.d.ts","names":[],"sources":["../filesink.base.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;AAWiB,KAXL,eAAA,GAAkB,iBAWC,GAAA;EAAA;;;EAYV,IAAS,CAAA,EAAA,OAAA;CAAU;;AAYnB;AAqCrB;;AAAsD,UA7DrC,cA6DqC,CAAA,KAAA,CAAA,CAAA;EAAe;AAAhB;AAerD;;EAAuC,QAA+B,CAAA,IAAA,EAAA,MAAA,CAAA,EAvE5C,KAuE4C;EAAK;AAAN;;;;gBAhErD,cAAc;;;;;gBAMd;;;;;gBAMA;;;;;;;;;;;;;;;UAqCC,uBAAA,SAAgC,KAAK;;;;;;;;;;;;;UAerC,sCAAsC,eAAe"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { defaultTextFormatter } from "@logtape/logtape";
|
|
2
|
+
|
|
3
|
+
//#region filesink.base.ts
|
|
4
|
+
/**
|
|
5
|
+
* Get a platform-independent file sink.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam TFile The type of the file descriptor.
|
|
8
|
+
* @param path A path to the file to write to.
|
|
9
|
+
* @param options The options for the sink and the file driver.
|
|
10
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
11
|
+
* object that closes the file when disposed.
|
|
12
|
+
*/
|
|
13
|
+
function getBaseFileSink(path, options) {
|
|
14
|
+
const formatter = options.formatter ?? defaultTextFormatter;
|
|
15
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
16
|
+
let fd = options.lazy ? null : options.openSync(path);
|
|
17
|
+
const sink = (record) => {
|
|
18
|
+
if (fd === null) fd = options.openSync(path);
|
|
19
|
+
options.writeSync(fd, encoder.encode(formatter(record)));
|
|
20
|
+
options.flushSync(fd);
|
|
21
|
+
};
|
|
22
|
+
sink[Symbol.dispose] = () => {
|
|
23
|
+
if (fd !== null) options.closeSync(fd);
|
|
24
|
+
};
|
|
25
|
+
return sink;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get a platform-independent rotating file sink.
|
|
29
|
+
*
|
|
30
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
31
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
32
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
33
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
34
|
+
*
|
|
35
|
+
* @param path A path to the file to write to.
|
|
36
|
+
* @param options The options for the sink and the file driver.
|
|
37
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
38
|
+
* object that closes the file when disposed.
|
|
39
|
+
*/
|
|
40
|
+
function getBaseRotatingFileSink(path, options) {
|
|
41
|
+
const formatter = options.formatter ?? defaultTextFormatter;
|
|
42
|
+
const encoder = options.encoder ?? new TextEncoder();
|
|
43
|
+
const maxSize = options.maxSize ?? 1024 * 1024;
|
|
44
|
+
const maxFiles = options.maxFiles ?? 5;
|
|
45
|
+
let offset = 0;
|
|
46
|
+
try {
|
|
47
|
+
const stat = options.statSync(path);
|
|
48
|
+
offset = stat.size;
|
|
49
|
+
} catch {}
|
|
50
|
+
let fd = options.openSync(path);
|
|
51
|
+
function shouldRollover(bytes) {
|
|
52
|
+
return offset + bytes.length > maxSize;
|
|
53
|
+
}
|
|
54
|
+
function performRollover() {
|
|
55
|
+
options.closeSync(fd);
|
|
56
|
+
for (let i = maxFiles - 1; i > 0; i--) {
|
|
57
|
+
const oldPath = `${path}.${i}`;
|
|
58
|
+
const newPath = `${path}.${i + 1}`;
|
|
59
|
+
try {
|
|
60
|
+
options.renameSync(oldPath, newPath);
|
|
61
|
+
} catch (_) {}
|
|
62
|
+
}
|
|
63
|
+
options.renameSync(path, `${path}.1`);
|
|
64
|
+
offset = 0;
|
|
65
|
+
fd = options.openSync(path);
|
|
66
|
+
}
|
|
67
|
+
const sink = (record) => {
|
|
68
|
+
const bytes = encoder.encode(formatter(record));
|
|
69
|
+
if (shouldRollover(bytes)) performRollover();
|
|
70
|
+
options.writeSync(fd, bytes);
|
|
71
|
+
options.flushSync(fd);
|
|
72
|
+
offset += bytes.length;
|
|
73
|
+
};
|
|
74
|
+
sink[Symbol.dispose] = () => options.closeSync(fd);
|
|
75
|
+
return sink;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
export { getBaseFileSink, getBaseRotatingFileSink };
|
|
80
|
+
//# sourceMappingURL=filesink.base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.base.js","names":["path: string","options: FileSinkOptions & FileSinkDriver<TFile>","sink: Sink & Disposable","record: LogRecord","options: RotatingFileSinkOptions & RotatingFileSinkDriver<TFile>","offset: number","bytes: Uint8Array"],"sources":["../filesink.base.ts"],"sourcesContent":["import {\n defaultTextFormatter,\n type LogRecord,\n type Sink,\n type StreamSinkOptions,\n} from \"@logtape/logtape\";\n\n/**\n * Options for the {@link getBaseFileSink} function.\n */\nexport type FileSinkOptions = StreamSinkOptions & {\n /**\n * If `true`, the file is not opened until the first write. Defaults to `false`.\n */\n lazy?: boolean;\n};\n\n/**\n * A platform-specific file sink driver.\n * @typeParam TFile The type of the file descriptor.\n */\nexport interface FileSinkDriver<TFile> {\n /**\n * Open a file for appending and return a file descriptor.\n * @param path A path to the file to open.\n */\n openSync(path: string): TFile;\n\n /**\n * Write a chunk of data to the file.\n * @param fd The file descriptor.\n * @param chunk The data to write.\n */\n writeSync(fd: TFile, chunk: Uint8Array): void;\n\n /**\n * Flush the file to ensure that all data is written to the disk.\n * @param fd The file descriptor.\n */\n flushSync(fd: TFile): void;\n\n /**\n * Close the file.\n * @param fd The file descriptor.\n */\n closeSync(fd: TFile): void;\n}\n\n/**\n * Get a platform-independent file sink.\n *\n * @typeParam TFile The type of the file descriptor.\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed.\n */\nexport function getBaseFileSink<TFile>(\n path: string,\n options: FileSinkOptions & FileSinkDriver<TFile>,\n): Sink & Disposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n let fd = options.lazy ? null : options.openSync(path);\n const sink: Sink & Disposable = (record: LogRecord) => {\n if (fd === null) {\n fd = options.openSync(path);\n }\n options.writeSync(fd, encoder.encode(formatter(record)));\n options.flushSync(fd);\n };\n sink[Symbol.dispose] = () => {\n if (fd !== null) {\n options.closeSync(fd);\n }\n };\n return sink;\n}\n\n/**\n * Options for the {@link getBaseRotatingFileSink} function.\n */\nexport interface RotatingFileSinkOptions extends Omit<FileSinkOptions, \"lazy\"> {\n /**\n * The maximum bytes of the file before it is rotated. 1 MiB by default.\n */\n maxSize?: number;\n\n /**\n * The maximum number of files to keep. 5 by default.\n */\n maxFiles?: number;\n}\n\n/**\n * A platform-specific rotating file sink driver.\n */\nexport interface RotatingFileSinkDriver<TFile> extends FileSinkDriver<TFile> {\n /**\n * Get the size of the file.\n * @param path A path to the file.\n * @returns The `size` of the file in bytes, in an object.\n */\n statSync(path: string): { size: number };\n\n /**\n * Rename a file.\n * @param oldPath A path to the file to rename.\n * @param newPath A path to be renamed to.\n */\n renameSync(oldPath: string, newPath: string): void;\n}\n\n/**\n * Get a platform-independent rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed.\n */\nexport function getBaseRotatingFileSink<TFile>(\n path: string,\n options: RotatingFileSinkOptions & RotatingFileSinkDriver<TFile>,\n): Sink & Disposable {\n const formatter = options.formatter ?? defaultTextFormatter;\n const encoder = options.encoder ?? new TextEncoder();\n const maxSize = options.maxSize ?? 1024 * 1024;\n const maxFiles = options.maxFiles ?? 5;\n let offset: number = 0;\n try {\n const stat = options.statSync(path);\n offset = stat.size;\n } catch {\n // Continue as the offset is already 0.\n }\n let fd = options.openSync(path);\n function shouldRollover(bytes: Uint8Array): boolean {\n return offset + bytes.length > maxSize;\n }\n function performRollover(): void {\n options.closeSync(fd);\n for (let i = maxFiles - 1; i > 0; i--) {\n const oldPath = `${path}.${i}`;\n const newPath = `${path}.${i + 1}`;\n try {\n options.renameSync(oldPath, newPath);\n } catch (_) {\n // Continue if the file does not exist.\n }\n }\n options.renameSync(path, `${path}.1`);\n offset = 0;\n fd = options.openSync(path);\n }\n const sink: Sink & Disposable = (record: LogRecord) => {\n const bytes = encoder.encode(formatter(record));\n if (shouldRollover(bytes)) performRollover();\n options.writeSync(fd, bytes);\n options.flushSync(fd);\n offset += bytes.length;\n };\n sink[Symbol.dispose] = () => options.closeSync(fd);\n return sink;\n}\n"],"mappings":";;;;;;;;;;;;AAyDA,SAAgB,gBACdA,MACAC,SACmB;CACnB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,IAAI,KAAK,QAAQ,OAAO,OAAO,QAAQ,SAAS,KAAK;CACrD,MAAMC,OAA0B,CAACC,WAAsB;AACrD,MAAI,OAAO,KACT,MAAK,QAAQ,SAAS,KAAK;AAE7B,UAAQ,UAAU,IAAI,QAAQ,OAAO,UAAU,OAAO,CAAC,CAAC;AACxD,UAAQ,UAAU,GAAG;CACtB;AACD,MAAK,OAAO,WAAW,MAAM;AAC3B,MAAI,OAAO,KACT,SAAQ,UAAU,GAAG;CAExB;AACD,QAAO;AACR;;;;;;;;;;;;;;AAiDD,SAAgB,wBACdH,MACAI,SACmB;CACnB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,UAAU,QAAQ,WAAW,IAAI;CACvC,MAAM,UAAU,QAAQ,WAAW,OAAO;CAC1C,MAAM,WAAW,QAAQ,YAAY;CACrC,IAAIC,SAAiB;AACrB,KAAI;EACF,MAAM,OAAO,QAAQ,SAAS,KAAK;AACnC,WAAS,KAAK;CACf,QAAO,CAEP;CACD,IAAI,KAAK,QAAQ,SAAS,KAAK;CAC/B,SAAS,eAAeC,OAA4B;AAClD,SAAO,SAAS,MAAM,SAAS;CAChC;CACD,SAAS,kBAAwB;AAC/B,UAAQ,UAAU,GAAG;AACrB,OAAK,IAAI,IAAI,WAAW,GAAG,IAAI,GAAG,KAAK;GACrC,MAAM,WAAW,EAAE,KAAK,GAAG,EAAE;GAC7B,MAAM,WAAW,EAAE,KAAK,GAAG,IAAI,EAAE;AACjC,OAAI;AACF,YAAQ,WAAW,SAAS,QAAQ;GACrC,SAAQ,GAAG,CAEX;EACF;AACD,UAAQ,WAAW,OAAO,EAAE,KAAK,IAAI;AACrC,WAAS;AACT,OAAK,QAAQ,SAAS,KAAK;CAC5B;CACD,MAAMJ,OAA0B,CAACC,WAAsB;EACrD,MAAM,QAAQ,QAAQ,OAAO,UAAU,OAAO,CAAC;AAC/C,MAAI,eAAe,MAAM,CAAE,kBAAiB;AAC5C,UAAQ,UAAU,IAAI,MAAM;AAC5B,UAAQ,UAAU,GAAG;AACrB,YAAU,MAAM;CACjB;AACD,MAAK,OAAO,WAAW,MAAM,QAAQ,UAAU,GAAG;AAClD,QAAO;AACR"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const require_filesink_base = require('./filesink.base.cjs');
|
|
2
|
+
|
|
3
|
+
//#region filesink.deno.ts
|
|
4
|
+
/**
|
|
5
|
+
* A Deno-specific file sink driver.
|
|
6
|
+
*/
|
|
7
|
+
const denoDriver = {
|
|
8
|
+
openSync(path) {
|
|
9
|
+
return Deno.openSync(path, {
|
|
10
|
+
create: true,
|
|
11
|
+
append: true
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
writeSync(fd, chunk) {
|
|
15
|
+
fd.writeSync(chunk);
|
|
16
|
+
},
|
|
17
|
+
flushSync(fd) {
|
|
18
|
+
fd.syncSync();
|
|
19
|
+
},
|
|
20
|
+
closeSync(fd) {
|
|
21
|
+
fd.close();
|
|
22
|
+
},
|
|
23
|
+
statSync: globalThis?.Deno.statSync,
|
|
24
|
+
renameSync: globalThis?.Deno.renameSync
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Get a file sink.
|
|
28
|
+
*
|
|
29
|
+
* Note that this function is unavailable in the browser.
|
|
30
|
+
*
|
|
31
|
+
* @param path A path to the file to write to.
|
|
32
|
+
* @param options The options for the sink.
|
|
33
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
34
|
+
* object that closes the file when disposed.
|
|
35
|
+
*/
|
|
36
|
+
function getFileSink(path, options = {}) {
|
|
37
|
+
return require_filesink_base.getBaseFileSink(path, {
|
|
38
|
+
...options,
|
|
39
|
+
...denoDriver
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get a rotating file sink.
|
|
44
|
+
*
|
|
45
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
46
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
47
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
48
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
49
|
+
*
|
|
50
|
+
* Note that this function is unavailable in the browser.
|
|
51
|
+
*
|
|
52
|
+
* @param path A path to the file to write to.
|
|
53
|
+
* @param options The options for the sink and the file driver.
|
|
54
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
55
|
+
* object that closes the file when disposed.
|
|
56
|
+
*/
|
|
57
|
+
function getRotatingFileSink(path, options = {}) {
|
|
58
|
+
return require_filesink_base.getBaseRotatingFileSink(path, {
|
|
59
|
+
...options,
|
|
60
|
+
...denoDriver
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.denoDriver = denoDriver;
|
|
66
|
+
exports.getFileSink = getFileSink;
|
|
67
|
+
exports.getRotatingFileSink = getRotatingFileSink;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from "./filesink.base.cjs";
|
|
2
|
+
import { Sink } from "@logtape/logtape";
|
|
3
|
+
|
|
4
|
+
//#region filesink.deno.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A Deno-specific file sink driver.
|
|
8
|
+
*/
|
|
9
|
+
declare const denoDriver: RotatingFileSinkDriver<Deno.FsFile>;
|
|
10
|
+
/**
|
|
11
|
+
* Get a file sink.
|
|
12
|
+
*
|
|
13
|
+
* Note that this function is unavailable in the browser.
|
|
14
|
+
*
|
|
15
|
+
* @param path A path to the file to write to.
|
|
16
|
+
* @param options The options for the sink.
|
|
17
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
18
|
+
* object that closes the file when disposed.
|
|
19
|
+
*/
|
|
20
|
+
declare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;
|
|
21
|
+
/**
|
|
22
|
+
* Get a rotating file sink.
|
|
23
|
+
*
|
|
24
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
25
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
26
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
27
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
28
|
+
*
|
|
29
|
+
* Note that this function is unavailable in the browser.
|
|
30
|
+
*
|
|
31
|
+
* @param path A path to the file to write to.
|
|
32
|
+
* @param options The options for the sink and the file driver.
|
|
33
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
34
|
+
* object that closes the file when disposed.
|
|
35
|
+
*/
|
|
36
|
+
declare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;
|
|
37
|
+
//# sourceMappingURL=filesink.deno.d.ts.map
|
|
38
|
+
//#endregion
|
|
39
|
+
export { denoDriver, getFileSink, getRotatingFileSink };
|
|
40
|
+
//# sourceMappingURL=filesink.deno.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.deno.d.cts","names":[],"sources":["../filesink.deno.ts"],"sourcesContent":[],"mappings":";;;;;;;AAYA;AAeC,cAfY,UAeZ,EAfwB,sBAexB,CAf+C,IAAA,CAAK,MAepD,CAAA;;;AAf8C;AA2B/C;;;;;AAGoB;AAmBpB;AAAmC,iBAtBnB,WAAA,CAsBmB,IAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EApBxB,eAoBwB,CAAA,EAnBhC,IAmBgC,GAnBzB,UAmByB;;;;AAGf;;;;;;;;;;;;iBAHJ,mBAAA,yBAEL,0BACR,OAAO"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { FileSinkOptions, RotatingFileSinkDriver, RotatingFileSinkOptions } from "./filesink.base.js";
|
|
2
|
+
import { Sink } from "@logtape/logtape";
|
|
3
|
+
|
|
4
|
+
//#region filesink.deno.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A Deno-specific file sink driver.
|
|
8
|
+
*/
|
|
9
|
+
declare const denoDriver: RotatingFileSinkDriver<Deno.FsFile>;
|
|
10
|
+
/**
|
|
11
|
+
* Get a file sink.
|
|
12
|
+
*
|
|
13
|
+
* Note that this function is unavailable in the browser.
|
|
14
|
+
*
|
|
15
|
+
* @param path A path to the file to write to.
|
|
16
|
+
* @param options The options for the sink.
|
|
17
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
18
|
+
* object that closes the file when disposed.
|
|
19
|
+
*/
|
|
20
|
+
declare function getFileSink(path: string, options?: FileSinkOptions): Sink & Disposable;
|
|
21
|
+
/**
|
|
22
|
+
* Get a rotating file sink.
|
|
23
|
+
*
|
|
24
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
25
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
26
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
27
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
28
|
+
*
|
|
29
|
+
* Note that this function is unavailable in the browser.
|
|
30
|
+
*
|
|
31
|
+
* @param path A path to the file to write to.
|
|
32
|
+
* @param options The options for the sink and the file driver.
|
|
33
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
34
|
+
* object that closes the file when disposed.
|
|
35
|
+
*/
|
|
36
|
+
declare function getRotatingFileSink(path: string, options?: RotatingFileSinkOptions): Sink & Disposable;
|
|
37
|
+
//# sourceMappingURL=filesink.deno.d.ts.map
|
|
38
|
+
//#endregion
|
|
39
|
+
export { denoDriver, getFileSink, getRotatingFileSink };
|
|
40
|
+
//# sourceMappingURL=filesink.deno.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.deno.d.ts","names":[],"sources":["../filesink.deno.ts"],"sourcesContent":[],"mappings":";;;;;;;AAYA;AAeC,cAfY,UAeZ,EAfwB,sBAexB,CAf+C,IAAA,CAAK,MAepD,CAAA;;;AAf8C;AA2B/C;;;;;AAGoB;AAmBpB;AAAmC,iBAtBnB,WAAA,CAsBmB,IAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EApBxB,eAoBwB,CAAA,EAnBhC,IAmBgC,GAnBzB,UAmByB;;;;AAGf;;;;;;;;;;;;iBAHJ,mBAAA,yBAEL,0BACR,OAAO"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { getBaseFileSink, getBaseRotatingFileSink } from "./filesink.base.js";
|
|
2
|
+
|
|
3
|
+
//#region filesink.deno.ts
|
|
4
|
+
/**
|
|
5
|
+
* A Deno-specific file sink driver.
|
|
6
|
+
*/
|
|
7
|
+
const denoDriver = {
|
|
8
|
+
openSync(path) {
|
|
9
|
+
return Deno.openSync(path, {
|
|
10
|
+
create: true,
|
|
11
|
+
append: true
|
|
12
|
+
});
|
|
13
|
+
},
|
|
14
|
+
writeSync(fd, chunk) {
|
|
15
|
+
fd.writeSync(chunk);
|
|
16
|
+
},
|
|
17
|
+
flushSync(fd) {
|
|
18
|
+
fd.syncSync();
|
|
19
|
+
},
|
|
20
|
+
closeSync(fd) {
|
|
21
|
+
fd.close();
|
|
22
|
+
},
|
|
23
|
+
statSync: globalThis?.Deno.statSync,
|
|
24
|
+
renameSync: globalThis?.Deno.renameSync
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Get a file sink.
|
|
28
|
+
*
|
|
29
|
+
* Note that this function is unavailable in the browser.
|
|
30
|
+
*
|
|
31
|
+
* @param path A path to the file to write to.
|
|
32
|
+
* @param options The options for the sink.
|
|
33
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
34
|
+
* object that closes the file when disposed.
|
|
35
|
+
*/
|
|
36
|
+
function getFileSink(path, options = {}) {
|
|
37
|
+
return getBaseFileSink(path, {
|
|
38
|
+
...options,
|
|
39
|
+
...denoDriver
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get a rotating file sink.
|
|
44
|
+
*
|
|
45
|
+
* This sink writes log records to a file, and rotates the file when it reaches
|
|
46
|
+
* the `maxSize`. The rotated files are named with the original file name
|
|
47
|
+
* followed by a dot and a number, starting from 1. The number is incremented
|
|
48
|
+
* for each rotation, and the maximum number of files to keep is `maxFiles`.
|
|
49
|
+
*
|
|
50
|
+
* Note that this function is unavailable in the browser.
|
|
51
|
+
*
|
|
52
|
+
* @param path A path to the file to write to.
|
|
53
|
+
* @param options The options for the sink and the file driver.
|
|
54
|
+
* @returns A sink that writes to the file. The sink is also a disposable
|
|
55
|
+
* object that closes the file when disposed.
|
|
56
|
+
*/
|
|
57
|
+
function getRotatingFileSink(path, options = {}) {
|
|
58
|
+
return getBaseRotatingFileSink(path, {
|
|
59
|
+
...options,
|
|
60
|
+
...denoDriver
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
export { denoDriver, getFileSink, getRotatingFileSink };
|
|
66
|
+
//# sourceMappingURL=filesink.deno.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesink.deno.js","names":["denoDriver: RotatingFileSinkDriver<Deno.FsFile>","path: string","options: FileSinkOptions","options: RotatingFileSinkOptions"],"sources":["../filesink.deno.ts"],"sourcesContent":["import type { Sink } from \"@logtape/logtape\";\nimport {\n type FileSinkOptions,\n getBaseFileSink,\n getBaseRotatingFileSink,\n type RotatingFileSinkDriver,\n type RotatingFileSinkOptions,\n} from \"./filesink.base.ts\";\n\n/**\n * A Deno-specific file sink driver.\n */\nexport const denoDriver: RotatingFileSinkDriver<Deno.FsFile> = {\n openSync(path: string) {\n return Deno.openSync(path, { create: true, append: true });\n },\n writeSync(fd, chunk) {\n fd.writeSync(chunk);\n },\n flushSync(fd) {\n fd.syncSync();\n },\n closeSync(fd) {\n fd.close();\n },\n statSync: globalThis?.Deno.statSync,\n renameSync: globalThis?.Deno.renameSync,\n};\n\n/**\n * Get a file sink.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed.\n */\nexport function getFileSink(\n path: string,\n options: FileSinkOptions = {},\n): Sink & Disposable {\n return getBaseFileSink(path, { ...options, ...denoDriver });\n}\n\n/**\n * Get a rotating file sink.\n *\n * This sink writes log records to a file, and rotates the file when it reaches\n * the `maxSize`. The rotated files are named with the original file name\n * followed by a dot and a number, starting from 1. The number is incremented\n * for each rotation, and the maximum number of files to keep is `maxFiles`.\n *\n * Note that this function is unavailable in the browser.\n *\n * @param path A path to the file to write to.\n * @param options The options for the sink and the file driver.\n * @returns A sink that writes to the file. The sink is also a disposable\n * object that closes the file when disposed.\n */\nexport function getRotatingFileSink(\n path: string,\n options: RotatingFileSinkOptions = {},\n): Sink & Disposable {\n return getBaseRotatingFileSink(path, { ...options, ...denoDriver });\n}\n\n// cSpell: ignore filesink\n"],"mappings":";;;;;;AAYA,MAAaA,aAAkD;CAC7D,SAASC,MAAc;AACrB,SAAO,KAAK,SAAS,MAAM;GAAE,QAAQ;GAAM,QAAQ;EAAM,EAAC;CAC3D;CACD,UAAU,IAAI,OAAO;AACnB,KAAG,UAAU,MAAM;CACpB;CACD,UAAU,IAAI;AACZ,KAAG,UAAU;CACd;CACD,UAAU,IAAI;AACZ,KAAG,OAAO;CACX;CACD,UAAU,YAAY,KAAK;CAC3B,YAAY,YAAY,KAAK;AAC9B;;;;;;;;;;;AAYD,SAAgB,YACdA,MACAC,UAA2B,CAAE,GACV;AACnB,QAAO,gBAAgB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAY,EAAC;AAC5D;;;;;;;;;;;;;;;;AAiBD,SAAgB,oBACdD,MACAE,UAAmC,CAAE,GAClB;AACnB,QAAO,wBAAwB,MAAM;EAAE,GAAG;EAAS,GAAG;CAAY,EAAC;AACpE"}
|