Created
October 26, 2012 18:56
-
-
Save MichaelBlume/3960711 to your computer and use it in GitHub Desktop.
Cute script for drawing attention to errors in scrolling log output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
import Data.List | |
import Control.Concurrent | |
import System.IO | |
takeBlock :: [String] -> (String, [String]) | |
takeBlock (firstLine:moreLines) = (block, restLines) where | |
block = intercalate "\n" (firstLine:indLines) | |
(indLines, restLines) = span (isPrefixOf " ") moreLines | |
data PrintOrSleep = Print String | |
| Sleep Int | |
processBlock :: String -> [PrintOrSleep] -> [PrintOrSleep] | |
processBlock block acts = Print block : restActs where | |
restActs = if "ERROR" `isInfixOf` block | |
then Sleep 5000000 : acts | |
else acts | |
makeBlocks :: [String] -> [String] | |
makeBlocks [] = [] | |
makeBlocks lines = block : makeBlocks remLines where | |
(block, remLines) = takeBlock lines | |
processStream :: String -> [PrintOrSleep] | |
processStream = foldr processBlock [] . makeBlocks . lines | |
actOn :: PrintOrSleep -> IO() | |
actOn (Sleep usecs) = threadDelay usecs | |
actOn (Print block) = putStrLn block | |
main :: IO () | |
main = do | |
hSetBuffering stdout NoBuffering | |
input <- getContents | |
mapM_ actOn $ processStream input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically just do
yourprogram | ./escroll.hs
mostly it'll just echo the output of your program to the screen, but on any line containing "ERROR", it'll pause for five seconds, then scramble to catch up.
Indented lines are assumed to be part of the line above them -- ie for tracebacks and the like.