#!/usr/bin/env python3
#
# Copyright (c) 2021 Intel Corporation
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0

"""
Parser library for Dictionary-based Logging

This library along with dictionary_parser converts the
input binary data to the log using log database.
"""

import logging
import sys

import dictionary_parser
from dictionary_parser.log_database import LogDatabase


def parser(logdata, dbfile, logger):
    """function of serial parser"""
    # Read from database file
    database = LogDatabase.read_json_database(dbfile)

    if not isinstance(logger, logging.Logger):
        raise ValueError("Invalid logger instance. Please configure the logger!")

    if database is None:
        logger.error("ERROR: Cannot open database file:  exiting...")
        sys.exit(1)

    if logdata is None:
        logger.error("ERROR: cannot read log from file:  exiting...")
        sys.exit(1)

    log_parser = dictionary_parser.get_parser(database)
    if log_parser is not None:
        logger.debug("# Build ID: %s", database.get_build_id())
        logger.debug("# Target: %s, %d-bit", database.get_arch(), database.get_tgt_bits())
        if database.is_tgt_little_endian():
            logger.debug("# Endianness: Little")
        else:
            logger.debug("# Endianness: Big")

        ret = log_parser.parse_log_data(logdata)
        if not ret:
            logger.error("ERROR: there were error(s) parsing log data")
            sys.exit(1)
    else:
        logger.error("ERROR: Cannot find a suitable parser matching database version!")
