-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated some text and wrote nicer printing
- Loading branch information
1 parent
6c0290d
commit 6d1154c
Showing
6 changed files
with
134 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module Helpers.Logger where | ||
|
||
import Network.Wai.Internal (Response(ResponseBuilder, ResponseFile), Request, pathInfo, requestMethod) | ||
import Data.List (intercalate) | ||
import Data.Text (unpack) | ||
import Network.HTTP.Types (Status(statusCode)) | ||
|
||
import Helpers.Utils (unpackBS) | ||
import Helpers.Globals (LogLevel (..), getLogLevel) | ||
|
||
colorStatus :: Int -> String | ||
colorStatus code | code < 300 = "\ESC[38;2;0;255;0m"++show code++"\ESC[0m" | ||
| code < 400 = "\ESC[38;2;255;255;0m"++show code++"\ESC[0m" | ||
| otherwise = "\ESC[38;2;255;0;0m"++show code++"\ESC[0m" | ||
|
||
getPath :: Request -> String | ||
getPath request = intercalate "/" $ map unpack $ pathInfo request | ||
|
||
up :: Int -> String | ||
up 0 = "" | ||
up n = "\ESC[" ++ show n ++ "A" | ||
|
||
left :: Int -> String | ||
left 0 = "" | ||
left n = "\ESC[" ++ show n ++ "D" | ||
|
||
down :: Int -> String | ||
down 0 = "" | ||
down n = "\ESC[" ++ show n ++ "B" | ||
|
||
right :: Int -> String | ||
right 0 = "" | ||
right n = "\ESC[" ++ show n ++ "C" | ||
|
||
tableify :: [String] -> String | ||
tableify (x:xs) = "| " ++ x ++ left l ++ right 20 ++ tableify xs | ||
where | ||
l = length x | ||
tableify [] = "|" | ||
|
||
|
||
info :: String -> IO () | ||
info input = do | ||
loglevel <- getLogLevel | ||
case loglevel of | ||
Info -> putStrLn $ "\ESC[38;2;100;100;100m" ++ input ++ "\ESC[0m" | ||
_ -> return () | ||
|
||
warning :: String -> IO () | ||
warning input = do | ||
loglevel <- getLogLevel | ||
case loglevel of | ||
Warning -> putStrLn $ "\ESC[38;2;255;255;0m" ++ input ++ "\ESC[0m" | ||
Info -> putStrLn $ "\ESC[38;2;255;255;0m" ++ input ++ "\ESC[0m" | ||
_ -> return () | ||
|
||
error :: String -> IO () | ||
error input = putStrLn $ "\ESC[38;2;255;0;0m" ++ input ++ "\ESC[0m" | ||
|
||
logger :: Request -> Response -> IO () | ||
logger request (ResponseBuilder status _ _) = do | ||
let method = unpackBS (requestMethod request) | ||
let path = getPath request | ||
putStrLn $ tableify [method, show $ statusCode status, path] | ||
logger request (ResponseFile status _ _ _) = do | ||
let method = unpackBS (requestMethod request) | ||
let path = getPath request | ||
putStrLn $ tableify [method, show $ statusCode status, path] | ||
logger request x = do | ||
let method = unpackBS (requestMethod request) | ||
let path = getPath request | ||
putStrLn $ tableify [method, path] |
This file contains 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
This file contains 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
This file contains 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