diff options
author | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-11-03 18:33:34 +0100 |
---|---|---|
committer | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-11-03 18:33:34 +0100 |
commit | 99db29942570e5acf82c236757b62253c1c6f803 (patch) | |
tree | e28d21c78f7cc9520ef05e9f8d42c44666439d86 /src/Plugins/Monitors/Batt.hs | |
parent | 67eee0be14c2966e9dcf6f4127e75da16b9942b7 (diff) | |
download | xmobar-99db29942570e5acf82c236757b62253c1c6f803.tar.gz xmobar-99db29942570e5acf82c236757b62253c1c6f803.tar.bz2 |
Battery monitor: better handling of exceptions
An attempt to fix #60, that is, crashes after power resumes due to
unreadable battery stats files.
Diffstat (limited to 'src/Plugins/Monitors/Batt.hs')
-rw-r--r-- | src/Plugins/Monitors/Batt.hs | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs index c276e6b..dfa8075 100644 --- a/src/Plugins/Monitors/Batt.hs +++ b/src/Plugins/Monitors/Batt.hs @@ -15,12 +15,14 @@ module Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where -import qualified Data.ByteString.Lazy.Char8 as B +import Control.Exception (SomeException, handle) import Plugins.Monitors.Common import System.FilePath ((</>)) import System.Posix.Files (fileExist) import System.Console.GetOpt +import qualified Data.ByteString.Lazy.Char8 as B + data BattOpts = BattOpts { onString :: String , offString :: String @@ -107,11 +109,9 @@ batteryFiles bat = haveAc :: FilePath -> IO Bool haveAc f = do - exists <- fileExist ofile - if exists - then fmap ((== "1\n") . B.unpack) (B.readFile ofile) - else return False + handle onError (fmap ((== "1\n") . B.unpack) (B.readFile ofile)) where ofile = sysDir </> f + onError = const (return False) :: SomeException -> IO Bool readBattery :: Files -> IO Battery readBattery NoFiles = return $ Battery 0 0 0 0 @@ -124,7 +124,8 @@ readBattery files = (3600 * b / 1000000) -- wattseconds (c / 1000000) -- volts (if c > 0 then (d / c) else -1) -- amperes - where grab f = catch (fmap (read . B.unpack) $ B.readFile f) (\_ -> return 0) + where grab f = handle onError (fmap (read . B.unpack) $ B.readFile f) + onError = const (return 0) :: SomeException -> IO Float readBatteries :: BattOpts -> [Files] -> IO Result readBatteries opts bfs = |