181 lines
3.9 KiB
C
181 lines
3.9 KiB
C
/*
|
|
*
|
|
* Copyright (c) 2012-2021 Shenshu Technologies Co., Ltd.
|
|
*
|
|
* This software is licensed under the terms of the GNU General Public
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
* may be copied, distributed, and modified under those terms.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
*/
|
|
|
|
|
|
#include <stdarg.h>
|
|
#include "securec.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <assert.h>
|
|
|
|
#include "config.h"
|
|
#include "type.h"
|
|
#include "message.h"
|
|
#include "dbg.h"
|
|
|
|
#if defined(MULTI_TASK_LOGQUEUE)
|
|
#ifdef OS_LINUX
|
|
#include <pthread.h>
|
|
#endif
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#endif /* __cplusplus */
|
|
|
|
log_queue g_log_queue;
|
|
|
|
static const char *gppsz_type[] = {
|
|
" ", "[fatal]:", "[error]:", "[warn]:",
|
|
"[info]:", "[debug]:", "[debug1]:", "[debug2]:"
|
|
};
|
|
|
|
static void log_print_msg(log_queue *p_log_queue, unsigned int uc_log_level,
|
|
const char *psz_format, va_list _args);
|
|
|
|
#if defined(MULTI_TASK_LOGQUEUE)
|
|
static void log_init_lock(log_queue *p_log_queue);
|
|
static int log_lock(log_queue *p_log_queue);
|
|
static int log_unlock(log_queue *p_log_queue);
|
|
static void log_destroy_lock(log_queue *p_log_queue);
|
|
|
|
/*-------- Lock Function , Maybe Replace later ---*/
|
|
|
|
|
|
static int log_lock(log_queue *p_log_queue)
|
|
{
|
|
#if defined(OS_LINUX)
|
|
return pthread_mutex_lock(&(p_log_queue->mutex_lock));
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
static int log_unlock(log_queue *p_log_queue)
|
|
{
|
|
#if defined(OS_LINUX)
|
|
return pthread_mutex_unlock(&(p_log_queue->mutex_lock));
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
static void log_init_lock(log_queue *p_log_queue)
|
|
{
|
|
#if defined(OS_LINUX)
|
|
pthread_mutex_init(&p_log_queue->mutex_lock, NULL);
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
static void log_destroy_lock(log_queue *p_log_queue)
|
|
{
|
|
log_unlock(p_log_queue);
|
|
#if defined(OS_LINUX)
|
|
pthread_mutex_destroy(&p_log_queue->mutex_lock);
|
|
#else
|
|
#endif
|
|
}
|
|
|
|
#else
|
|
#define log_lock(p)
|
|
#define log_unlock(p)
|
|
#define log_init_lock(p)
|
|
#define log_destroy_lock(p)
|
|
#endif
|
|
|
|
/* --------- Message Functions -----*/
|
|
int bsp_log_create(log_queue *p_log_queue, unsigned char min_level, unsigned long len_msg)
|
|
{
|
|
dbg_assert(p_log_queue);
|
|
log_init_lock(p_log_queue);
|
|
p_log_queue->uc_min_level = min_level;
|
|
p_log_queue->len_msg = len_msg;
|
|
if (len_msg <= 0)
|
|
return FAILURE;
|
|
p_log_queue->p_msg = malloc(len_msg);
|
|
if (p_log_queue->p_msg == NULL)
|
|
return FAILURE;
|
|
return SUCCESS;
|
|
}
|
|
|
|
void bsp_log_destroy(log_queue *p_log_queue)
|
|
{
|
|
dbg_assert(p_log_queue);
|
|
free(p_log_queue->p_msg);
|
|
log_destroy_lock(p_log_queue);
|
|
p_log_queue->p_msg = NULL;
|
|
}
|
|
|
|
void log_msg(log_queue *p_log_queue, unsigned int uc_log_level,
|
|
char *psz_format, ...)
|
|
{
|
|
dbg_assert(p_log_queue);
|
|
|
|
if (p_log_queue->uc_min_level < uc_log_level)
|
|
return;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, psz_format);
|
|
log_print_msg(p_log_queue, uc_log_level, psz_format, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void log_msg_va(log_queue *p_log_queue, unsigned int uc_log_level,
|
|
const char *psz_format, va_list args)
|
|
{
|
|
log_print_msg(p_log_queue, uc_log_level, psz_format, args);
|
|
}
|
|
|
|
/* following functions are local */
|
|
|
|
static void log_print_msg(log_queue *p_log_queue, unsigned int uc_log_level,
|
|
const char *psz_format, va_list _args)
|
|
{
|
|
va_list args;
|
|
log_lock(p_log_queue);
|
|
va_copy(args, _args);
|
|
if (vsnprintf_s(p_log_queue->p_msg, p_log_queue->len_msg, p_log_queue->len_msg, psz_format, args) < 0)
|
|
printf("log_print_msg error.\n");
|
|
|
|
va_end(args);
|
|
p_log_queue->p_msg[ p_log_queue->len_msg - 1 ] = 0;
|
|
|
|
if (uc_log_level)
|
|
fprintf(stdout, "%s %s", gppsz_type[uc_log_level], p_log_queue->p_msg);
|
|
else
|
|
fprintf(stdout, "%s", p_log_queue->p_msg);
|
|
|
|
log_unlock(p_log_queue);
|
|
|
|
fflush(stdout);
|
|
return;
|
|
}
|
|
|
|
|
|
#ifdef __cplusplus
|
|
#if __cplusplus
|
|
}
|
|
#endif
|
|
#endif /* __cplusplus */
|