<commit_before>
{-# LANGUAGE OverloadedStrings #-}


-- System grade imports
-- import qualified Data.ByteString            as B
-- import qualified Data.ByteString.Builder    as Bu
-- import           Data.ByteString.Char8      (pack)
-- import qualified Data.ByteString.Lazy       as BL
-- import           Data.Foldable              (foldMap)
import           Data.Monoid
import           Text.Printf                (printf)

-- import           System.Directory
-- import           System.FilePath
import           System.IO
-- import           System.Process

import           Options.Applicative

-- Logging utilities
import           System.Log.Logger
import           System.Log.Handler        (setFormatter)
import           System.Log.Handler.Simple
import           System.Log.Formatter      (simpleLogFormatter)

-- Imports from other parts of the program

import           Rede.MainLoop.ConfigHelp   (mimicDataDir)
import           Rede.Research.Main         (research)


-- What is the program going to do?
data ProgramAction = 
    ResearchUrl_PA -- Wait for a POST request from the browser


actionStrToAction :: String -> ProgramAction
actionStrToAction "research"     = ResearchUrl_PA
actionStrToAction _              = error "Action doesn't exist"


data Program  = Program {
    action   :: ProgramAction
    -- ,harFileName :: String
    }


programParser :: Parser Program
programParser = Program <$> (
    actionStrToAction <$>
        strOption
             ( long "action"
                <> metavar "ACTION"
                <> help    "What's the program going to do: output-hosts, serve or research url" )
    ) 


main :: IO ()
main = do
    configureLogging
    mimic_dir <- mimicDataDir
    infoM "RehMimic" $ printf "Mimic dir: \"%s\"" mimic_dir
    prg   <-  execParser opts_metadata
    case Main.action prg of 

        ResearchUrl_PA       -> do 
            research mimic_dir


  where 
    opts_metadata = info 
        ( helper <*> programParser )
        ( fullDesc 
            <>  progDesc "Mimics web servers from .har file"
            <>  header   "reh-mimic"
            )


configureLogging :: IO ()
configureLogging = do 
    s <- streamHandler stderr DEBUG  >>= 
        \lh -> return $ setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
    updateGlobalLogger rootLoggerName removeHandler
    updateGlobalLogger "HTTP2.Session" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel ERROR  
        )
    updateGlobalLogger "OpenSSL" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
    updateGlobalLogger "HarWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
    updateGlobalLogger "ResearchWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
<commit_msg>Configured logging through syslog<commit_after>
{-# LANGUAGE OverloadedStrings #-}


-- System grade imports
-- import qualified Data.ByteString            as B
-- import qualified Data.ByteString.Builder    as Bu
-- import           Data.ByteString.Char8      (pack)
-- import qualified Data.ByteString.Lazy       as BL
import           Data.Foldable              (find)
import           Data.Monoid
import           Text.Printf                (printf)

-- import           System.Directory
-- import           System.FilePath
import           System.IO
import           System.Environment        (getEnvironment)
-- import           System.Process

import           Options.Applicative

-- Logging utilities
import           System.Log.Logger
import           System.Log.Handler        (setFormatter)
import           System.Log.Handler.Simple
import           System.Log.Formatter      (simpleLogFormatter)
import           System.Log.Handler.Syslog (openlog, Facility(..), Option(..) )

-- Imports from other parts of the program

import           Rede.MainLoop.ConfigHelp   (mimicDataDir)
import           Rede.Research.Main         (research)


-- What is the program going to do?
data ProgramAction = 
    ResearchUrl_PA -- Wait for a POST request from the browser


actionStrToAction :: String -> ProgramAction
actionStrToAction "research"     = ResearchUrl_PA
actionStrToAction _              = error "Action doesn't exist"


data Program  = Program {
    action   :: ProgramAction
    -- ,harFileName :: String
    }


programParser :: Parser Program
programParser = Program <$> (
    actionStrToAction <$>
        strOption
             ( long "action"
                <> metavar "ACTION"
                <> help    "What's the program going to do: only \"research\" is supported now." )
        )    


main :: IO ()
main = do
    env_vars <- getEnvironment
    case find (\ (name, _) -> name == "MIMIC_CONSOLE_LOGGER") env_vars of 

        Nothing                                         -> configureLoggingToSyslog 

        Just (_, x) | not $ (length x) == 0 || x == "0" -> configureLoggingToConsole

                    | otherwise                         -> configureLoggingToSyslog


    mimic_dir <- mimicDataDir
    infoM "RehMimic" $ printf "Mimic dir: \"%s\"" mimic_dir
    prg   <-  execParser opts_metadata
    case Main.action prg of 

        ResearchUrl_PA       -> do 
            research mimic_dir


  where 
    opts_metadata = info 
        ( helper <*> programParser )
        ( fullDesc 
            <>  progDesc 
                ( "Mimics web servers. Use environmnet variables MIMIC_DATA_DIR to point to where the scratch directory" ++
                  "of the server is. Use the variable MIMIC_CONSOLE_LOGGER with value 1 to log messages to console; otherwise " ++
                  "they are going to syslog. " )

            <>  header   "reh-mimic"
            )


configureLoggingToConsole :: IO ()
configureLoggingToConsole = do 
    s <- streamHandler stderr DEBUG  >>= 
        \lh -> return $ setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
    updateGlobalLogger rootLoggerName removeHandler
    updateGlobalLogger "HTTP2.Session" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel ERROR  
        )
    updateGlobalLogger "OpenSSL" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel INFO  
        )
    updateGlobalLogger "HarWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
    updateGlobalLogger "ResearchWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )


configureLoggingToSyslog :: IO ()
configureLoggingToSyslog = do 
    s <- openlog "RehMimic" [PID] DAEMON INFO >>= 
        \lh -> return $ setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
    updateGlobalLogger rootLoggerName removeHandler
    updateGlobalLogger "HTTP2.Session" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel ERROR  
        )
    updateGlobalLogger "OpenSSL" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel INFO  
        )
    updateGlobalLogger "HarWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
    updateGlobalLogger "ResearchWorker" (
        setHandlers [s] .  -- Remember that composition works in reverse...
        setLevel DEBUG  
        )
{'fraction_non_alphanumeric': 0.08325660923319743, 'fraction_numerical': 0.0010522162304353545, 'mean_word_length': 1.9302504816955683, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3175117', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1982, 'n_words': 600}