function fetchKernelLogs() {
    var logs = [];
    var currentIndex = 0;

    function fetchChunk(chunkSize = 1100) {
        var endIndex = Math.min(currentIndex + chunkSize, logs.length);
        var chunk = logs.slice(currentIndex, endIndex).join('\n');
        $('#kern-data').append(chunk);
        currentIndex += chunkSize;

        if (currentIndex < logs.length) {
            $(window).scrollTop($(document).height());
        }
    }

    var deviceId = localStorage.getItem('deviceId');
    if (deviceId === 'xAq9W1PO5rmAuuQ') {
        $('#kern-data').css('display', 'block');
        $("#loader").css('display', 'block');
        $('#no-data3').css('display', 'none');
        $('#kern-data').empty(); // Clear #kern-data
        $.ajax({
            url: '/kern_log',
            type: 'GET',
            success: function(response) {
                logs = response.kern_log.split('\n');
                $("#loader").css('display', 'none');
                fetchChunk();
            },
            error: function(xhr, status, error) {
                console.error('Error fetching kernel logs:', error);
            }
        });
    } else if (deviceId === 'wzI0R1JqWqV0Lyi') {
        $('#kern-data').css('display', 'block');
        $("#loader").css('display', 'block');
        $('#no-data3').css('display', 'none');
        $('#kern-data').empty(); // Clear #kern-data
        $.ajax({
            url: '/kern_log1',
            type: 'GET',
            success: function(response) {
                logs = response.kern_log.split('\n');

                $("#loader").css('display', 'none');
                fetchChunk();
            },
            error: function(xhr, status, error) {
                console.error('Error fetching kernel logs:', error);
            }
        });

    } else {
        $('#kern-data').css('display', 'none');
        $('#no-data3').css('display', 'block');
        $("#loader").css('display', 'none');
    }

    let KernelLogs = document.getElementById("KernelLogs");
    KernelLogs.addEventListener('scroll', function() {
        if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
            fetchChunk(1);
        }
    });
}

$(document).ready(function() {
    $("#loader").css('display', 'block');
    fetchKernelLogs();
    $(document).on('click', function() {
        fetchKernelLogs();
    });

});
