<commit_before><?php

namespace ByJG\Daemon;

use Exception;

class Runner
{

    const SLEEP_SERVICE = 1000;

    const BASE_LOG_PATH = "/var/log/daemonize";

    protected $stdIn = STDIN;
    protected $stdOut = STDOUT;
    protected $stdErr = STDERR;

    protected $className = null;
    protected $methodName = null;
    protected $instance = null;

    protected $daemon = true;

    public function __construct($object, $svcName, $consoleArgs = [], $daemon = true)
    {
        $this->daemon = $daemon;

        $arr = explode("::", $object);
        $className = $this->className = $arr[0];
        $this->methodName = $arr[1];

        // Prepare environment
        if ($this->daemon) {
            $this->prepareLogs($svcName);
        }

        try {
            $this->extractQueryParameters($consoleArgs);

            // Instantiate the class
            $this->instance = new $className();
        } catch (Exception $ex) {
            $this->writeToStderr("System Fail to Start: \n");
            $this->writeToStderr('Message: ' . $ex->getMessage() . "\n");
            $this->writeToStderr("Stack Trace:\n" . $ex->getTraceAsString());
            $this->writeToStderr("\n\n");
        }
    }

    protected function prepareLogs($logname = null)
    {
        if (!file_exists(self::BASE_LOG_PATH)) {
            mkdir(self::BASE_LOG_PATH);
        }

        if (empty($logname)) {
            $logname = str_replace("\\", "_", strtolower($this->className)) . "." . strtolower($this->methodName);
        }

        fclose(STDIN);
        fclose(STDOUT);
        fclose(STDERR);
        $this->stdIn = fopen('/dev/null', 'r');
        $this->stdOut = fopen(self::BASE_LOG_PATH . '/' . $logname . '.log', 'ab');
        $this->stdErr = fopen(self::BASE_LOG_PATH . '/' . $logname . '.error.log', 'ab');
    }

    protected function extractQueryParameters($consoleArgs)
    {
        // Read parameters and convert to PHP $_GET variables
        foreach ($consoleArgs as $pair) {<commit_msg>Adjusts to see log.<commit_after><?php

namespace ByJG\Daemon;

use Exception;

class Runner
{

    const SLEEP_SERVICE = 1000;

    const BASE_LOG_PATH = "/var/log/daemonize";

    protected $stdIn = STDIN;
    protected $stdOut = STDOUT;
    protected $stdErr = STDERR;

    protected $className = null;
    protected $methodName = null;
    protected $instance = null;

    protected $daemon = true;

    public function __construct($object, $svcName, $consoleArgs = [], $daemon = true)
    {
        $this->daemon = $daemon;

        $arr = explode("::", $object);
        $className = $this->className = $arr[0];
        $this->methodName = $arr[1];

        // Prepare environment
        if ($this->daemon) {
            $this->prepareLogs($svcName);
        }

        try {
            $this->extractQueryParameters($consoleArgs);

            // Instantiate the class
            if (!class_exists($className)) {
                throw new \Exception("Could not found the class $className");
            }
            $this->instance = new $className();
        } catch (Exception $ex) {
            $this->writeToStderr("System Fail to Start: \n");
            $this->writeToStderr('Message: ' . $ex->getMessage() . "\n");
            $this->writeToStderr("Stack Trace:\n" . $ex->getTraceAsString());
            $this->writeToStderr("\n\n");
        }
    }

    protected function prepareLogs($logname = null)
    {
        if (!file_exists(self::BASE_LOG_PATH)) {
            mkdir(self::BASE_LOG_PATH);
        }

        if (empty($logname)) {
            $logname = str_replace("\\", "_", strtolower($this->className)) . "." . strtolower($this->methodName);
        }

        if (
            is_writable(self::BASE_LOG_PATH . '/' . $logname . '.log')
            && is_writable(self::BASE_LOG_PATH . '/' . $logname . '.error.log')
        ) {
            fclose(STDIN);
            fclose(STDOUT);
            fclose(STDERR);
            $this->stdIn = fopen('/dev/null', 'r');
            $this->stdOut = fopen(self::BASE_LOG_PATH . '/' . $logname . '.log', 'ab');
            $this->stdErr = fopen(self::BASE_LOG_PATH . '/' . $logname . '.error.log', 'ab');
        }
    }

    protected function extractQueryParameters($consoleArgs)
    {
        // Read parameters and convert to PHP $_GET variables
        foreach ($consoleArgs as $pair) {
{'fraction_non_alphanumeric': 0.15748571428571428, 'fraction_numerical': 0.002742857142857143, 'mean_word_length': 2.141421392677674, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5166438', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1327, 'n_words': 266}