public static function store_log_for_task(task_base $task, string $logpath, bool $failed,
            int $dbreads, int $dbwrites, float $timestart, float $timeend) {
        global $DB;

        // Write this log to the database.
        $logdata = (object) [
            'type' => is_a($task, scheduled_task::class) ? self::TYPE_SCHEDULED : self::TYPE_ADHOC,
            'component' => $task->get_component(),
            'classname' => get_class($task),
            'userid' => 0,
            'timestart' => $timestart,
            'timeend' => $timeend,
            'dbreads' => $dbreads,
            'dbwrites' => $dbwrites,
            'result' => (int) $failed,
            'output' => file_get_contents($logpath),
        ];

        if (is_a($task, adhoc_task::class) && $userid = $task->get_userid()) {
            $logdata->userid = $userid;
        }

        $logdata->id = $DB->insert_record('task_log', $logdata);
    }