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 , text >= 1.2.1.3
, these , these
, time , time
, unix
, haskell-tree-sitter , haskell-tree-sitter
, c , c
, go , go

View File

@ -6,6 +6,8 @@ import qualified Data.Time.Format as Time
import qualified Data.Time.LocalTime as LocalTime import qualified Data.Time.LocalTime as LocalTime
import System.Console.ANSI import System.Console.ANSI
import System.IO (hIsTerminalDevice) import System.IO (hIsTerminalDevice)
import System.Posix.Process
import System.Posix.Types
import Text.Show import Text.Show
import Text.Printf import Text.Printf
@ -30,9 +32,10 @@ data Level
logfmtFormatter :: Options -> Message -> String logfmtFormatter :: Options -> Message -> String
logfmtFormatter Options{..} (Message level message pairs time) = logfmtFormatter Options{..} (Message level message pairs time) =
showPairs showPairs
( kv "time" (showTime time) ( kv "time" (showTime time)
: kv "msg" (shows message) : kv "msg" (shows message)
: kv "level" (shows level) : kv "level" (shows level)
: kv "pid" (shows optionsProcessID)
: (uncurry kv . second shows <$> pairs) ) : (uncurry kv . second shows <$> pairs) )
. showChar '\n' $ "" . showChar '\n' $ ""
where 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). { 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. , optionsLevel :: Maybe Level -- ^ What level of messages to log. 'Nothing' disabled logging.
, optionsPrintSource :: Bool -- ^ Whether to print the source reference when logging errors. , optionsPrintSource :: Bool -- ^ Whether to print the source reference when logging errors.
, optionsIsTerminal :: Bool -- ^ Whether a terminal is attached. , optionsIsTerminal :: Bool -- ^ Whether a terminal is attached (set automaticaly at runtime).
, optionsFormatter :: Options -> Message -> String -- ^ Log formatter to use. , optionsFormatter :: Options -> Message -> String -- ^ Log formatter to use (set automaticaly at runtime).
, optionsProcessID :: CPid -- ^ ProcessID (set automaticaly at runtime).
} }
defaultOptions :: Options defaultOptions :: Options
@ -77,14 +81,17 @@ defaultOptions = Options
, optionsPrintSource = False , optionsPrintSource = False
, optionsIsTerminal = False , optionsIsTerminal = False
, optionsFormatter = logfmtFormatter , optionsFormatter = logfmtFormatter
, optionsProcessID = 0
} }
configureOptionsForHandle :: Handle -> Options -> IO Options configureOptionsForHandle :: Handle -> Options -> IO Options
configureOptionsForHandle handle options = do configureOptionsForHandle handle options = do
pid <- getProcessID
isTerminal <- hIsTerminalDevice handle isTerminal <- hIsTerminalDevice handle
pure $ options pure $ options
{ optionsIsTerminal = isTerminal { optionsIsTerminal = isTerminal
, optionsFormatter = if isTerminal then terminalFormatter else logfmtFormatter , optionsFormatter = if isTerminal then terminalFormatter else logfmtFormatter
, optionsProcessID = pid
} }
withSGRCode :: Bool -> [SGR] -> ShowS -> ShowS 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)] <*> 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.") (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.") <*> switch (long "print-source" <> help "Include source references in logged errors where applicable.")
<*> pure False -- The rest of the logging options are set automatically at runtime.
<*> pure Log.logfmtFormatter <*> pure False -- IsTerminal
<*> pure Log.logfmtFormatter -- Formatter
<*> pure 0 -- ProcessID
argumentsParser = (. Task.writeToOutput) . (>>=) argumentsParser = (. Task.writeToOutput) . (>>=)
<$> hsubparser (diffCommand <> parseCommand) <$> hsubparser (diffCommand <> parseCommand)
<*> ( Right <$> strOption (long "output" <> short 'o' <> help "Output path, defaults to stdout") <*> ( Right <$> strOption (long "output" <> short 'o' <> help "Output path, defaults to stdout")