-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdump-main.hs
110 lines (98 loc) · 3.61 KB
/
dump-main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Main where
import System.Directory
import System.FilePath
import System.Console.GetOpt
import System.Environment
import System.Exit
import System.IO
import qualified Data.Map as M
import Data.Version (showVersion)
import Data.Maybe
import Control.Monad
import Data.Char
import Data.List.TakeR
import Data.Time.LocalTime
import TimeLog
import Data
import CommonStartup
import DumpFormat
import Paths_arbtt (version)
data Options = Options
{ optLogFile :: String
, optFormat :: DumpFormat
, optFirst :: Maybe Int
, optLast :: Maybe Int
}
defaultOptions dir = Options
{ optLogFile = dir </> "capture.log"
, optFormat = DFHuman
, optFirst = Nothing
, optLast = Nothing
}
versionStr = "arbtt-dump " ++ showVersion version
header = "Usage: arbtt-dump [OPTIONS...]"
options :: [OptDescr (Options -> IO Options)]
options =
[ Option "h?" ["help"]
(NoArg $ \_ -> do
putStr (usageInfo header options)
exitSuccess
)
"show this help"
, Option "V" ["version"]
(NoArg $ \_ -> do
putStrLn versionStr
exitSuccess
)
"show the version number"
, Option "f" ["logfile"]
(ReqArg (\arg opt -> return opt { optLogFile = arg }) "FILE")
"use this file instead of ~/.arbtt/capture.log"
, Option "t" ["format"]
(ReqArg (\arg opt ->
case readDumpFormat arg of
Just fm -> return $ opt { optFormat = fm}
Nothing -> do
hPutStrLn stderr ("Invalid format \"" ++ arg ++ "\".")
hPutStr stderr (usageInfo header options)
exitFailure) "FORMAT")
"output format, one of Human (default), Show or JSON "
, Option "i" ["first"]
(ReqArg (\arg opt ->
case reads arg of
[(n, "")] | n >= 0 -> return $ opt { optFirst = Just n }
_ -> do
hPutStrLn stderr ("Invalid number \"" ++ arg ++ "\".")
hPutStr stderr (usageInfo header options)
exitFailure) "NUMBER")
"only dump the first NUMBER of samples."
, Option "l" ["last"]
(ReqArg (\arg opt ->
case reads arg of
[(n, "")] | n >= 0 -> return $ opt { optLast = Just n }
_ -> do
hPutStrLn stderr ("Invalid number \"" ++ arg ++ "\".")
hPutStr stderr (usageInfo header options)
exitFailure) "NUMBER")
"only dump the last NUMBER of samples."
]
main = do
commonStartup
args <- getArgs
actions <- case getOpt Permute options args of
(o,[],[]) -> return o
(_,_,errs) -> do
hPutStr stderr (concat errs ++ usageInfo header options)
exitFailure
dir <- getAppUserDataDirectory "arbtt"
flags <- foldl (>>=) (return (defaultOptions dir)) actions
captures <- readTimeLog (optLogFile flags) :: IO (TimeLog CaptureData)
captures <- case (optFirst flags, optLast flags) of
(Nothing, Nothing) -> return captures
(Just n , Nothing) -> return $ take n captures
(Nothing, Just n ) -> return $ takeR n captures
(Just _ , Just _ ) -> do
hPutStrLn stderr "--first and --last are mutually exclusive"
hPutStr stderr (usageInfo header options)
exitFailure
dumpSamples (optFormat flags) captures