re-org platform APIs, simplify porting process (#201)
Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
@ -2,213 +2,53 @@
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
/**
|
||||
* @file bh_log.c
|
||||
* @date Tue Nov 8 18:20:06 2011
|
||||
*
|
||||
* @brief Implementation of Beihai's log system.
|
||||
*/
|
||||
|
||||
#include "bh_assert.h"
|
||||
#include "bh_time.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_platform_log.h"
|
||||
|
||||
/**
|
||||
* The verbose level of the log system. Only those verbose logs whose
|
||||
* levels are less than or equal to this value are outputed.
|
||||
*/
|
||||
static int log_verbose_level;
|
||||
static uint32 log_verbose_level = LOG_LEVEL_WARNING;
|
||||
|
||||
/**
|
||||
* The lock for protecting the global output stream of logs.
|
||||
*/
|
||||
static korp_mutex log_stream_lock;
|
||||
|
||||
/**
|
||||
* The current logging thread that owns the log_stream_lock.
|
||||
*/
|
||||
static korp_tid cur_logging_thread = INVALID_THREAD_ID;
|
||||
|
||||
/**
|
||||
* Whether the currently being printed log is ennabled.
|
||||
*/
|
||||
static bool cur_log_enabled;
|
||||
|
||||
int _bh_log_init()
|
||||
{
|
||||
log_verbose_level = 3;
|
||||
return vm_mutex_init(&log_stream_lock);
|
||||
}
|
||||
|
||||
void _bh_log_set_verbose_level(int level)
|
||||
void
|
||||
bh_log_set_verbose_level(uint32 level)
|
||||
{
|
||||
log_verbose_level = level;
|
||||
}
|
||||
|
||||
void _bh_log_printf(const char *fmt, ...)
|
||||
void
|
||||
bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
_bh_log_vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
korp_tid self;
|
||||
char buf[32] = { 0 };
|
||||
uint64 usec;
|
||||
uint32 t, h, m, s, mills;
|
||||
|
||||
#ifndef BH_PLATFORM_ANDROID
|
||||
/**
|
||||
* Return true if the given tag is enabled by the configuration.
|
||||
*
|
||||
* @param tag the tag (or prefix) of the log message.
|
||||
*
|
||||
* @return true if the log should be enabled.
|
||||
*/
|
||||
static bool is_log_enabled(const char *tag)
|
||||
{
|
||||
/* Print all non-verbose or verbose logs whose levels are less than
|
||||
or equal to the configured verbose level. */
|
||||
return tag[0] != 'V' || tag[1] - '0' <= log_verbose_level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for converting "..." to va_list.
|
||||
*/
|
||||
static void bh_log_emit_helper(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
bh_log_emit(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
#endif
|
||||
|
||||
extern size_t _bh_time_strftime(char *s, size_t max, const char *format,
|
||||
int64 time);
|
||||
|
||||
void _bh_log_vprintf(const char *fmt, va_list ap)
|
||||
{
|
||||
#ifndef BH_PLATFORM_ANDROID
|
||||
korp_tid self = vm_self_thread();
|
||||
/* Try to own the log stream and start the log output. */
|
||||
if (self != cur_logging_thread) {
|
||||
vm_mutex_lock(&log_stream_lock);
|
||||
cur_logging_thread = self;
|
||||
|
||||
if (fmt && (cur_log_enabled = is_log_enabled(fmt))) {
|
||||
char buf[32];
|
||||
bh_time_strftime(buf, 32, "%Y-%m-%d %H:%M:%S",
|
||||
(int64)bh_time_get_millisecond_from_1970());
|
||||
bh_log_emit_helper("\n[%s - %X]: ", buf, (int) self);
|
||||
|
||||
/* Strip the "Vn." prefix. */
|
||||
if (fmt && strlen(fmt) >= 3 && fmt[0] == 'V' && fmt[1] && fmt[2])
|
||||
fmt += 3;
|
||||
}
|
||||
}
|
||||
#else
|
||||
// since we are using android log, do not worry about that
|
||||
cur_log_enabled = true;
|
||||
#endif//BH_PLATFORM_ANDROID
|
||||
if (cur_log_enabled && fmt)
|
||||
bh_log_emit(fmt, ap);
|
||||
}
|
||||
|
||||
void _bh_log_commit()
|
||||
{
|
||||
if (vm_self_thread() != cur_logging_thread)
|
||||
/* Ignore the single commit without printing anything. */
|
||||
if (log_level > log_verbose_level)
|
||||
return;
|
||||
|
||||
cur_logging_thread = INVALID_THREAD_ID;
|
||||
vm_mutex_unlock(&log_stream_lock);
|
||||
}
|
||||
self = os_self_thread();
|
||||
|
||||
void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
usec = os_time_get_boot_microsecond();
|
||||
t = (uint32)(usec / 1000000) % (24 * 60 * 60);
|
||||
h = t / (60 * 60);
|
||||
t = t % (60 * 60);
|
||||
m = t / 60;
|
||||
s = t % 60;
|
||||
mills = (uint32)(usec % 1000);
|
||||
|
||||
#ifndef BH_PLATFORM_ANDROID
|
||||
if (tag)
|
||||
_bh_log_printf(tag);
|
||||
snprintf(buf, sizeof(buf), "%02u:%02u:%02u:%03u", h, m, s, mills);
|
||||
|
||||
os_printf("[%s - %X]: ", buf, (uint32)self);
|
||||
|
||||
if (file)
|
||||
_bh_log_printf("%s:%d", file, line);
|
||||
#else
|
||||
(void)tag;
|
||||
(void)file;
|
||||
(void)line;
|
||||
#endif//BH_PLATFORM_ANDROID
|
||||
os_printf("%s, line %d, ", file, line);
|
||||
|
||||
va_start(ap, fmt);
|
||||
_bh_log_vprintf(fmt, ap);
|
||||
os_vprintf(fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
_bh_log_commit();
|
||||
os_printf("\n");
|
||||
}
|
||||
|
||||
#if defined(BH_DEBUG)
|
||||
|
||||
BH_STATIC char com_switchs[LOG_COM_MAX]; /* 0: off; 1: on */
|
||||
BH_STATIC char *com_names[LOG_COM_MAX] = {"app_manager", "gc", "hmc", "utils",
|
||||
"verifier_jeff", "vmcore_jeff"};
|
||||
|
||||
BH_STATIC int com_find_name(const char **com)
|
||||
{
|
||||
int i;
|
||||
const char *c, *name;
|
||||
|
||||
for (i = 0; i < LOG_COM_MAX; i++) {
|
||||
c = *com;
|
||||
name = com_names[i];
|
||||
while (*name != '\0') {
|
||||
if (*c != *name)
|
||||
break;
|
||||
c++;
|
||||
name++;
|
||||
}
|
||||
if (*name == '\0') {
|
||||
*com = c;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return LOG_COM_MAX;
|
||||
}
|
||||
|
||||
void log_parse_coms(const char *coms)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = LOG_COM_MAX; i--; )
|
||||
com_switchs[i] = 0;
|
||||
|
||||
com_switchs[LOG_COM_UTILS] = 1; /* utils is a common part */
|
||||
|
||||
if (coms == NULL)
|
||||
return;
|
||||
|
||||
while (*coms != '\0') {
|
||||
i = com_find_name(&coms);
|
||||
if (i == LOG_COM_MAX)
|
||||
break;
|
||||
com_switchs[i] = 1;
|
||||
|
||||
if (*coms == '\0')
|
||||
return;
|
||||
if (*coms != ';')
|
||||
break;
|
||||
/* *com == ';' */
|
||||
coms++;
|
||||
}
|
||||
|
||||
/* Log the message without aborting. */
|
||||
LOG_DEBUG(LOG_COM_UTILS, "The component names for logging are not right: %s.", coms);
|
||||
}
|
||||
|
||||
int bh_log_dcom_is_enabled(int component)
|
||||
{
|
||||
return com_switchs[component];
|
||||
}
|
||||
|
||||
#endif /* defined(BH_DEBUG) */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user