2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# 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.
|
2020-03-23 06:03:36 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-03-09 12:22:52 +00:00
|
|
|
|
2020-03-23 06:03:36 +00:00
|
|
|
import stew/byteutils
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
template public* {.pragma.}
|
|
|
|
|
2020-03-23 06:03:36 +00:00
|
|
|
const
|
|
|
|
ShortDumpMax = 12
|
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
func shortLog*(item: openArray[byte]): string =
|
2020-03-23 06:03:36 +00:00
|
|
|
if item.len <= ShortDumpMax:
|
|
|
|
result = item.toHex()
|
|
|
|
else:
|
|
|
|
const
|
|
|
|
split = ShortDumpMax div 2
|
|
|
|
dumpLen = (ShortDumpMax * 2) + 3
|
|
|
|
result = newStringOfCap(dumpLen)
|
|
|
|
result &= item.toOpenArray(0, split - 1).toHex()
|
|
|
|
result &= "..."
|
|
|
|
result &= item.toOpenArray(item.len - split, item.high).toHex()
|
|
|
|
|
|
|
|
func shortLog*(item: string): string =
|
|
|
|
if item.len <= ShortDumpMax:
|
|
|
|
result = item
|
|
|
|
else:
|
|
|
|
const
|
|
|
|
split = ShortDumpMax div 2
|
|
|
|
dumpLen = ShortDumpMax + 3
|
|
|
|
result = newStringOfCap(dumpLen)
|
|
|
|
result &= item[0..<split]
|
|
|
|
result &= "..."
|
|
|
|
result &= item[(item.len - split)..item.high]
|
2021-01-08 05:21:24 +00:00
|
|
|
|
|
|
|
when defined(libp2p_agents_metrics):
|
2021-03-15 01:42:47 +00:00
|
|
|
import strutils
|
|
|
|
export split
|
|
|
|
|
|
|
|
import stew/results
|
|
|
|
export results
|
|
|
|
|
|
|
|
proc safeToLowerAscii*(s: string): Result[string, cstring] =
|
|
|
|
try:
|
|
|
|
ok(s.toLowerAscii())
|
2021-03-15 07:48:11 +00:00
|
|
|
except CatchableError:
|
2021-03-15 01:42:47 +00:00
|
|
|
err("toLowerAscii failed")
|
|
|
|
|
2021-01-08 05:21:24 +00:00
|
|
|
const
|
|
|
|
KnownLibP2PAgents* {.strdefine.} = ""
|
2021-03-15 01:42:47 +00:00
|
|
|
KnownLibP2PAgentsSeq* = KnownLibP2PAgents.safeToLowerAscii().tryGet().split(",")
|