1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 01:47:01 +03:00

Log process ID

This commit is contained in:
Timothy Clem 2017-07-31 08:56:17 -07:00
parent 79b5509b00
commit 87b2662b02
3 changed files with 15 additions and 5 deletions

View File

@ -116,6 +116,7 @@ library
, text >= 1.2.1.3
, these
, time
, unix
, haskell-tree-sitter
, c
, go

View File

@ -6,6 +6,8 @@ import qualified Data.Time.Format as Time
import qualified Data.Time.LocalTime as LocalTime
import System.Console.ANSI
import System.IO (hIsTerminalDevice)
import System.Posix.Process
import System.Posix.Types
import Text.Show
import Text.Printf
@ -30,9 +32,10 @@ data Level
logfmtFormatter :: Options -> Message -> String
logfmtFormatter Options{..} (Message level message pairs time) =
showPairs
( kv "time" (showTime time)
( kv "time" (showTime time)
: kv "msg" (shows message)
: kv "level" (shows level)
: kv "pid" (shows optionsProcessID)
: (uncurry kv . second shows <$> pairs) )
. showChar '\n' $ ""
where
@ -66,8 +69,9 @@ data Options = Options
{ optionsEnableColour :: Bool -- ^ Whether to enable colour formatting for logging (Only works when logging to a terminal that supports ANSI colors).
, optionsLevel :: Maybe Level -- ^ What level of messages to log. 'Nothing' disabled logging.
, optionsPrintSource :: Bool -- ^ Whether to print the source reference when logging errors.
, optionsIsTerminal :: Bool -- ^ Whether a terminal is attached.
, optionsFormatter :: Options -> Message -> String -- ^ Log formatter to use.
, optionsIsTerminal :: Bool -- ^ Whether a terminal is attached (set automaticaly at runtime).
, optionsFormatter :: Options -> Message -> String -- ^ Log formatter to use (set automaticaly at runtime).
, optionsProcessID :: CPid -- ^ ProcessID (set automaticaly at runtime).
}
defaultOptions :: Options
@ -77,14 +81,17 @@ defaultOptions = Options
, optionsPrintSource = False
, optionsIsTerminal = False
, optionsFormatter = logfmtFormatter
, optionsProcessID = 0
}
configureOptionsForHandle :: Handle -> Options -> IO Options
configureOptionsForHandle handle options = do
pid <- getProcessID
isTerminal <- hIsTerminalDevice handle
pure $ options
{ optionsIsTerminal = isTerminal
, optionsFormatter = if isTerminal then terminalFormatter else logfmtFormatter
, optionsProcessID = pid
}
withSGRCode :: Bool -> [SGR] -> ShowS -> ShowS

View File

@ -45,8 +45,10 @@ arguments = info (version <*> helper <*> ((,) <$> optionsParser <*> argumentsPar
<*> options [("error", Just Log.Error), ("warning", Just Log.Warning), ("info", Just Log.Info), ("debug", Just Log.Debug), ("none", Nothing)]
(long "log-level" <> value (Just Log.Warning) <> help "Log messages at or above this level, or disable logging entirely.")
<*> switch (long "print-source" <> help "Include source references in logged errors where applicable.")
<*> pure False
<*> pure Log.logfmtFormatter
-- The rest of the logging options are set automatically at runtime.
<*> pure False -- IsTerminal
<*> pure Log.logfmtFormatter -- Formatter
<*> pure 0 -- ProcessID
argumentsParser = (. Task.writeToOutput) . (>>=)
<$> hsubparser (diffCommand <> parseCommand)
<*> ( Right <$> strOption (long "output" <> short 'o' <> help "Output path, defaults to stdout")