Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

[#345] logging configuration for logging to file #350

Merged
merged 10 commits into from
Nov 13, 2019
53 changes: 39 additions & 14 deletions cardano-launcher/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ module Main where
import Cardano.Prelude hiding (option)
import qualified Prelude

import Control.Exception.Safe (throwM)

-- Yes, we should use these seldomly but here it seems quite acceptable.
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Text.Lazy.Builder (fromString, fromText)

import Distribution.System (OS (Windows), buildOS)

import System.FilePath ((</>))
import System.Environment (setEnv)
import System.Exit (exitWith)
import System.IO.Silently (hSilence)
Expand All @@ -25,6 +29,9 @@ import Options.Applicative (Parser, ParserInfo, auto, execParser,
fullDesc, header, help, helper, info,
long, metavar, option, optional, progDesc)

import qualified Cardano.BM.Configuration.Model as CM
import Cardano.BM.Data.Output
import Cardano.BM.Data.Rotation
import Cardano.BM.Setup (withTrace)
import qualified Cardano.BM.Trace as Trace
import Cardano.BM.Tracing
Expand All @@ -41,11 +48,9 @@ import Cardano.Shell.Launcher (LoggingDependencies (..), TLSError,
TLSPath (..), WalletRunner (..),
generateTlsCertificates, runLauncher,
walletRunnerProcess)
import Cardano.Shell.Launcher.Types (nullLogging)
import Cardano.Shell.Update.Lib (UpdaterData (..),
runDefaultUpdateProcess)
import Control.Exception.Safe (throwM)

import System.FilePath ((</>))

--------------------------------------------------------------------------------
-- Main
Expand All @@ -72,7 +77,7 @@ main = silence $ do

-- This function either stubs out the wallet exit code or
-- returns the "real" function.
let walletExectionFunction =
let walletExecutionFunction =
WalletRunner $ \daedalusBin walletArguments -> do
-- Check if we have any exit codes remaining.
stubExitCodes <- readIORef walletTestExitCodesMVar
Expand Down Expand Up @@ -104,8 +109,37 @@ main = silence $ do
-- Otherwise run the real deal, the real function.
runDefaultUpdateProcess filePath arguments

-- We get the launcher options. We don't log them currently because of the cat-mouse deps.
launcherOptions <- do
eLauncherOptions <- getLauncherOptions nullLogging (launcherConfigPath launcherCLI)
case eLauncherOptions of
Left err -> do
putTextLn $
"Error occured while parsing configuration file: " <> show err
throwM $ LauncherOptionsError (show err)
Right lo -> pure lo

logConfig <- defaultConfigStdout
let logfilepath = lologsPrefix launcherOptions </> "launcher"

-- We configure the logging to be on stdout and in the file as well.
CM.setSetupScribes logConfig
[ScribeDefinition {
scName = toS logfilepath,
scFormat = ScText,
scKind = FileSK,
scPrivacy = ScPublic,
scRotation = Just $ RotationParameters
{ rpLogLimitBytes = 10000000
, rpMaxAgeHours = 24
, rpKeepFilesNum = 3
}
}]

CM.setDefaultScribes logConfig
[ "StdoutSK::text"
, "FileSK::" <> toS logfilepath
]

-- A safer way to close the tracing.
withTrace logConfig "launcher" $ \baseTrace -> do
Expand All @@ -122,15 +156,6 @@ main = silence $ do
setEnv "LC_ALL" "en_GB.UTF-8"
setEnv "LANG" "en_GB.UTF-8"

launcherOptions <- do
eLauncherOptions <- getLauncherOptions loggingDependencies (launcherConfigPath launcherCLI)
case eLauncherOptions of
Left err -> do
logErrorMessage baseTrace $
"Error occured while parsing configuration file: " <> show err
throwM $ LauncherOptionsError (show err)
Right lo -> pure lo

let lockFile = loStateDir launcherOptions </> "daedalus_lockfile"
Trace.logNotice baseTrace $ "Locking file so that multiple applications won't run at same time"
-- Check if it's locked or not. Will throw an exception if the
Expand Down Expand Up @@ -183,7 +208,7 @@ main = silence $ do
-- Finally, run the launcher once everything is set up!
exitCode <- runLauncher
loggingDependencies
walletExectionFunction
walletExecutionFunction
daedalusBin
updaterExecutionFunction
updaterData
Expand Down
1 change: 1 addition & 0 deletions cardano-launcher/cardano-launcher.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ executable cardano-launcher
, optparse-applicative
-- directory
, directory
, filepath

if os(windows)
ghc-options: -optl-mwindows
Expand Down
3 changes: 3 additions & 0 deletions cardano-launcher/src/Cardano/Shell/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ data LauncherOptions = LauncherOptions
, loWorkingDirectory :: !FilePath
, loStateDir :: !FilePath
-- On WIN it should set this directory as current.
, lologsPrefix :: !FilePath
} deriving (Show, Generic)

instance FromJSON LauncherOptions where
Expand All @@ -63,6 +64,7 @@ instance FromJSON LauncherOptions where
tlsPath <- o .:? "tlsPath"
workingDir <- o .: "workingDir"
stateDir <- o .: "stateDir"
logsPrefix <- o .: "logsPrefix"

pure $ LauncherOptions
configuration
Expand All @@ -73,6 +75,7 @@ instance FromJSON LauncherOptions where
daedalusBin
workingDir
stateDir
logsPrefix

-- | Configuration yaml file location and the key to use. The file should
-- parse to a MultiConfiguration and the 'cfoKey' should be one of the keys
Expand Down
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-launcher.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.