Skip to content

Commit

Permalink
CAD-2907 workbench: add locli, a log analysis tool
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfire committed Jun 18, 2021
1 parent f0eb0d3 commit 807a651
Show file tree
Hide file tree
Showing 21 changed files with 2,129 additions and 3 deletions.
1 change: 1 addition & 0 deletions cabal.project
Expand Up @@ -9,6 +9,7 @@ packages:
cardano-node-chairman
cardano-submit-api
nix/workbench/cardano-topology
nix/workbench/locli

package cardano-api
ghc-options: -Werror
Expand Down
2 changes: 1 addition & 1 deletion nix/haskell.nix
Expand Up @@ -84,7 +84,7 @@ let
}
{
# Stamp executables with the git revision and add shell completion
packages = lib.genAttrs ["cardano-node" "cardano-cli" "cardano-topology"] (name: {
packages = lib.genAttrs ["cardano-node" "cardano-cli" "cardano-topology" "locli" ] (name: {
components.exes.${name}.postInstall = ''
${lib.optionalString stdenv.hostPlatform.isWindows setLibSodium}
${setGitRev}
Expand Down
1 change: 1 addition & 0 deletions nix/pkgs.nix
Expand Up @@ -65,6 +65,7 @@ final: prev: with final;
inherit (cardanoNodeHaskellPackages.cardano-node.components.exes) cardano-node;
inherit (cardanoNodeHaskellPackages.cardano-cli.components.exes) cardano-cli;
inherit (cardanoNodeHaskellPackages.cardano-topology.components.exes) cardano-topology;
inherit (cardanoNodeHaskellPackages.locli.components.exes) locli;
inherit (cardanoNodeHaskellPackages.bech32.components.exes) bech32;
inherit (cardanoNodeHaskellPackages.cardano-submit-api.components.exes) cardano-submit-api;
cardano-node-profiled = cardanoNodeProfiledHaskellPackages.cardano-node.components.exes.cardano-node;
Expand Down
10 changes: 8 additions & 2 deletions nix/workbench/default.nix
Expand Up @@ -10,6 +10,7 @@
, customConfig
, cardano-cli
, cardano-topology
, locli

, useCabalRun
}:
Expand Down Expand Up @@ -56,6 +57,7 @@ let

cardano-cli
cardano-topology
locli
];

runWorkbench =
Expand Down Expand Up @@ -120,7 +122,11 @@ let
${exeCabalOp "run" "cardano-topology"} "$@"
}
export -f cardano-cli cardano-node cardano-topology
function locli() {
${exeCabalOp "exec" "locli"} "$@"
}
export -f cardano-cli cardano-node cardano-topology locli
''}
Expand All @@ -129,7 +135,7 @@ let
''
git log -n1 --alternate-refs --pretty=format:"%Cblue%h %Cred%cr %Cgreen%D %Cblue%s%Creset"
echo -n "workbench: prebuilding executables (because of useCabalRun):"
for exe in cardano-cli cardano-node cardano-topology
for exe in cardano-cli cardano-node cardano-topology locli
do echo -n " $exe"
${exeCabalOp "run" "$exe"} --help >/dev/null || return 1
done
Expand Down
13 changes: 13 additions & 0 deletions nix/workbench/locli/NOTICE
@@ -0,0 +1,13 @@
Copyright 2020 Input Output (Hong Kong) Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
18 changes: 18 additions & 0 deletions nix/workbench/locli/app/locli.hs
@@ -0,0 +1,18 @@
{-# LANGUAGE OverloadedStrings #-}

import Cardano.Prelude hiding (option)

import Control.Monad.Trans.Except.Exit (orDie)
import qualified Options.Applicative as Opt

import Cardano.Unlog.Parsers (opts, pref)
import Cardano.Unlog.Run (renderCommandError, runCommand)
import Cardano.TopHandler


main :: IO ()
main = toplevelExceptionHandler $ do

co <- Opt.customExecParser pref opts

orDie renderCommandError $ runCommand co
96 changes: 96 additions & 0 deletions nix/workbench/locli/locli.cabal
@@ -0,0 +1,96 @@
cabal-version: 2.4

name: locli
version: 1.27
description: Log parsing CLI.
author: IOHK
maintainer: operations@iohk.io
license: Apache-2.0
license-files:
NOTICE
build-type: Simple

library

hs-source-dirs: src

exposed-modules: Data.Accum
Data.Distribution

Cardano.Config.Git.Rev
Cardano.Config.Git.RevFromGit

Cardano.Profile

Cardano.TopHandler

Cardano.Unlog.Analysis
Cardano.Unlog.Commands
Cardano.Unlog.LogObject
Cardano.Unlog.Parsers
Cardano.Unlog.Resources
Cardano.Unlog.Run
Cardano.Unlog.SlotStats
Cardano.Unlog.Summary

other-modules: Paths_locli

build-depends: base
, aeson
, aeson-pretty
, attoparsec
, attoparsec-iso8601
, bytestring
, cardano-prelude
, containers
, directory
, filepath
, file-embed
, gnuplot
, Histogram
, iohk-monitoring
, optparse-applicative
, optparse-generic
, process
, scientific
, split
, template-haskell
, text
, text-short
, time
, transformers
, transformers-except
, unordered-containers
, utf8-string
, vector

default-language: Haskell2010
default-extensions: NoImplicitPrelude
OverloadedStrings
TupleSections

ghc-options: -Wall
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wredundant-constraints
-Wpartial-fields
-Wcompat
-Wno-all-missed-specialisations

executable locli
hs-source-dirs: app
main-is: locli.hs
default-language: Haskell2010
ghc-options: -threaded
-Wall
-rtsopts
"-with-rtsopts=-T"
build-depends: base
, cardano-prelude
, locli
, optparse-applicative
, text
, text-short
, transformers
, transformers-except
default-extensions: NoImplicitPrelude
37 changes: 37 additions & 0 deletions nix/workbench/locli/src/Cardano/Config/Git/Rev.hs
@@ -0,0 +1,37 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Cardano.Config.Git.Rev (
gitRev
) where

import Cardano.Prelude

import Data.FileEmbed (dummySpaceWith)
import qualified Data.Text as T

import Cardano.Config.Git.RevFromGit (gitRevFromGit)

gitRev :: Text
gitRev | gitRevEmbed /= zeroRev = gitRevEmbed
| T.null fromGit = zeroRev
| otherwise = fromGit
where
-- Git revision embedded after compilation using
-- Data.FileEmbed.injectWith. If nothing has been injected,
-- this will be filled with 0 characters.
gitRevEmbed :: Text
gitRevEmbed = decodeUtf8 $(dummySpaceWith "gitrev" 40)

-- Git revision found during compilation by running git. If
-- git could not be run, then this will be empty.
#if defined(arm_HOST_ARCH)
-- cross compiling to arm fails; due to a linker bug
fromGit = ""
#else
fromGit = T.strip (T.pack $(gitRevFromGit))
#endif

zeroRev :: Text
zeroRev = "0000000000000000000000000000000000000000"
26 changes: 26 additions & 0 deletions nix/workbench/locli/src/Cardano/Config/Git/RevFromGit.hs
@@ -0,0 +1,26 @@
module Cardano.Config.Git.RevFromGit (
gitRevFromGit
) where

import Cardano.Prelude
import Prelude (String)

import qualified Language.Haskell.TH as TH
import System.IO.Error (ioeGetErrorType, isDoesNotExistErrorType)
import System.Process (readProcessWithExitCode)

-- | Git revision found by running git rev-parse. If git could not be
-- executed, then this will be an empty string.
gitRevFromGit :: TH.Q TH.Exp
gitRevFromGit = TH.LitE . TH.StringL <$> TH.runIO runGitRevParse
where
runGitRevParse :: IO String
runGitRevParse = handleJust missingGit (const $ pure "") $ do
(exitCode, output, _) <-
readProcessWithExitCode "git" ["rev-parse", "--verify", "HEAD"] ""
pure $ case exitCode of
ExitSuccess -> output
_ -> ""

missingGit e = if isDoesNotExistErrorType (ioeGetErrorType e) then Just () else Nothing

0 comments on commit 807a651

Please sign in to comment.