-- Copyright 2013 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

-- | Format and display log msgs.
--
-- TODO: formatting options
module LogView.LogCat where
import Control.Monad
import qualified Data.ByteString as ByteString
import qualified Data.Text.IO as Text.IO
import qualified System.Environment as Environment
import qualified System.IO as IO

import qualified Util.Log as Log
import qualified LogView.Tail as Tail


main :: IO ()
main :: IO ()
main = do
    [String]
args <- IO [String]
Environment.getArgs
    Handle
hdl <- case [String]
args of
        [] -> forall (m :: * -> *) a. Monad m => a -> m a
return Handle
IO.stdin
        [String
fn] -> String -> IOMode -> IO Handle
IO.openFile String
fn IOMode
IO.ReadMode
        [String]
_ -> forall a. HasCallStack => String -> a
error String
"usage: logcat [filename]"
    Handle -> BufferMode -> IO ()
IO.hSetBuffering Handle
hdl BufferMode
IO.LineBuffering
    forall (f :: * -> *) a b. Applicative f => f a -> f b
forever forall a b. (a -> b) -> a -> b
$ do
        ByteString
line <- Handle -> IO ByteString
ByteString.hGetLine Handle
hdl
        let msg :: Msg
msg = ByteString -> Msg
Tail.deserialize_line ByteString
line
        Text -> IO ()
Text.IO.putStrLn (Msg -> Text
Log.format_msg Msg
msg)