Files
Genarrative/scripts/spacetime-logs-local.sh

130 lines
3.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
用法:
npm run dev:rust:logs
npm run dev:rust:logs -- --follow
./scripts/spacetime-logs-local.sh --lines 500 --output logs/spacetime/local.log
说明:
1. 从本地 SpacetimeDB 通过 spacetime logs 提取模块日志到本地文件。
2. 默认读取 spacetime.local.json 的 database 字段,默认 server 为 http://127.0.0.1:3101。
3. 默认输出到 logs/spacetime/<database>-<timestamp>.log--follow 会持续追加并同步写到终端。
4. 默认使用 server-rs/.spacetimedb/local 作为 spacetime CLI root保持本地身份与 standalone 一致。
EOF
}
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "[stdb:logs] 缺少命令: ${command_name}" >&2
exit 1
fi
}
read_local_spacetime_database() {
local config_path="${REPO_ROOT}/spacetime.local.json"
if [[ ! -f "${config_path}" ]]; then
return
fi
node -e '
const fs = require("fs");
const path = process.argv[1];
try {
const value = JSON.parse(fs.readFileSync(path, "utf8")).database;
if (typeof value === "string" && value.trim()) {
process.stdout.write(value.trim());
}
} catch (error) {
process.stderr.write(`[stdb:logs] ignore invalid spacetime.local.json: ${error.message}\n`);
}
' "${config_path}"
}
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
DATABASE=""
SPACETIME_SERVER="http://127.0.0.1:3101"
SPACETIME_ROOT_DIR="${REPO_ROOT}/server-rs/.spacetimedb/local"
LINES="200"
OUTPUT=""
FOLLOW=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--database)
DATABASE="${2:?缺少 --database 的值}"
shift 2
;;
--server)
SPACETIME_SERVER="${2:?缺少 --server 的值}"
shift 2
;;
--spacetime-root-dir)
SPACETIME_ROOT_DIR="${2:?缺少 --spacetime-root-dir 的值}"
shift 2
;;
--lines|-n)
LINES="${2:?缺少 --lines 的值}"
shift 2
;;
--output|-o)
OUTPUT="${2:?缺少 --output 的值}"
shift 2
;;
--follow|-f)
FOLLOW=1
shift
;;
*)
echo "[stdb:logs] 未知参数: $1" >&2
usage >&2
exit 1
;;
esac
done
require_command node
require_command spacetime
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="$(read_local_spacetime_database)"
fi
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="genarrative-dev"
fi
if [[ -z "${OUTPUT//[[:space:]]/}" ]]; then
LOG_DIR="${REPO_ROOT}/logs/spacetime"
mkdir -p "${LOG_DIR}"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
OUTPUT="${LOG_DIR}/${DATABASE}-${TIMESTAMP}.log"
else
mkdir -p "$(dirname -- "${OUTPUT}")"
fi
ARGS=(logs "${DATABASE}" --server "${SPACETIME_SERVER}" -n "${LINES}")
if [[ "${FOLLOW}" -eq 1 ]]; then
ARGS+=(-f)
fi
echo "[stdb:logs] database: ${DATABASE}"
echo "[stdb:logs] server: ${SPACETIME_SERVER}"
echo "[stdb:logs] spacetime root: ${SPACETIME_ROOT_DIR}"
echo "[stdb:logs] output: ${OUTPUT}"
spacetime --root-dir="${SPACETIME_ROOT_DIR}" "${ARGS[@]}" | tee -a "${OUTPUT}"