重构tracer设计
This commit is contained in:
44
tracers.ts/src/tracers/log-tracer.ts
Normal file
44
tracers.ts/src/tracers/log-tracer.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getTracerContext } from '../context';
|
||||
|
||||
interface LogTracerCreateOptions {
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export const createLogTracer = (options: LogTracerCreateOptions) => {
|
||||
const { description = 'LogTracer' } = options;
|
||||
const tracer = crypto.randomUUID();
|
||||
|
||||
const { command } = getTracerContext();
|
||||
|
||||
command({
|
||||
type: 'LogTracer',
|
||||
tracer: tracer,
|
||||
action: 'create',
|
||||
params: {
|
||||
description: description,
|
||||
},
|
||||
});
|
||||
|
||||
const log = (...args: unknown[]) => {
|
||||
const parsed = args.map((arg) => {
|
||||
if (typeof arg === 'string') {
|
||||
return arg;
|
||||
}
|
||||
return JSON.stringify(arg);
|
||||
});
|
||||
const message = parsed.join(' ');
|
||||
|
||||
command({
|
||||
type: 'LogTracer',
|
||||
tracer: tracer,
|
||||
action: 'log',
|
||||
params: {
|
||||
message: message,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
log,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user