@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.1
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/LICENSE +24 -0
- package/README.md +212 -2
- package/bin/sft.js +22 -0
- package/dist/cli.js +35 -0
- package/dist/commands/admin.js +46 -0
- package/dist/commands/check.js +20 -0
- package/dist/commands/ci.js +123 -0
- package/dist/commands/deploy.js +31 -0
- package/dist/commands/doctor.js +11 -0
- package/dist/commands/dryrun.js +17 -0
- package/dist/commands/hook.js +46 -0
- package/dist/commands/index.js +36 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +40 -0
- package/dist/commands/log.js +58 -0
- package/dist/commands/metasync.js +21 -0
- package/dist/commands/pr.js +32 -0
- package/dist/commands/project.js +93 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +19 -0
- package/dist/commands/switch-org.js +12 -0
- package/dist/commands/update-cli.js +11 -0
- package/dist/commands/version.js +18 -0
- package/dist/core/admin-clone.js +79 -0
- package/dist/core/ai-answers.js +40 -0
- package/dist/core/branches.js +88 -0
- package/dist/core/check-targets.js +170 -0
- package/dist/core/check.js +34 -0
- package/dist/core/ci-check-branch.js +150 -0
- package/dist/core/ci-login-steps.js +73 -0
- package/dist/core/ci-release-steps.js +193 -0
- package/dist/core/ci-sequence-steps.js +188 -0
- package/dist/core/ci-validate-steps.js +140 -0
- package/dist/core/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +314 -0
- package/dist/core/create-state.js +82 -0
- package/dist/core/delegate.js +45 -0
- package/dist/core/deploy.js +66 -0
- package/dist/core/diag.js +200 -0
- package/dist/core/doctor.js +211 -0
- package/dist/core/eca.js +259 -0
- package/dist/core/exit-codes.js +12 -0
- package/dist/core/gh-identity.js +46 -0
- package/dist/core/hook-install.js +130 -0
- package/dist/core/hook.js +116 -0
- package/dist/core/identity.js +48 -0
- package/dist/core/job.js +243 -0
- package/dist/core/launcher-status.js +184 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +85 -0
- package/dist/core/log-collector.js +370 -0
- package/dist/core/log-format.js +44 -0
- package/dist/core/log-pull.js +565 -0
- package/dist/core/log-select.js +126 -0
- package/dist/core/log-session.js +362 -0
- package/dist/core/log-start.js +421 -0
- package/dist/core/log-status.js +223 -0
- package/dist/core/log-summarize.js +127 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/login-url.js +50 -0
- package/dist/core/manifest-build.js +118 -0
- package/dist/core/mask.js +70 -0
- package/dist/core/metadata-types.js +79 -0
- package/dist/core/metasync.js +684 -0
- package/dist/core/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +199 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +235 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +368 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +152 -0
- package/dist/core/release.js +256 -0
- package/dist/core/scoped-box.js +29 -0
- package/dist/core/start.js +155 -0
- package/dist/core/switch-org.js +49 -0
- package/dist/core/test-types.js +212 -0
- package/dist/core/toolbox.js +4 -0
- package/dist/core/update-cli.js +156 -0
- package/dist/core/update-secret.js +301 -0
- package/dist/core/update-workflows.js +132 -0
- package/dist/core/verify-gh.js +286 -0
- package/dist/core/verify-samples.js +152 -0
- package/dist/core/windows-shim.js +21 -0
- package/dist/io/capability-file.js +11 -0
- package/dist/io/create-deps.js +68 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +31 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +161 -0
- package/dist/io/log-deps.js +106 -0
- package/dist/io/metasync-deps.js +21 -0
- package/dist/io/open-browser.js +21 -0
- package/dist/io/package-version.js +10 -0
- package/dist/io/pinned-project.js +111 -0
- package/dist/io/promote-deps.js +27 -0
- package/dist/io/push-refs.js +20 -0
- package/dist/io/resolve.js +74 -0
- package/dist/io/run.js +201 -0
- package/dist/io/switch-org-deps.js +10 -0
- package/dist/io/temp-dir.js +10 -0
- package/dist/io/tool-versions.js +14 -0
- package/dist/io/toolbox.js +68 -0
- package/dist/io/update-cli-deps.js +75 -0
- package/dist/io/update-secret-deps.js +27 -0
- package/dist/launch.js +152 -0
- package/dist/ui/confirm.js +18 -0
- package/dist/ui/launcher.js +132 -0
- package/dist/ui/prompt.js +66 -0
- package/package.json +37 -4
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { isReservedOrgAlias } from "./branches.js";
|
|
2
|
+
import { fmtZ, note, otherHandler, parseTime, RUNTIME_SELF, read, stateSet } from "./log-pull.js";
|
|
3
|
+
import { ID_RE, indexMark, kvGet, parseIndex } from "./log-select.js";
|
|
4
|
+
import { parseTraceFlags, readOrgUser } from "./log-status.js";
|
|
5
|
+
import { getTargetOrg, orgLocalReleaseCheck } from "./org.js";
|
|
6
|
+
const APEX = "logs/apex";
|
|
7
|
+
const STATE_F = `${APEX}/.state`;
|
|
8
|
+
const STATUS_F = `${APEX}/.status`;
|
|
9
|
+
const INDEX_F = `${APEX}/.index`;
|
|
10
|
+
const OWNERS_F = `${APEX}/.owners`;
|
|
11
|
+
const CLAIM_F = `${APEX}/.claim`;
|
|
12
|
+
const ORG_LOG_WARN_BYTES = 700 * 1024 * 1024;
|
|
13
|
+
export const NOTICE_TEXT = "ログには、レコードの値・SOQL の条件・コールアウトの内容が載ります。ログは AI に読ませる前提で、読ませると AI の提供元に送られます。ダミーデータの組織で使ってください。本番のデータを写した Sandbox(Full・Partial)では、使わないでください。";
|
|
14
|
+
const LEVELS = {
|
|
15
|
+
standard: "ApexCode=FINE ApexProfiling=INFO Workflow=FINE Database=INFO Validation=INFO Callout=INFO System=DEBUG Visualforce=INFO",
|
|
16
|
+
detail: "ApexCode=FINEST ApexProfiling=FINE Workflow=FINER Database=FINEST Validation=INFO Callout=FINEST System=DEBUG Visualforce=FINE",
|
|
17
|
+
};
|
|
18
|
+
/** 止める理由と終了コード(自動の取り始めでは、10 = 取らない判断・1 = 失敗) */
|
|
19
|
+
export class Stop extends Error {
|
|
20
|
+
code;
|
|
21
|
+
constructor(code, message) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function sf(box, args) {
|
|
27
|
+
const r = await box.run("sf", args).catch(() => null);
|
|
28
|
+
let json = {};
|
|
29
|
+
try {
|
|
30
|
+
json = JSON.parse(r?.stdout ?? "");
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
json = {};
|
|
34
|
+
}
|
|
35
|
+
return { ok: r !== null && r.code === 0, json };
|
|
36
|
+
}
|
|
37
|
+
export const errName = (j) => (typeof j.name === "string" && j.name !== "" ? j.name : "不明");
|
|
38
|
+
/** 消そうとしたものが、すでにない(sf-log.sh の sf_err_is_gone と同じ) */
|
|
39
|
+
export const isGone = (name) => /NOT_FOUND|ENTITY_IS_DELETED|INVALID_CROSS_REFERENCE_KEY/.test(name);
|
|
40
|
+
async function confirm(ask, name, question) {
|
|
41
|
+
const a = await ask(name, question);
|
|
42
|
+
if (a === "need")
|
|
43
|
+
throw new Stop(20, "");
|
|
44
|
+
if (a !== "yes")
|
|
45
|
+
throw new Stop(1, "中断しました。");
|
|
46
|
+
}
|
|
47
|
+
/** DebugLevel を探し、上書きするか作る。その ID */
|
|
48
|
+
export async function ensureDebugLevel(box, org, level) {
|
|
49
|
+
const name = `sf_tools_log_${level}`;
|
|
50
|
+
const base = ["--use-tooling-api", "--target-org", org, "--json"];
|
|
51
|
+
const q = await sf(box, ["data", "query", ...base, "--query", `SELECT Id FROM DebugLevel WHERE DeveloperName = '${name}'`]);
|
|
52
|
+
if (!q.ok)
|
|
53
|
+
throw new Stop(1, `DebugLevel を検索できません(${errName(q.json)})。`);
|
|
54
|
+
const total = q.json.result?.totalSize;
|
|
55
|
+
let id = "";
|
|
56
|
+
if (total === 0) {
|
|
57
|
+
const c = await sf(box, [
|
|
58
|
+
"data",
|
|
59
|
+
"create",
|
|
60
|
+
"record",
|
|
61
|
+
"--sobject",
|
|
62
|
+
"DebugLevel",
|
|
63
|
+
...base,
|
|
64
|
+
"--values",
|
|
65
|
+
`DeveloperName=${name} MasterLabel=${name} ${LEVELS[level]}`,
|
|
66
|
+
]);
|
|
67
|
+
if (!c.ok)
|
|
68
|
+
throw new Stop(1, `DebugLevel を作れません(${errName(c.json)})。`);
|
|
69
|
+
id = String(c.json.result?.id ?? "");
|
|
70
|
+
}
|
|
71
|
+
else if (total === 1) {
|
|
72
|
+
const recs = q.json.result?.records;
|
|
73
|
+
id = String(recs?.[0]?.Id ?? "");
|
|
74
|
+
if (!ID_RE.test(id))
|
|
75
|
+
throw new Stop(1, "DebugLevel の ID の形が正しくありません。");
|
|
76
|
+
const u = await sf(box, ["data", "update", "record", "--sobject", "DebugLevel", "--record-id", id, ...base, "--values", LEVELS[level]]);
|
|
77
|
+
if (!u.ok)
|
|
78
|
+
throw new Stop(1, "DebugLevel を上書きできません。");
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new Stop(1, `DebugLevel「${name}」が 1 つに決まりません(${String(total ?? "不明")} 件)。`);
|
|
82
|
+
}
|
|
83
|
+
if (!ID_RE.test(id))
|
|
84
|
+
throw new Stop(1, "DebugLevel の ID の形が正しくありません。");
|
|
85
|
+
return id;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* TraceFlag を延ばすか作る。その ID と期限。
|
|
89
|
+
* auto(sft の起動からの自動の取り始め): ほかの有効な USER_DEBUG が 1 つなら、sf-tools のもの(DebugLevel が sf_tools_log_*)は
|
|
90
|
+
* 消して引き継ぐ(後から起動したほうを優先する。消せなければ作らない)。それ以外(人・ほかの道具の設定)は、触らずに 10 で止める
|
|
91
|
+
*/
|
|
92
|
+
export async function ensureTraceFlag(box, ask, o) {
|
|
93
|
+
const base = ["--use-tooling-api", "--target-org", o.org, "--json"];
|
|
94
|
+
const nowMs = box.clock.now().getTime();
|
|
95
|
+
const q = await box
|
|
96
|
+
.run("sf", [
|
|
97
|
+
"data",
|
|
98
|
+
"query",
|
|
99
|
+
...base,
|
|
100
|
+
"--query",
|
|
101
|
+
`SELECT Id, LogType, StartDate, ExpirationDate, DebugLevel.DeveloperName FROM TraceFlag WHERE TracedEntityId = '${o.userId}'`,
|
|
102
|
+
])
|
|
103
|
+
.catch(() => null);
|
|
104
|
+
if (q === null || q.code !== 0)
|
|
105
|
+
throw new Stop(1, "TraceFlag を検索できません。");
|
|
106
|
+
const flags = parseTraceFlags(q.stdout, nowMs);
|
|
107
|
+
if (flags === null)
|
|
108
|
+
throw new Stop(1, "TraceFlag の検索の応答の形が正しくありません。");
|
|
109
|
+
const mineId = kvGet(read(box, STATE_F), "TRACE_FLAG_ID");
|
|
110
|
+
const mine = ID_RE.test(mineId) && flags.some((f) => f.id === mineId && f.type === "USER_DEBUG");
|
|
111
|
+
const others = flags.filter((f) => f.id !== mineId && f.type === "USER_DEBUG" && f.state === "active");
|
|
112
|
+
const hoursMs = o.hours * 3600_000;
|
|
113
|
+
// a. 自分が作ったもの → 延ばす(ほかに有効な USER_DEBUG があれば、延ばさない。期間が重なって失敗するため)
|
|
114
|
+
if (mine && others.length === 0) {
|
|
115
|
+
const expires = fmtZ(nowMs + hoursMs);
|
|
116
|
+
const u = await sf(box, [
|
|
117
|
+
"data",
|
|
118
|
+
"update",
|
|
119
|
+
"record",
|
|
120
|
+
"--sobject",
|
|
121
|
+
"TraceFlag",
|
|
122
|
+
"--record-id",
|
|
123
|
+
mineId,
|
|
124
|
+
...base,
|
|
125
|
+
"--values",
|
|
126
|
+
`DebugLevelId=${o.dlId} StartDate=${fmtZ(nowMs)} ExpirationDate=${expires}`,
|
|
127
|
+
]);
|
|
128
|
+
if (!u.ok)
|
|
129
|
+
throw new Stop(1, `TraceFlag を延ばせません(${errName(u.json)})。`);
|
|
130
|
+
return { id: mineId, expires };
|
|
131
|
+
}
|
|
132
|
+
// b. ほかの有効な USER_DEBUG
|
|
133
|
+
let start = fmtZ(nowMs);
|
|
134
|
+
let shrunk = "";
|
|
135
|
+
if (others.length >= 2)
|
|
136
|
+
throw new Stop(1, `このユーザーに、有効な USER_DEBUG の TraceFlag が ${others.length} つあります。`);
|
|
137
|
+
const other = others[0];
|
|
138
|
+
if (other && o.auto) {
|
|
139
|
+
if (other.level !== "sf_tools_log_standard" && other.level !== "sf_tools_log_detail") {
|
|
140
|
+
box.output.err(`[WARNING] ほかの USER_DEBUG の TraceFlag(${other.id}・期限 ${other.expires})があります。sft log start で置き換えられます。`);
|
|
141
|
+
throw new Stop(10, `sf-tools 以外のログ設定があるため。期限 ${localHm(other.expires)}。置き換えは sft log start`);
|
|
142
|
+
}
|
|
143
|
+
// 準備のあいだに sft が終了していたら、引き継がない(使う人がいないのに、相手の取得を止めないため)
|
|
144
|
+
if (!o.auto.ownerPresent())
|
|
145
|
+
throw new Stop(10, "sft が終了したため");
|
|
146
|
+
const d = await sf(box, ["data", "delete", "record", "--sobject", "TraceFlag", "--record-id", other.id, ...base]);
|
|
147
|
+
if (!d.ok && !isGone(errName(d.json)))
|
|
148
|
+
throw new Stop(1, `別の sft・sf-start のログ設定を消せないため、引き継げません(${errName(d.json)})`);
|
|
149
|
+
box.output.err(`[WARNING] 別の場所の sft・sf-start のログ設定(${other.id}・期限 ${other.expires})を引き継ぎました。あちらの取得は止まります。`);
|
|
150
|
+
}
|
|
151
|
+
else if (other) {
|
|
152
|
+
const err = (l) => box.output.err(l);
|
|
153
|
+
err("[WARNING] このユーザーに、ほかの USER_DEBUG の TraceFlag があります(設定画面などで作られた、このユーザーのログの設定です)。");
|
|
154
|
+
err(`[WARNING] ID: ${other.id}・期限: ${other.expires}`);
|
|
155
|
+
err("[WARNING] 止めると、期限を「元の期限」と「今から 2 分後」の早いほうに縮めます(消しません)。");
|
|
156
|
+
if (!o.yes)
|
|
157
|
+
await confirm(ask, "log.replace", "この TraceFlag を止めて、sft log の TraceFlag を作りますか?");
|
|
158
|
+
const otherMs = parseTime(other.expires) ?? nowMs;
|
|
159
|
+
const newMs = Math.min(otherMs, box.clock.now().getTime() + 120_000);
|
|
160
|
+
const newExp = fmtZ(newMs);
|
|
161
|
+
if (newExp !== other.expires) {
|
|
162
|
+
const u = await sf(box, [
|
|
163
|
+
"data",
|
|
164
|
+
"update",
|
|
165
|
+
"record",
|
|
166
|
+
"--sobject",
|
|
167
|
+
"TraceFlag",
|
|
168
|
+
"--record-id",
|
|
169
|
+
other.id,
|
|
170
|
+
...base,
|
|
171
|
+
"--values",
|
|
172
|
+
`ExpirationDate=${newExp}`,
|
|
173
|
+
]);
|
|
174
|
+
if (!u.ok)
|
|
175
|
+
throw new Stop(1, "ほかの TraceFlag の期限を縮められません(新しい TraceFlag は作っていません)。");
|
|
176
|
+
shrunk = `${other.id}(元の期限 ${other.expires} → ${newExp})`;
|
|
177
|
+
}
|
|
178
|
+
start = fmtZ(Math.max(newMs + 1000, box.clock.now().getTime()));
|
|
179
|
+
}
|
|
180
|
+
// c. 作る
|
|
181
|
+
const expires = fmtZ(parseTime(start) + hoursMs);
|
|
182
|
+
const c = await sf(box, [
|
|
183
|
+
"data",
|
|
184
|
+
"create",
|
|
185
|
+
"record",
|
|
186
|
+
"--sobject",
|
|
187
|
+
"TraceFlag",
|
|
188
|
+
...base,
|
|
189
|
+
"--values",
|
|
190
|
+
`TracedEntityId=${o.userId} LogType=USER_DEBUG DebugLevelId=${o.dlId} StartDate=${start} ExpirationDate=${expires}`,
|
|
191
|
+
]);
|
|
192
|
+
if (!c.ok) {
|
|
193
|
+
if (shrunk !== "")
|
|
194
|
+
box.output.err(`[ERROR] 縮めた TraceFlag は、元に戻していません: ${shrunk}`);
|
|
195
|
+
throw new Stop(1, `TraceFlag を作れません(${errName(c.json)})。`);
|
|
196
|
+
}
|
|
197
|
+
const id = String(c.json.result?.id ?? "");
|
|
198
|
+
if (!ID_RE.test(id))
|
|
199
|
+
throw new Stop(1, "作った TraceFlag の ID の形が正しくありません。");
|
|
200
|
+
return { id, expires, tookOver: other !== undefined && o.auto !== undefined };
|
|
201
|
+
}
|
|
202
|
+
export function localHm(iso) {
|
|
203
|
+
const d = new Date(parseTime(iso) ?? 0);
|
|
204
|
+
return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
205
|
+
}
|
|
206
|
+
/** 今日(PC のタイムゾーン)のフォルダにあるログの本数 */
|
|
207
|
+
export function countToday(box) {
|
|
208
|
+
const d = box.clock.now();
|
|
209
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
210
|
+
return box.files.listDir(`${APEX}/${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}`).filter((n) => n.endsWith(".log")).length;
|
|
211
|
+
}
|
|
212
|
+
function parseOrgOption(args, i) {
|
|
213
|
+
const v = args[i + 1] ?? "";
|
|
214
|
+
if (v === "" || v.startsWith("-"))
|
|
215
|
+
throw new Stop(1, "-o には、組織のエイリアスを指定してください。");
|
|
216
|
+
return v;
|
|
217
|
+
}
|
|
218
|
+
async function prepareOrg(box, orgArg) {
|
|
219
|
+
const org = await getTargetOrg(box, orgArg);
|
|
220
|
+
if (org === null)
|
|
221
|
+
throw new Stop(1, "接続先の組織エイリアスを特定できません。");
|
|
222
|
+
if (isReservedOrgAlias(org))
|
|
223
|
+
throw new Stop(1, `${org} は共有環境(予約名)のため、ログを取りません。`);
|
|
224
|
+
const kind = await orgLocalReleaseCheck(box, org);
|
|
225
|
+
if (kind === 1)
|
|
226
|
+
throw new Stop(1, `${org} は本番組織のため、ログを取りません。`);
|
|
227
|
+
if (kind === 2)
|
|
228
|
+
throw new Stop(1, `${org} の組織の種類を確認できないため、ログを取りません。`);
|
|
229
|
+
const user = await readOrgUser(box, org);
|
|
230
|
+
if (typeof user === "string")
|
|
231
|
+
throw new Stop(1, `ユーザーの情報を取得できません(${user})。`);
|
|
232
|
+
const other = otherHandler(box);
|
|
233
|
+
if (other !== null)
|
|
234
|
+
throw new Stop(1, `${other}。そちらで操作してください(sf-log.sh)。`);
|
|
235
|
+
return { org, userId: user.userId };
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* TraceFlag を延ばした・作ったあと: 組織を切り替えていたら前の組織の TraceFlag を消し、.state・.status を書き、組織のログの量を表示する。
|
|
239
|
+
* state: 取り始める前の .state
|
|
240
|
+
*/
|
|
241
|
+
export async function recordStart(box, deps, o) {
|
|
242
|
+
const { org, userId, state, tf } = o;
|
|
243
|
+
const ctx = { box, deps };
|
|
244
|
+
const prevOrg = kvGet(state, "ORG");
|
|
245
|
+
const prevUser = kvGet(state, "USER_ID");
|
|
246
|
+
const prevTf = kvGet(state, "TRACE_FLAG_ID");
|
|
247
|
+
// 組織を切り替えた: 前の組織の、自分が作った TraceFlag を消す(消せなければ、最大 2 時間で切れる)
|
|
248
|
+
if (prevOrg !== "" && prevOrg !== org && ID_RE.test(prevTf)) {
|
|
249
|
+
const d = await sf(box, [
|
|
250
|
+
"data",
|
|
251
|
+
"delete",
|
|
252
|
+
"record",
|
|
253
|
+
"--use-tooling-api",
|
|
254
|
+
"--sobject",
|
|
255
|
+
"TraceFlag",
|
|
256
|
+
"--record-id",
|
|
257
|
+
prevTf,
|
|
258
|
+
"--target-org",
|
|
259
|
+
prevOrg,
|
|
260
|
+
"--json",
|
|
261
|
+
]);
|
|
262
|
+
box.output.err(d.ok
|
|
263
|
+
? `[INFO] 前の組織(${prevOrg})の TraceFlag(${prevTf})を消しました。`
|
|
264
|
+
: `[WARNING] 前の組織(${prevOrg})の TraceFlag(${prevTf})を消せませんでした(最大 2 時間で切れます)。`);
|
|
265
|
+
}
|
|
266
|
+
const now = fmtZ(box.clock.now().getTime());
|
|
267
|
+
box.files.makeDir(APEX);
|
|
268
|
+
const updates = { ORG: org, USER_ID: userId, TRACE_FLAG_ID: tf.id };
|
|
269
|
+
if (prevOrg !== org || prevUser !== userId || kvGet(state, "LAST_FETCH") === "") {
|
|
270
|
+
const mark = indexMark(parseIndex(read(box, INDEX_F)));
|
|
271
|
+
Object.assign(updates, {
|
|
272
|
+
LAST_FETCH: now,
|
|
273
|
+
LAST_FETCH_ID: "",
|
|
274
|
+
LATE_FROM: "",
|
|
275
|
+
LAST_READ_LINE: String(mark.line),
|
|
276
|
+
LAST_READ_ID: mark.id,
|
|
277
|
+
LAST_READ: "",
|
|
278
|
+
LAST_READ_IDS: "",
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
if (!(await stateSet(ctx, STATE_F, updates)))
|
|
282
|
+
throw new Stop(1, "手元の状態を書けません。");
|
|
283
|
+
await stateSet(ctx, STATUS_F, {
|
|
284
|
+
STATE: "collecting",
|
|
285
|
+
ORG: org,
|
|
286
|
+
EXPIRES: tf.expires,
|
|
287
|
+
EXPIRES_AT: localHm(tf.expires),
|
|
288
|
+
COUNT_TODAY: String(countToday(box)),
|
|
289
|
+
REASON: "",
|
|
290
|
+
WARNING: "",
|
|
291
|
+
TAKEOVER: tf.tookOver ? "1" : "",
|
|
292
|
+
});
|
|
293
|
+
note(box, "INFO", `TraceFlag を作りました(${tf.id}・組織: ${org}・期限: ${tf.expires})`);
|
|
294
|
+
// 組織のログの量
|
|
295
|
+
const vol = await sf(box, [
|
|
296
|
+
"data",
|
|
297
|
+
"query",
|
|
298
|
+
"--use-tooling-api",
|
|
299
|
+
"--target-org",
|
|
300
|
+
org,
|
|
301
|
+
"--json",
|
|
302
|
+
"--query",
|
|
303
|
+
"SELECT COUNT(Id) n, SUM(LogLength) s FROM ApexLog",
|
|
304
|
+
]);
|
|
305
|
+
const rec = vol.json.result?.records?.[0];
|
|
306
|
+
const n = Number.isInteger(rec?.n) ? rec?.n : null;
|
|
307
|
+
const s = Number.isInteger(rec?.s) ? rec?.s : 0;
|
|
308
|
+
box.output.err(`[SUCCESS] デバッグログを取り始めました(組織: ${org}・TraceFlag: ${tf.id}・期限: ${tf.expires}(UTC))。`);
|
|
309
|
+
if (n !== null)
|
|
310
|
+
box.output.err(`[INFO] 組織のログ: ${n} 本・${Math.floor(s / 1024 / 1024)}MB(上限 1,000MB)`);
|
|
311
|
+
if (s > ORG_LOG_WARN_BYTES)
|
|
312
|
+
box.output.err("[WARNING] 組織のログが 700MB を超えています。1,000MB を超えると、TraceFlag を作れなくなります。");
|
|
313
|
+
}
|
|
314
|
+
async function start(args, box, deps, ask) {
|
|
315
|
+
let orgArg = "";
|
|
316
|
+
let level = "standard";
|
|
317
|
+
let hours = 2;
|
|
318
|
+
let yes = false;
|
|
319
|
+
for (let i = 0; i < args.length; i++) {
|
|
320
|
+
const a = args[i];
|
|
321
|
+
const v = args[i + 1] ?? "";
|
|
322
|
+
if (a === "-o") {
|
|
323
|
+
orgArg = parseOrgOption(args, i);
|
|
324
|
+
i++;
|
|
325
|
+
}
|
|
326
|
+
else if (a === "--level") {
|
|
327
|
+
if (v !== "standard" && v !== "detail")
|
|
328
|
+
throw new Stop(1, `--level は standard か detail です: ${v}`);
|
|
329
|
+
level = v;
|
|
330
|
+
i++;
|
|
331
|
+
}
|
|
332
|
+
else if (a === "--hours") {
|
|
333
|
+
if (!/^[0-9]+$/.test(v) || Number(v) < 1 || Number(v) > 23)
|
|
334
|
+
throw new Stop(1, `--hours は 1〜23 の整数です: ${v}`);
|
|
335
|
+
hours = Number(v);
|
|
336
|
+
i++;
|
|
337
|
+
}
|
|
338
|
+
else if (a === "-y")
|
|
339
|
+
yes = true;
|
|
340
|
+
else
|
|
341
|
+
throw new Stop(1, `不明なオプションです: ${a}`);
|
|
342
|
+
}
|
|
343
|
+
const { org, userId } = await prepareOrg(box, orgArg);
|
|
344
|
+
box.output.err(`[WARNING] ${NOTICE_TEXT}`);
|
|
345
|
+
if (!yes)
|
|
346
|
+
await confirm(ask, "log.start", `${org} のデバッグログを取り始めますか?`);
|
|
347
|
+
const state = read(box, STATE_F);
|
|
348
|
+
const dlId = await ensureDebugLevel(box, org, level);
|
|
349
|
+
const tf = await ensureTraceFlag(box, ask, { org, userId, dlId, hours, yes });
|
|
350
|
+
await recordStart(box, deps, { org, userId, state, tf });
|
|
351
|
+
return 0;
|
|
352
|
+
}
|
|
353
|
+
async function stop(args, box, deps) {
|
|
354
|
+
let orgArg = "";
|
|
355
|
+
for (let i = 0; i < args.length; i++) {
|
|
356
|
+
if (args[i] === "-o") {
|
|
357
|
+
orgArg = parseOrgOption(args, i);
|
|
358
|
+
i++;
|
|
359
|
+
}
|
|
360
|
+
else
|
|
361
|
+
throw new Stop(1, `不明なオプションです: ${args[i]}`);
|
|
362
|
+
}
|
|
363
|
+
const other = otherHandler(box);
|
|
364
|
+
if (other !== null)
|
|
365
|
+
throw new Stop(1, `${other}。そちらで止めてください(sf-log.sh stop)。`);
|
|
366
|
+
const ctx = { box, deps };
|
|
367
|
+
const state = read(box, STATE_F);
|
|
368
|
+
const tf = kvGet(state, "TRACE_FLAG_ID");
|
|
369
|
+
if (ID_RE.test(tf)) {
|
|
370
|
+
let org = orgArg !== "" ? orgArg : kvGet(state, "ORG"); // 消す先は、TraceFlag を作った組織
|
|
371
|
+
if (org === "")
|
|
372
|
+
org = (await getTargetOrg(box, "")) ?? "";
|
|
373
|
+
if (org === "")
|
|
374
|
+
throw new Stop(1, "接続先の組織エイリアスを特定できません。");
|
|
375
|
+
const d = await sf(box, [
|
|
376
|
+
"data",
|
|
377
|
+
"delete",
|
|
378
|
+
"record",
|
|
379
|
+
"--use-tooling-api",
|
|
380
|
+
"--sobject",
|
|
381
|
+
"TraceFlag",
|
|
382
|
+
"--record-id",
|
|
383
|
+
tf,
|
|
384
|
+
"--target-org",
|
|
385
|
+
org,
|
|
386
|
+
"--json",
|
|
387
|
+
]);
|
|
388
|
+
if (d.ok)
|
|
389
|
+
box.output.err(`[SUCCESS] TraceFlag を消しました(${tf}・組織: ${org})。`);
|
|
390
|
+
else if (isGone(errName(d.json)))
|
|
391
|
+
box.output.err(`[WARNING] TraceFlag(${tf})は、すでにありません(${errName(d.json)})。`);
|
|
392
|
+
else {
|
|
393
|
+
await stateSet(ctx, STATUS_F, { STATE: "error", REASON: `TraceFlag を消せませんでした(${errName(d.json)})` });
|
|
394
|
+
throw new Stop(1, `TraceFlag(${tf})を消せませんでした(${errName(d.json)})。最大 2 時間で切れます。`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
box.output.err("[INFO] sft log が作った TraceFlag はありません。");
|
|
399
|
+
}
|
|
400
|
+
await stateSet(ctx, STATE_F, { TRACE_FLAG_ID: "" });
|
|
401
|
+
await stateSet(ctx, STATUS_F, { STATE: "stopped", EXPIRES: "", EXPIRES_AT: "", REASON: "sft log stop で止めました", BACKLOG: "" });
|
|
402
|
+
box.files.removeFile(OWNERS_F);
|
|
403
|
+
if (kvGet(read(box, CLAIM_F), "RUNTIME") === RUNTIME_SELF)
|
|
404
|
+
box.files.removeFile(CLAIM_F);
|
|
405
|
+
box.output.err(`[INFO] 手元のログ(${APEX}/)は残しました。`);
|
|
406
|
+
return 0;
|
|
407
|
+
}
|
|
408
|
+
async function guard(f, box) {
|
|
409
|
+
try {
|
|
410
|
+
return await f();
|
|
411
|
+
}
|
|
412
|
+
catch (e) {
|
|
413
|
+
if (!(e instanceof Stop))
|
|
414
|
+
throw e;
|
|
415
|
+
if (e.message !== "")
|
|
416
|
+
box.output.err(`[ERROR] ${e.message}`);
|
|
417
|
+
return e.code;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
export const runLogStart = (args, box, deps, ask) => guard(() => start(args, box, deps, ask), box);
|
|
421
|
+
export const runLogStop = (args, box, deps) => guard(() => stop(args, box, deps), box);
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// sft log status: デバッグログの状態を表示する(読み取りのみ)。sf-log.sh status(phases/log/status.sh 10 章)の移し先。
|
|
2
|
+
// 取得役の行(5.6): 別の環境が扱っているか・記録が読めるかで、言い切らない(シェル版の status_collector_line と同じ判断。自分の環境は node)
|
|
3
|
+
import { collectInterval } from "./log-collector.js";
|
|
4
|
+
import { RUNTIME_SELF } from "./log-pull.js";
|
|
5
|
+
import { ID_RE, kvGet, parseIndex } from "./log-select.js";
|
|
6
|
+
import { getTargetOrg } from "./org.js";
|
|
7
|
+
const APEX_DIR = "logs/apex";
|
|
8
|
+
function toMs(s) {
|
|
9
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(Z|[+-]\d{2}:?\d{2})$/.exec(String(s));
|
|
10
|
+
if (!m)
|
|
11
|
+
return null;
|
|
12
|
+
let ms = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]));
|
|
13
|
+
const tz = m[7];
|
|
14
|
+
if (tz !== "Z") {
|
|
15
|
+
const t = tz.replace(":", "");
|
|
16
|
+
ms -= (t.startsWith("-") ? -1 : 1) * (Number(t.slice(1, 3)) * 60 + Number(t.slice(3, 5))) * 60000;
|
|
17
|
+
}
|
|
18
|
+
return Number.isNaN(ms) ? null : ms;
|
|
19
|
+
}
|
|
20
|
+
function fmtZ(ms) {
|
|
21
|
+
return new Date(Math.floor(ms / 1000) * 1000).toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
22
|
+
}
|
|
23
|
+
/** sf data query --json の応答から、TraceFlag を有効・期限切れ・これから始まるものに分ける(lib/apexlog.js の tflist と同じ)。形が正しくなければ null */
|
|
24
|
+
export function parseTraceFlags(json, nowMs) {
|
|
25
|
+
let data;
|
|
26
|
+
try {
|
|
27
|
+
data = JSON.parse(json);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const recs = data.result?.records;
|
|
33
|
+
if (!Array.isArray(recs))
|
|
34
|
+
return null;
|
|
35
|
+
const out = [];
|
|
36
|
+
for (const r of recs) {
|
|
37
|
+
if (!r || typeof r.Id !== "string" || !ID_RE.test(r.Id))
|
|
38
|
+
return null;
|
|
39
|
+
if (typeof r.LogType !== "string" || !/^[A-Z_]+$/.test(r.LogType))
|
|
40
|
+
return null;
|
|
41
|
+
const exp = toMs(r.ExpirationDate);
|
|
42
|
+
if (exp === null)
|
|
43
|
+
return null;
|
|
44
|
+
let start = null;
|
|
45
|
+
if (r.StartDate !== null && r.StartDate !== undefined) {
|
|
46
|
+
start = toMs(r.StartDate);
|
|
47
|
+
if (start === null)
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const level = r.DebugLevel?.DeveloperName;
|
|
51
|
+
out.push({
|
|
52
|
+
id: r.Id,
|
|
53
|
+
type: r.LogType,
|
|
54
|
+
start: start === null ? "-" : fmtZ(start),
|
|
55
|
+
expires: fmtZ(exp),
|
|
56
|
+
state: start !== null && start > nowMs ? "future" : exp <= nowMs ? "expired" : "active",
|
|
57
|
+
level: typeof level === "string" && level !== "" ? level : "-",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
function sfErrName(json) {
|
|
63
|
+
try {
|
|
64
|
+
const n = JSON.parse(json).name;
|
|
65
|
+
return typeof n === "string" && n !== "" ? n : "不明";
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return "不明";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** 自分のユーザー ID と組織 ID(sf org display user)。読めなければ、sf の応答の name(理由)を返す */
|
|
72
|
+
export async function readOrgUser(box, org) {
|
|
73
|
+
const u = await box.run("sf", ["org", "display", "user", "--target-org", org, "--json"]).catch(() => null);
|
|
74
|
+
let res = {};
|
|
75
|
+
try {
|
|
76
|
+
res = JSON.parse(u?.stdout ?? "").result ?? {};
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
res = {};
|
|
80
|
+
}
|
|
81
|
+
const userId = String(res.id ?? "");
|
|
82
|
+
const orgId = String(res.orgId ?? "");
|
|
83
|
+
if (u === null || u.code !== 0)
|
|
84
|
+
return sfErrName(u?.stdout ?? "");
|
|
85
|
+
if (!ID_RE.test(userId))
|
|
86
|
+
return "ユーザー ID の形が正しくありません";
|
|
87
|
+
if (!ID_RE.test(orgId))
|
|
88
|
+
return "組織 ID の形が正しくありません";
|
|
89
|
+
return { userId, orgId };
|
|
90
|
+
}
|
|
91
|
+
const MIX_STALE = 1800;
|
|
92
|
+
const MAX_RUN = 43200;
|
|
93
|
+
const agoText = (sec) => (sec === null ? "時刻が分からない" : `${sec} 秒前`);
|
|
94
|
+
/** 別の環境が、このプロジェクトのログを扱っているか(シェル版の other_runtime_active と同じ順)。扱っていれば、環境の名前と出どころ */
|
|
95
|
+
export function otherRuntime(collector, claim, nowSec) {
|
|
96
|
+
const recent = (at) => !/^[0-9]+$/.test(at) || Number(at) > nowSec || nowSec - Number(at) <= MIX_STALE;
|
|
97
|
+
// 1. 名乗り(.claim)が別の環境で、30 分以内(読めない・未来の時刻を含む)
|
|
98
|
+
const clRt = kvGet(claim, "RUNTIME");
|
|
99
|
+
if (claim !== "" && clRt !== RUNTIME_SELF && recent(kvGet(claim, "AT")))
|
|
100
|
+
return { rt: clRt, src: "claim" };
|
|
101
|
+
// 2〜3. 取得役の記録がない・起動の時刻が読めない・12 時間より古い → 扱っていない
|
|
102
|
+
if (collector === "")
|
|
103
|
+
return null;
|
|
104
|
+
const started = kvGet(collector, "STARTED");
|
|
105
|
+
if (!/^[0-9]+$/.test(started) || (Number(started) <= nowSec && nowSec - Number(started) >= MAX_RUN))
|
|
106
|
+
return null;
|
|
107
|
+
// 4. 環境の記録のない古い取得役 → 環境不明が扱っている。5. 自分と同じ環境 → 扱っていない。6. 別の環境は合図が 30 分以内なら
|
|
108
|
+
const rt = kvGet(collector, "RUNTIME");
|
|
109
|
+
if (rt === "")
|
|
110
|
+
return { rt: "", src: "collector" };
|
|
111
|
+
if (rt === RUNTIME_SELF || !recent(kvGet(collector, "HEARTBEAT")))
|
|
112
|
+
return null;
|
|
113
|
+
return { rt, src: "collector" };
|
|
114
|
+
}
|
|
115
|
+
/** 表示用の環境の名前 */
|
|
116
|
+
export const runtimeLabel = (rt) => (rt === "" ? "不明(古い形の記録)" : rt);
|
|
117
|
+
/** status の取得役の行(シェル版の status_collector_line と同じ) */
|
|
118
|
+
export function collectorLine(collector, claim, nowSec, pidAlive, interval) {
|
|
119
|
+
const ago = (at) => agoText(/^[0-9]+$/.test(at) && Number(at) <= nowSec ? nowSec - Number(at) : null);
|
|
120
|
+
const cRt = kvGet(collector, "RUNTIME");
|
|
121
|
+
const other = otherRuntime(collector, claim, nowSec);
|
|
122
|
+
if (other !== null) {
|
|
123
|
+
if (other.src === "claim" && (collector === "" || cRt !== other.rt))
|
|
124
|
+
return ` 取得役: 取得役の起動待ち(環境 ${runtimeLabel(other.rt)}・名乗りは ${ago(kvGet(claim, "AT"))})`;
|
|
125
|
+
if (other.src === "collector" && cRt === "")
|
|
126
|
+
return " 取得役: 状態不明(環境の記録のない、古い形の取得役の記録です。起動から 12 時間は、動いているものとみなします)";
|
|
127
|
+
return ` 取得役: 別の環境(${runtimeLabel(cRt)})で動作中(最後の合図は ${ago(kvGet(collector, "HEARTBEAT"))}・組織 ${kvGet(collector, "ORG")})`;
|
|
128
|
+
}
|
|
129
|
+
const pid = kvGet(collector, "PID");
|
|
130
|
+
if (collector !== "" && !(/^[0-9]+$/.test(pid) && /^[0-9]+$/.test(kvGet(collector, "STARTED"))))
|
|
131
|
+
return " 取得役: 状態不明(logs/apex/.collector の形が正しくありません)";
|
|
132
|
+
// 別の環境の PID は、この環境では確かめない(たまたま同じ番号の、関係のないプロセスがある)。環境の記録のない古い形は確かめる
|
|
133
|
+
if (collector !== "" && (cRt === RUNTIME_SELF || cRt === "") && pidAlive(Number(pid)))
|
|
134
|
+
return ` 取得役: 動作中(PID ${pid}・組織 ${kvGet(collector, "ORG")}・間隔 ${interval} 秒)`;
|
|
135
|
+
if (claim !== "" && kvGet(claim, "RUNTIME") === RUNTIME_SELF)
|
|
136
|
+
return ` 取得役: 取得役の起動待ち(この環境・名乗りは ${ago(kvGet(claim, "AT"))})`;
|
|
137
|
+
return " 取得役: 動いていません";
|
|
138
|
+
}
|
|
139
|
+
export async function runLogStatus(args, box, deps = { pidAlive: () => false }) {
|
|
140
|
+
let orgArg = "";
|
|
141
|
+
for (let i = 0; i < args.length; i++) {
|
|
142
|
+
if (args[i] === "-o" && (args[i + 1] ?? "").length > 0 && !args[i + 1].startsWith("-")) {
|
|
143
|
+
orgArg = args[i + 1];
|
|
144
|
+
i++;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
box.output.err(`[ERROR] 不明なオプションです: ${args[i]}`);
|
|
148
|
+
return 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const { files } = box;
|
|
152
|
+
const read = (name) => (files.exists(`${APEX_DIR}/${name}`) ? files.readText(`${APEX_DIR}/${name}`) : "");
|
|
153
|
+
const status = read(".status");
|
|
154
|
+
const state = read(".state");
|
|
155
|
+
const out = (s) => box.output.out(s);
|
|
156
|
+
out("デバッグログ(sf-log.sh)の状態");
|
|
157
|
+
out(collectorLine(read(".collector"), read(".claim"), Math.floor(box.clock.now().getTime() / 1000), deps.pidAlive, collectInterval(box)));
|
|
158
|
+
for (const k of ["STATE", "ORG", "EXPIRES", "LAST_FETCH", "COUNT_TODAY", "BACKLOG", "REASON", "WARNING"]) {
|
|
159
|
+
const v = kvGet(status, k);
|
|
160
|
+
if (v !== "")
|
|
161
|
+
out(` 表示の状態 ${k}: ${v}`);
|
|
162
|
+
}
|
|
163
|
+
for (const k of ["ORG", "TRACE_FLAG_ID", "LAST_FETCH", "LATE_FROM", "LAST_READ_LINE"]) {
|
|
164
|
+
const v = kvGet(state, k);
|
|
165
|
+
if (v !== "")
|
|
166
|
+
out(` 取得の記録 ${k}: ${v}`);
|
|
167
|
+
}
|
|
168
|
+
const owners = read(".owners")
|
|
169
|
+
.split("\n")
|
|
170
|
+
.filter((l) => l !== "" && !l.startsWith("CLOSING=")).length;
|
|
171
|
+
out(` 持ち主(起動中の sf-start): ${owners}`);
|
|
172
|
+
const auto = kvGet(read(".consent"), "AUTO");
|
|
173
|
+
out(` 自動(sf-start のとき): ${auto === "" ? "まだ答えていません" : auto}`);
|
|
174
|
+
if (files.exists(`${APEX_DIR}/.index`))
|
|
175
|
+
out(` 手元のログ: ${parseIndex(read(".index")).length} 本`);
|
|
176
|
+
if (files.exists(`${APEX_DIR}/latest.md`))
|
|
177
|
+
out(` 最新の要約: ${APEX_DIR}/latest.md`);
|
|
178
|
+
const org = await getTargetOrg(box, orgArg);
|
|
179
|
+
if (org === null) {
|
|
180
|
+
box.output.err("[ERROR] 接続先の組織エイリアスを特定できません");
|
|
181
|
+
return 1;
|
|
182
|
+
}
|
|
183
|
+
const shown = kvGet(status, "ORG");
|
|
184
|
+
if (shown !== "" && shown !== org)
|
|
185
|
+
out(` 表示の状態: 状態不明(別の組織(${shown})の記録です。接続先は ${org})`);
|
|
186
|
+
const user = await readOrgUser(box, org);
|
|
187
|
+
if (typeof user === "string") {
|
|
188
|
+
box.output.err(`[ERROR] ユーザーの情報を取得できません(${user})`);
|
|
189
|
+
return 1;
|
|
190
|
+
}
|
|
191
|
+
const userId = user.userId;
|
|
192
|
+
const mine = kvGet(state, "TRACE_FLAG_ID");
|
|
193
|
+
const q = async (soql) => box.run("sf", ["data", "query", "--use-tooling-api", "--target-org", org, "--json", "--query", soql]).catch(() => null);
|
|
194
|
+
const tf = await q(`SELECT Id, LogType, StartDate, ExpirationDate, DebugLevel.DeveloperName FROM TraceFlag WHERE TracedEntityId = '${userId}'`);
|
|
195
|
+
if (tf === null || tf.code !== 0) {
|
|
196
|
+
out(` このユーザーの TraceFlag: 組織に問い合わせできませんでした(${sfErrName(tf?.stdout ?? "")})`);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const rows = parseTraceFlags(tf.stdout, box.clock.now().getTime());
|
|
200
|
+
if (rows === null) {
|
|
201
|
+
out(" このユーザーの TraceFlag: 検索の応答の形が正しくありません");
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
out(` このユーザーの TraceFlag(組織: ${org}):`);
|
|
205
|
+
if (rows.length === 0)
|
|
206
|
+
out(" なし");
|
|
207
|
+
for (const r of rows)
|
|
208
|
+
out(` ${r.id} ${r.type} ${r.state} 期限 ${r.expires} ${r.level}${r.id === mine ? " (sf-log.sh が作ったもの)" : ""}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const c = await q(`SELECT COUNT(Id) n, SUM(LogLength) s FROM ApexLog WHERE LogUserId = '${userId}'`);
|
|
212
|
+
if (c !== null && c.code === 0) {
|
|
213
|
+
try {
|
|
214
|
+
const rec = JSON.parse(c.stdout).result?.records?.[0];
|
|
215
|
+
if (rec && /^[0-9]+$/.test(String(rec.n)))
|
|
216
|
+
out(` 組織にある自分のログ: ${rec.n} 本・${Math.floor(Number(rec.s ?? 0) / 1024)}KB`);
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
// 応答の形が違うときは、この行だけ省く(読み取りのみの表示)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return 0;
|
|
223
|
+
}
|