     * @param $message
     */
    protected function logAll($message)
    {
        $this->log($message, "[Sugar Repairs][{$this->cycle_id}][{$this->loggerTitle}][All] ", "All");
    }

    /**
     * Logs a change for the user to view
     * @param $message
     */
    protected function logChange($message)
    {
        $this->log($message, "[Sugar Repairs][{$this->cycle_id}][{$this->loggerTitle}][Change] ", "Change");
    }

    /**
     * Logs an action the user will have to do
     * @param $message
     */
    protected function logAction($message)
    {
        $this->log($message, "[Sugar Repairs][{$this->cycle_id}][{$this->loggerTitle}][Action] ", "Action");
    }

    /**
     * Logger Function
     * @param $message
     * @param string $prefix
     * @param string $type ('All', 'Combined, 'Action', 'Change')
     */
    protected function log($message, $prefix = '', $type = 'Combined')
    {
        if (empty($prefix)) {
            $log = "[Sugar Repairs][{$this->cycle_id}][{$this->loggerTitle}] ";
        } else {
            $log = $prefix;
        }

        $log.= $message;

        //$GLOBALS['log']->fatal($log);
        $this->logThis($log, $type);

        if (php_sapi_name() === 'cli') {
            if (
                isset($_SERVER['argv'][0])
                && (
                    strpos($_SERVER['argv'][0], 'phpunit')!== FALSE
                    || $_SERVER['argv'][0] == '/usr/bin/phpunit'
                )
            ) {
                //Capture the log file for unit tests
                $this->unitTestLog[] = $log;
            } else {
                echo $log. "\n";
            }
        }
    }

    /**
     * Flat File Logging System
     * @param string $entry
     * @param string $type ('All', 'Combined, 'Action', 'Change')
     * @param string $path
     */
    function logThis($entry, $type = 'Combined', $path = '')
    {
        if (file_exists('include/utils/sugar_file_utils.php')) {
            require_once('include/utils/sugar_file_utils.php');
        }

        //Make sure all messages end in a CR
        if (substr($entry, -1)!= "\n") {
            $entry.= "\n";
        }

        if ($type == 'All') {
            $fileArray = array('Combined', "Action", "Change");
        } else {
            //We always write to the 'Combined' file
            $fileArray = array_unique(array($type, 'Combined'));
        }

        foreach ($fileArray as $fileType) {
            $log = empty($path)? "SugarRepairModule-{$fileType}-{$this->cycle_id}.log" : $path;

            // create if not exists
            $fp = @fopen($log, 'a+');
            if (!is_resource($fp)) {
                $GLOBALS['log']->fatal('SugarRepairModule could not open/lock SugarRepairModule.log file');
                die('SugarRepairModule could not open/lock SugarRepairModule.log file');
            }

            if (@fwrite($fp, $entry) === false) {
                $GLOBALS['log']->fatal('SugarRepairModule could not write to SugarRepairModule.log: '. $entry);
                die('SugarRepairModule could not write to SugarRepairModule.log');
            }

            if (is_resource($fp)) {
                fclose($fp);
            }
        }
    }

    /**
     * Captures a database record
     * @param $cycle_id
     * @param $repair_type
     * @param $target_type
     * @param $target
     * @param $value_before
     * @param $value_after
     * @param $description
     * @param $status
     * @param $priority
     */
    protected function capture($cycle_id, $repair_type, $target_type, $target, $value_before, $value_after, $description, $status, $priority)
    {
        $sugarRepair = BeanFactory::newBean('supp_SugarRepairs');
        $sugarRepair->name = $target;
        $sugarRepair->cycle_id = $cycle_id;
        $sugarRepair->type = $repair_type;
        $sugarRepair->target_type = $target_type;
        $sugarRepair->target = $target;
        $sugarRepair->value_before = $value_before;
        $sugarRepair->value_after = $value_after;
        $sugarRepair->description = $description;
        $sugarRepair->status = $status;
        $sugarRepair->priority = $priority;
        $sugarRepair->save();
    }

    /**
     * Returns the custom language directories in Sugar.
     * @return string
     */
    protected function getLangRegex()
    {
        $langRegexes = array(
            //include
            '(\\/|\\\)custom(\\/|\\\)include(\\/|\\\)language(\\/|\\\)(.*?)\.lang.php$',
            //application extensions
            '(\\/|\\\)custom(\\/|\\\)Extension(\\/|\\\)application(\\/|\\\)Ext(\\/|\\\)Language(\\/|\\\)(.*?)\.php$',
            //module extensions
            '(\\/|\\\)custom(\\/|\\\)Extension(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)Ext(\\/|\\\)Language(\\/|\\\)(.*?)\.php$',
            //module builder application (removed with plans to make modulebuilder its own scan soon)
            //'(\\/|\\\)custom(\\/|\\\)modulebuilder(\\/|\\\)packages(\\/|\\\)(.*?)(\\/|\\\)language(\\/|\\\)application(\\/|\\\)(.*?)\.lang.php$',
            //module builder modules (removed with plans to make modulebuilder its own scan soon)
            //'(\\/|\\\)custom(\\/|\\\)modulebuilder(\\/|\\\)packages(\\/|\\\)(.*?)(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)language(\\/|\\\)(.*?)\.lang.php$',
            //custom modules
            '(\\/|\\\)custom(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)language(\\/|\\\)(.*?)\.lang.php$',
        );

        return '/('. implode(')|(', $langRegexes). ')/';
    }

    /**
     * Returns the custom language directories in Sugar.
     * @return string
     */
    protected function getVardefRegex()
    {
        $langRegexes = array(
            //module extensions
            '(\\/|\\\)custom(\\/|\\\)Extension(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)Ext(\\/|\\\)Vardefs(\\/|\\\)(.*?)\.php$',
            //module builder application
            //disabling for now as the variables are $vardefs and not $dictionary
            //'(\\/|\\\)custom(\\/|\\\)modulebuilder(\\/|\\\)packages(\\/|\\\)(.*?)(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)vardefs.php$',
            //custom modules
            '(\\/|\\\)custom(\\/|\\\)modules(\\/|\\\)(.*?)(\\/|\\\)vardefs.php$',
        );

        return '/('. implode(')|(', $langRegexes). ')/';
    }

    /**
     * Returns the list of variables in a file
     * @param $file
     * @return array
     */
    public function getVariablesInFile($file)
    {
        $vars = array();
        $results = array_filter(
            token_get_all(file_get_contents($file)),
            function ($t) {
                return $t[0] == T_VARIABLE;
            }
        );

        foreach ($results as $result) {
            if (isset($result[1])) {
