/**
 * @file tees_log.c
 * @brief Multibuild's logging API implementation for <t-base
 * @author Iaroslav Makarchuk (i.makarchuk@samsung.com)
 * @date Created Oct 3, 2016
 * @par In Samsung Ukraine R&D Center (SURC) under a contract between
 * @par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * @par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
 * @par Copyright: (c) Samsung Electronics Co, Ltd 2015. All rights reserved.
 *
 * This software is proprietary of Samsung Electronics.
 * No part of this software, either material or conceptual may be copied
 * or distributed, transmitted, transcribed, stored in a retrieval system
 * or translated into any human or computer language in any form by any means,
 * electronic, mechanical, manual or otherwise, or disclosed to third parties
 * without the express written permission of Samsung Electronics.
 */

#include <tees_log.h>

#include <tlStd.h>
#include <TlApi/TlApi.h>

#include <log_utils.h>

TEES_LogLevel g_tees_log_level = TEES_LOG_LEVEL_DEFAULT;

void TEES_LogSetTarget(TEES_LogTarget target,
                          const void *param,
                          uint32_t param_len) {
  (void) param;
  (void) param_len;
  (void) target;
}

void TEES_LogSetLevel(TEES_LogLevel level) {
  if (level < TEES_LOG_LEVEL_MAX) {
    g_tees_log_level = level;
  }
}

void TEES_Log(TEES_LogLevel level, const char *function_name, int line,
                 const char *project_name, const char *fmt, ...)
{
  if (level < g_tees_log_level ||
      level > TEES_LOG_LEVEL_MAX) {
    return;
  }

  tlApiLogPrintf("[%s] %s [%s:%d] ",
                 project_name ? project_name : NAME_UNKNOWN,
                 LogUtilsGetTag(level),
                 function_name ? function_name : NAME_UNKNOWN, line);
  va_list vl;
  va_start(vl, fmt);
  tlApiLogvPrintf(fmt, vl);
  va_end(vl);
}

void TEES_LogMem(TEES_LogLevel level, const char *function_name, int line,
                    const char *project_name, const void *data,
                    uint32_t data_len)
{
  uint32_t i = 0;

  if (level < g_tees_log_level ||
      level > TEES_LOG_LEVEL_MAX) {
    return;
  }

  tlApiLogPrintf("[%s] %s [%s:%d] [MEM]: %p (%u)\n",
                 project_name ? project_name : NAME_UNKNOWN,
                 LogUtilsGetTag(level),
                 function_name ? function_name : NAME_UNKNOWN,
                 line, data, data_len);

  for (i = 0; i < data_len; i++) {
    if (i % DUMP_ROW_LEN == 0) {
      tlApiLogPrintf("\n[%s] ", project_name ? project_name : NAME_UNKNOWN);
    }
    tlApiLogPrintf("%02X%s",
                   ((uint8_t *)data)[i], (i + 1) % DUMP_ROW_LEN ? "" : "\n");
  }

  if ((data_len + 1) % DUMP_ROW_LEN != 0) {
    tlApiLogPrintf("%s", "\n");
  }
}

