2022-05-19 19:56:03 +00:00
|
|
|
## Nim-Codex
|
2022-01-10 15:32:56 +00:00
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2022-03-18 22:17:51 +00:00
|
|
|
import pkg/upraises
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
import std/os
|
2022-04-12 13:21:07 +00:00
|
|
|
import std/terminal
|
2022-01-10 15:32:56 +00:00
|
|
|
import std/options
|
2022-04-14 10:49:03 +00:00
|
|
|
import std/strutils
|
2022-04-12 13:21:07 +00:00
|
|
|
import std/typetraits
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2023-02-15 19:40:21 +00:00
|
|
|
import pkg/chronos
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/chronicles
|
2022-11-01 20:05:40 +00:00
|
|
|
import pkg/chronicles/helpers
|
2022-04-12 13:21:07 +00:00
|
|
|
import pkg/chronicles/topics_registry
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/confutils/defs
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/confutils/std/net
|
2023-05-02 13:06:34 +00:00
|
|
|
import pkg/toml_serialization
|
2022-04-14 10:49:03 +00:00
|
|
|
import pkg/metrics
|
|
|
|
import pkg/metrics/chronos_httpserver
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/stew/shims/net as stewnet
|
2023-07-06 23:23:27 +00:00
|
|
|
import pkg/stew/shims/parseutils
|
2022-01-10 15:32:56 +00:00
|
|
|
import pkg/libp2p
|
2022-04-14 16:20:01 +00:00
|
|
|
import pkg/ethers
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
import ./discovery
|
2022-12-03 00:00:55 +00:00
|
|
|
import ./stores
|
2023-07-06 23:23:27 +00:00
|
|
|
import ./units
|
|
|
|
import ./utils
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
export units
|
|
|
|
export net, DefaultQuotaBytes, DefaultBlockTtl, DefaultBlockMaintenanceInterval, DefaultNumberOfBlocksToMaintainPerInterval
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2023-06-19 06:21:03 +00:00
|
|
|
const
|
|
|
|
codex_enable_api_debug_peers* {.booldefine.} = false
|
2023-06-22 10:32:18 +00:00
|
|
|
codex_enable_proof_failures* {.booldefine.} = false
|
2023-06-19 06:21:03 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
type
|
|
|
|
StartUpCommand* {.pure.} = enum
|
|
|
|
noCommand,
|
|
|
|
initNode
|
|
|
|
|
2022-04-12 13:21:07 +00:00
|
|
|
LogKind* = enum
|
|
|
|
Auto = "auto"
|
|
|
|
Colors = "colors"
|
|
|
|
NoColors = "nocolors"
|
|
|
|
Json = "json"
|
|
|
|
None = "none"
|
|
|
|
|
2023-03-14 22:32:15 +00:00
|
|
|
RepoKind* = enum
|
|
|
|
repoFS = "fs"
|
|
|
|
repoSQLite = "sqlite"
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
CodexConf* = object
|
2023-05-02 13:06:34 +00:00
|
|
|
configFile* {.
|
|
|
|
desc: "Loads the configuration from a TOML file"
|
|
|
|
defaultValueDesc: "none"
|
|
|
|
defaultValue: InputFile.none
|
|
|
|
name: "config-file" }: Option[InputFile]
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
logLevel* {.
|
2023-06-16 16:20:49 +00:00
|
|
|
defaultValue: "info"
|
2022-04-12 13:21:07 +00:00
|
|
|
desc: "Sets the log level",
|
2022-11-01 20:05:40 +00:00
|
|
|
name: "log-level" }: string
|
2022-04-12 13:21:07 +00:00
|
|
|
|
|
|
|
logFormat* {.
|
|
|
|
hidden
|
|
|
|
desc: "Specifies what kind of logs should be written to stdout (auto, colors, nocolors, json)"
|
|
|
|
defaultValueDesc: "auto"
|
|
|
|
defaultValue: LogKind.Auto
|
|
|
|
name: "log-format" }: LogKind
|
|
|
|
|
2022-04-14 10:49:03 +00:00
|
|
|
metricsEnabled* {.
|
|
|
|
desc: "Enable the metrics server"
|
|
|
|
defaultValue: false
|
|
|
|
name: "metrics" }: bool
|
|
|
|
|
|
|
|
metricsAddress* {.
|
|
|
|
desc: "Listening address of the metrics server"
|
|
|
|
defaultValue: ValidIpAddress.init("127.0.0.1")
|
|
|
|
defaultValueDesc: "127.0.0.1"
|
|
|
|
name: "metrics-address" }: ValidIpAddress
|
|
|
|
|
|
|
|
metricsPort* {.
|
|
|
|
desc: "Listening HTTP port of the metrics server"
|
|
|
|
defaultValue: 8008
|
|
|
|
name: "metrics-port" }: Port
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
dataDir* {.
|
2022-05-19 19:56:03 +00:00
|
|
|
desc: "The directory where codex will store configuration and data."
|
2022-01-10 15:32:56 +00:00
|
|
|
defaultValue: defaultDataDir()
|
|
|
|
defaultValueDesc: ""
|
|
|
|
abbr: "d"
|
|
|
|
name: "data-dir" }: OutDir
|
|
|
|
|
|
|
|
case cmd* {.
|
|
|
|
command
|
|
|
|
defaultValue: noCommand }: StartUpCommand
|
|
|
|
|
|
|
|
of noCommand:
|
2023-08-01 23:47:57 +00:00
|
|
|
|
2022-10-27 13:44:56 +00:00
|
|
|
listenAddrs* {.
|
|
|
|
desc: "Multi Addresses to listen on"
|
|
|
|
defaultValue: @[
|
|
|
|
MultiAddress.init("/ip4/0.0.0.0/tcp/0")
|
|
|
|
.expect("Should init multiaddress")]
|
|
|
|
defaultValueDesc: "/ip4/0.0.0.0/tcp/0"
|
2022-04-13 16:32:35 +00:00
|
|
|
abbr: "i"
|
2022-10-27 13:44:56 +00:00
|
|
|
name: "listen-addrs" }: seq[MultiAddress]
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
# TODO: change this once we integrate nat support
|
2022-11-02 00:58:41 +00:00
|
|
|
nat* {.
|
|
|
|
desc: "IP Addresses to announce behind a NAT"
|
|
|
|
defaultValue: ValidIpAddress.init("127.0.0.1")
|
|
|
|
defaultValueDesc: "127.0.0.1"
|
2022-10-27 13:44:56 +00:00
|
|
|
abbr: "a"
|
2022-11-02 00:58:41 +00:00
|
|
|
name: "nat" }: ValidIpAddress
|
|
|
|
|
|
|
|
discoveryIp* {.
|
|
|
|
desc: "Discovery listen address"
|
|
|
|
defaultValue: ValidIpAddress.init(IPv4_any())
|
|
|
|
defaultValueDesc: "0.0.0.0"
|
2022-11-15 19:16:02 +00:00
|
|
|
abbr: "e"
|
2022-11-02 00:58:41 +00:00
|
|
|
name: "disc-ip" }: ValidIpAddress
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
discoveryPort* {.
|
2022-11-02 00:58:41 +00:00
|
|
|
desc: "Discovery (UDP) port"
|
2022-11-15 19:16:02 +00:00
|
|
|
defaultValue: 8090.Port
|
2022-04-13 16:32:35 +00:00
|
|
|
defaultValueDesc: "8090"
|
2022-11-15 19:16:02 +00:00
|
|
|
abbr: "u"
|
2022-11-02 00:58:41 +00:00
|
|
|
name: "disc-port" }: Port
|
2022-04-13 16:32:35 +00:00
|
|
|
|
|
|
|
netPrivKeyFile* {.
|
2022-11-02 00:58:41 +00:00
|
|
|
desc: "Source of network (secp256k1) private key file path or name"
|
|
|
|
defaultValue: "key"
|
2022-04-13 16:32:35 +00:00
|
|
|
name: "net-privkey" }: string
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
bootstrapNodes* {.
|
|
|
|
desc: "Specifies one or more bootstrap nodes to use when connecting to the network."
|
|
|
|
abbr: "b"
|
2022-04-14 10:49:03 +00:00
|
|
|
name: "bootstrap-node" }: seq[SignedPeerRecord]
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
maxPeers* {.
|
|
|
|
desc: "The maximum number of peers to connect to"
|
|
|
|
defaultValue: 160
|
|
|
|
name: "max-peers" }: int
|
|
|
|
|
|
|
|
agentString* {.
|
2022-05-19 19:56:03 +00:00
|
|
|
defaultValue: "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
desc: "Node agent string which is used as identifier in network"
|
|
|
|
name: "agent-string" }: string
|
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
apiBindAddress* {.
|
|
|
|
desc: "The REST API bind address"
|
|
|
|
defaultValue: "127.0.0.1"
|
|
|
|
name: "api-bindaddr"
|
|
|
|
}: string
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
apiPort* {.
|
|
|
|
desc: "The REST Api port",
|
2023-07-06 23:23:27 +00:00
|
|
|
defaultValue: 8080.Port
|
2022-01-10 15:32:56 +00:00
|
|
|
defaultValueDesc: "8080"
|
|
|
|
name: "api-port"
|
2023-07-06 23:23:27 +00:00
|
|
|
abbr: "p" }: Port
|
2022-01-10 15:32:56 +00:00
|
|
|
|
2023-03-14 22:32:15 +00:00
|
|
|
repoKind* {.
|
|
|
|
desc: "backend for main repo store (fs, sqlite)"
|
|
|
|
defaultValueDesc: "fs"
|
|
|
|
defaultValue: repoFS
|
|
|
|
name: "repo-kind" }: RepoKind
|
|
|
|
|
2022-12-03 00:00:55 +00:00
|
|
|
storageQuota* {.
|
|
|
|
desc: "The size of the total storage quota dedicated to the node"
|
|
|
|
defaultValue: DefaultQuotaBytes
|
|
|
|
defaultValueDesc: $DefaultQuotaBytes
|
|
|
|
name: "storage-quota"
|
2023-07-06 23:23:27 +00:00
|
|
|
abbr: "q" }: NBytes
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
blockTtl* {.
|
2022-12-03 00:00:55 +00:00
|
|
|
desc: "Default block timeout in seconds - 0 disables the ttl"
|
2023-07-06 23:23:27 +00:00
|
|
|
defaultValue: DefaultBlockTtl
|
2023-03-08 15:04:54 +00:00
|
|
|
defaultValueDesc: $DefaultBlockTtl
|
2022-12-03 00:00:55 +00:00
|
|
|
name: "block-ttl"
|
2023-07-06 23:23:27 +00:00
|
|
|
abbr: "t" }: Duration
|
2023-03-08 15:04:54 +00:00
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
blockMaintenanceInterval* {.
|
2023-03-08 15:04:54 +00:00
|
|
|
desc: "Time interval in seconds - determines frequency of block maintenance cycle: how often blocks are checked for expiration and cleanup."
|
2023-07-06 23:23:27 +00:00
|
|
|
defaultValue: DefaultBlockMaintenanceInterval
|
2023-03-08 15:04:54 +00:00
|
|
|
defaultValueDesc: $DefaultBlockMaintenanceInterval
|
2023-07-06 23:23:27 +00:00
|
|
|
name: "block-mi" }: Duration
|
2023-03-08 15:04:54 +00:00
|
|
|
|
|
|
|
blockMaintenanceNumberOfBlocks* {.
|
|
|
|
desc: "Number of blocks to check every maintenance cycle."
|
|
|
|
defaultValue: DefaultNumberOfBlocksToMaintainPerInterval
|
|
|
|
defaultValueDesc: $DefaultNumberOfBlocksToMaintainPerInterval
|
|
|
|
name: "block-mn" }: int
|
2022-12-03 00:00:55 +00:00
|
|
|
|
2022-03-02 16:30:42 +00:00
|
|
|
cacheSize* {.
|
2023-07-06 23:23:27 +00:00
|
|
|
desc: "The size of the block cache, 0 disables the cache - might help on slow hardrives"
|
2022-11-15 15:46:21 +00:00
|
|
|
defaultValue: 0
|
|
|
|
defaultValueDesc: "0"
|
2022-03-02 16:30:42 +00:00
|
|
|
name: "cache-size"
|
2023-07-06 23:23:27 +00:00
|
|
|
abbr: "c" }: NBytes
|
2022-03-02 16:30:42 +00:00
|
|
|
|
2022-08-09 04:29:06 +00:00
|
|
|
persistence* {.
|
|
|
|
desc: "Enables persistence mechanism, requires an Ethereum node"
|
|
|
|
defaultValue: false
|
|
|
|
name: "persistence"
|
|
|
|
.}: bool
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
ethProvider* {.
|
|
|
|
desc: "The URL of the JSON-RPC API of the Ethereum node"
|
|
|
|
defaultValue: "ws://localhost:8545"
|
|
|
|
name: "eth-provider"
|
|
|
|
.}: string
|
|
|
|
|
|
|
|
ethAccount* {.
|
|
|
|
desc: "The Ethereum account that is used for storage contracts"
|
2022-08-09 04:29:06 +00:00
|
|
|
defaultValue: EthAddress.none
|
2022-04-14 16:20:01 +00:00
|
|
|
name: "eth-account"
|
2022-08-09 04:29:06 +00:00
|
|
|
.}: Option[EthAddress]
|
2022-04-14 16:20:01 +00:00
|
|
|
|
2023-05-03 07:24:25 +00:00
|
|
|
marketplaceAddress* {.
|
|
|
|
desc: "Address of deployed Marketplace contract"
|
|
|
|
defaultValue: EthAddress.none
|
|
|
|
name: "marketplace-address"
|
|
|
|
.}: Option[EthAddress]
|
2022-04-25 13:12:37 +00:00
|
|
|
|
2023-04-19 13:06:00 +00:00
|
|
|
validator* {.
|
|
|
|
desc: "Enables validator, requires an Ethereum node"
|
|
|
|
defaultValue: false
|
|
|
|
name: "validator"
|
|
|
|
.}: bool
|
|
|
|
|
|
|
|
validatorMaxSlots* {.
|
|
|
|
desc: "Maximum number of slots that the validator monitors"
|
|
|
|
defaultValue: 1000
|
|
|
|
name: "validator-max-slots"
|
|
|
|
.}: int
|
|
|
|
|
2023-06-22 10:32:18 +00:00
|
|
|
simulateProofFailures* {.
|
|
|
|
desc: "Simulates proof failures once every N proofs. 0 = disabled."
|
|
|
|
defaultValue: 0
|
|
|
|
name: "simulate-proof-failures"
|
|
|
|
hidden
|
2023-08-21 10:26:43 +00:00
|
|
|
.}: int
|
2023-06-22 10:32:18 +00:00
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
of initNode:
|
|
|
|
discard
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
EthAddress* = ethers.Address
|
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getCodexVersion(): string =
|
|
|
|
let tag = strip(staticExec("git tag"))
|
|
|
|
if tag.isEmptyOrWhitespace:
|
|
|
|
return "untagged build"
|
|
|
|
return tag
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getCodexRevision(): string =
|
2023-06-21 22:02:05 +00:00
|
|
|
# using a slice in a static context breaks nimsuggest for some reason
|
|
|
|
var res = strip(staticExec("git rev-parse --short HEAD"))
|
|
|
|
res.setLen(6)
|
|
|
|
return res
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2023-03-08 11:45:55 +00:00
|
|
|
proc getNimBanner(): string =
|
|
|
|
staticExec("nim --version | grep Version")
|
|
|
|
|
|
|
|
const
|
|
|
|
codexVersion* = getCodexVersion()
|
|
|
|
codexRevision* = getCodexRevision()
|
|
|
|
nimBanner* = getNimBanner()
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
codexFullVersion* =
|
2023-03-08 11:45:55 +00:00
|
|
|
"Codex version: " & codexVersion & "\p" &
|
|
|
|
"Codex revision: " & codexRevision & "\p" &
|
2022-04-14 10:49:03 +00:00
|
|
|
nimBanner
|
|
|
|
|
2022-01-10 15:32:56 +00:00
|
|
|
proc defaultDataDir*(): string =
|
|
|
|
let dataDir = when defined(windows):
|
2022-05-19 19:56:03 +00:00
|
|
|
"AppData" / "Roaming" / "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
elif defined(macosx):
|
2022-05-19 19:56:03 +00:00
|
|
|
"Library" / "Application Support" / "Codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
else:
|
2022-05-19 19:56:03 +00:00
|
|
|
".cache" / "codex"
|
2022-01-10 15:32:56 +00:00
|
|
|
|
|
|
|
getHomeDir() / dataDir
|
|
|
|
|
2023-08-01 23:47:57 +00:00
|
|
|
proc parseCmdArg*(T: typedesc[MultiAddress],
|
|
|
|
input: string): MultiAddress
|
2023-05-02 13:06:34 +00:00
|
|
|
{.upraises: [ValueError, LPError].} =
|
2023-08-01 23:47:57 +00:00
|
|
|
var ma: MultiAddress
|
|
|
|
let res = MultiAddress.init(input)
|
|
|
|
if res.isOk:
|
|
|
|
ma = res.get()
|
|
|
|
else:
|
|
|
|
warn "Invalid MultiAddress", input=input, error=res.error()
|
|
|
|
quit QuitFailure
|
|
|
|
ma
|
2022-04-12 13:21:07 +00:00
|
|
|
|
2023-03-09 11:23:45 +00:00
|
|
|
proc parseCmdArg*(T: type SignedPeerRecord, uri: string): T =
|
2022-04-13 16:32:35 +00:00
|
|
|
var res: SignedPeerRecord
|
|
|
|
try:
|
|
|
|
if not res.fromURI(uri):
|
|
|
|
warn "Invalid SignedPeerRecord uri", uri=uri
|
|
|
|
quit QuitFailure
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Invalid SignedPeerRecord uri", uri=uri, error=exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
res
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
proc parseCmdArg*(T: type EthAddress, address: string): T =
|
2022-04-14 16:20:01 +00:00
|
|
|
EthAddress.init($address).get()
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
proc parseCmdArg*(T: type NBytes, val: string): T =
|
|
|
|
var num = 0'i64
|
|
|
|
let count = parseSize(val, num, alwaysBin = true)
|
|
|
|
if count == 0:
|
|
|
|
warn "Invalid number of bytes", nbytes=val
|
|
|
|
quit QuitFailure
|
|
|
|
NBytes(num)
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type Duration, val: string): T =
|
|
|
|
var dur: Duration
|
|
|
|
let count = parseDuration(val, dur)
|
|
|
|
if count == 0:
|
|
|
|
warn "Invalid duration parse", dur=dur
|
|
|
|
quit QuitFailure
|
|
|
|
dur
|
|
|
|
|
2023-05-02 13:06:34 +00:00
|
|
|
proc readValue*(r: var TomlReader, val: var EthAddress)
|
|
|
|
{.upraises: [SerializationError, IOError].} =
|
|
|
|
val = EthAddress.init(r.readValue(string)).get()
|
|
|
|
|
|
|
|
proc readValue*(r: var TomlReader, val: var SignedPeerRecord) =
|
|
|
|
without uri =? r.readValue(string).catch, err:
|
|
|
|
error "invalid SignedPeerRecord configuration value", error = err.msg
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
val = SignedPeerRecord.parseCmdArg(uri)
|
|
|
|
|
2023-08-01 23:47:57 +00:00
|
|
|
proc readValue*(r: var TomlReader, val: var MultiAddress) =
|
|
|
|
without input =? r.readValue(string).catch, err:
|
|
|
|
error "invalid MultiAddress configuration value", error = err.msg
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
let res = MultiAddress.init(input)
|
|
|
|
if res.isOk:
|
|
|
|
val = res.get()
|
|
|
|
else:
|
|
|
|
warn "Invalid MultiAddress", input=input, error=res.error()
|
|
|
|
quit QuitFailure
|
|
|
|
|
2023-07-06 23:23:27 +00:00
|
|
|
proc readValue*(r: var TomlReader, val: var NBytes)
|
|
|
|
{.upraises: [SerializationError, IOError].} =
|
|
|
|
var value = 0'i64
|
|
|
|
var str = r.readValue(string)
|
|
|
|
let count = parseSize(str, value, alwaysBin = true)
|
|
|
|
if count == 0:
|
|
|
|
error "invalid number of bytes for configuration value", value = str
|
|
|
|
quit QuitFailure
|
|
|
|
val = NBytes(value)
|
|
|
|
|
|
|
|
proc readValue*(r: var TomlReader, val: var Duration)
|
|
|
|
{.upraises: [SerializationError, IOError].} =
|
|
|
|
var str = r.readValue(string)
|
|
|
|
var dur: Duration
|
|
|
|
let count = parseDuration(str, dur)
|
|
|
|
if count == 0:
|
|
|
|
error "Invalid duration parse", value = str
|
|
|
|
quit QuitFailure
|
|
|
|
val = dur
|
|
|
|
|
2022-04-14 16:20:01 +00:00
|
|
|
# no idea why confutils needs this:
|
2023-03-09 11:23:45 +00:00
|
|
|
proc completeCmdArg*(T: type EthAddress; val: string): seq[string] =
|
2022-04-14 16:20:01 +00:00
|
|
|
discard
|
2023-07-06 23:23:27 +00:00
|
|
|
|
|
|
|
proc completeCmdArg*(T: type NBytes; val: string): seq[string] =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc completeCmdArg*(T: type Duration; val: string): seq[string] =
|
|
|
|
discard
|
2022-04-14 16:20:01 +00:00
|
|
|
|
2022-04-12 13:21:07 +00:00
|
|
|
# silly chronicles, colors is a compile-time property
|
|
|
|
proc stripAnsi(v: string): string =
|
|
|
|
var
|
|
|
|
res = newStringOfCap(v.len)
|
|
|
|
i: int
|
|
|
|
|
|
|
|
while i < v.len:
|
|
|
|
let c = v[i]
|
|
|
|
if c == '\x1b':
|
|
|
|
var
|
|
|
|
x = i + 1
|
|
|
|
found = false
|
|
|
|
|
|
|
|
while x < v.len: # look for [..m
|
|
|
|
let c2 = v[x]
|
|
|
|
if x == i + 1:
|
|
|
|
if c2 != '[':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if c2 in {'0'..'9'} + {';'}:
|
|
|
|
discard # keep looking
|
|
|
|
elif c2 == 'm':
|
|
|
|
i = x + 1
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc x
|
|
|
|
|
|
|
|
if found: # skip adding c
|
|
|
|
continue
|
|
|
|
res.add c
|
|
|
|
inc i
|
|
|
|
|
|
|
|
res
|
|
|
|
|
2023-05-02 13:06:34 +00:00
|
|
|
proc updateLogLevel*(logLevel: string) {.upraises: [ValueError].} =
|
2022-11-01 20:05:40 +00:00
|
|
|
# Updates log levels (without clearing old ones)
|
|
|
|
let directives = logLevel.split(";")
|
|
|
|
try:
|
2023-06-16 16:20:49 +00:00
|
|
|
setLogLevel(parseEnum[LogLevel](directives[0].toUpperAscii))
|
2022-11-01 20:05:40 +00:00
|
|
|
except ValueError:
|
2023-06-16 16:20:49 +00:00
|
|
|
raise (ref ValueError)(msg: "Please specify one of: trace, debug, info, notice, warn, error or fatal")
|
2022-11-01 20:05:40 +00:00
|
|
|
|
|
|
|
if directives.len > 1:
|
|
|
|
for topicName, settings in parseTopicDirectives(directives[1..^1]):
|
|
|
|
if not setTopicState(topicName, settings.state, settings.logLevel):
|
|
|
|
warn "Unrecognized logging topic", topic = topicName
|
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc setupLogging*(conf: CodexConf) =
|
2022-04-12 13:21:07 +00:00
|
|
|
when defaultChroniclesStream.outputs.type.arity != 2:
|
|
|
|
warn "Logging configuration options not enabled in the current build"
|
|
|
|
else:
|
|
|
|
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
|
|
|
|
proc writeAndFlush(f: File, msg: LogOutputStr) =
|
|
|
|
try:
|
|
|
|
f.write(msg)
|
|
|
|
f.flushFile()
|
|
|
|
except IOError as err:
|
|
|
|
logLoggingFailure(cstring(msg), err)
|
|
|
|
|
|
|
|
proc stdoutFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, msg)
|
|
|
|
|
|
|
|
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, stripAnsi(msg))
|
|
|
|
|
|
|
|
defaultChroniclesStream.outputs[1].writer = noOutput
|
|
|
|
|
|
|
|
defaultChroniclesStream.outputs[0].writer =
|
|
|
|
case conf.logFormat:
|
|
|
|
of LogKind.Auto:
|
|
|
|
if isatty(stdout):
|
|
|
|
stdoutFlush
|
|
|
|
else:
|
|
|
|
noColorsFlush
|
|
|
|
of LogKind.Colors: stdoutFlush
|
|
|
|
of LogKind.NoColors: noColorsFlush
|
|
|
|
of LogKind.Json:
|
|
|
|
defaultChroniclesStream.outputs[1].writer = stdoutFlush
|
|
|
|
noOutput
|
|
|
|
of LogKind.None:
|
|
|
|
noOutput
|
|
|
|
|
2022-11-01 20:05:40 +00:00
|
|
|
try:
|
|
|
|
updateLogLevel(conf.logLevel)
|
|
|
|
except ValueError as err:
|
|
|
|
try:
|
|
|
|
stderr.write "Invalid value for --log-level. " & err.msg & "\n"
|
|
|
|
except IOError:
|
|
|
|
echo "Invalid value for --log-level. " & err.msg
|
|
|
|
quit QuitFailure
|
2022-04-14 10:49:03 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
proc setupMetrics*(config: CodexConf) =
|
2022-04-14 10:49:03 +00:00
|
|
|
if config.metricsEnabled:
|
|
|
|
let metricsAddress = config.metricsAddress
|
|
|
|
notice "Starting metrics HTTP server",
|
|
|
|
url = "http://" & $metricsAddress & ":" & $config.metricsPort & "/metrics"
|
|
|
|
try:
|
|
|
|
startMetricsHttpServer($metricsAddress, config.metricsPort)
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert exc.msg
|
|
|
|
except Exception as exc:
|
|
|
|
raiseAssert exc.msg # TODO fix metrics
|