parent
db2f3647a1
commit
cc99d24fe3
@ -0,0 +1,264 @@
|
||||
/*
|
||||
* This file is part of the EasyLogger Library.
|
||||
*
|
||||
* Copyright (c) 2016, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Logs asynchronous output.
|
||||
* Created on: 2016-11-06
|
||||
*/
|
||||
|
||||
#include <elog.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
|
||||
#if !defined(ELOG_ASYNC_OUTPUT_BUF_SIZE)
|
||||
#error "Please configure buffer size for asynchronous output mode (in elog_cfg.h)"
|
||||
#endif
|
||||
|
||||
#ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <semaphore.h>
|
||||
/* thread default stack size */
|
||||
#ifndef ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE
|
||||
#if PTHREAD_STACK_MIN > 4*1024
|
||||
#define ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE PTHREAD_STACK_MIN
|
||||
#else
|
||||
#define ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE (1*1024)
|
||||
#endif
|
||||
/* thread default priority */
|
||||
#ifndef ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY
|
||||
#define ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY (sched_get_priority_max(SCHED_RR) - 1)
|
||||
#endif
|
||||
/* output thread poll get log buffer size */
|
||||
#ifndef ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE
|
||||
#define ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE (ELOG_LINE_BUF_SIZE - 4)
|
||||
#endif
|
||||
#endif /* ELOG_ASYNC_OUTPUT_USING_PTHREAD */
|
||||
|
||||
/* asynchronous output log notice */
|
||||
static sem_t output_notice;
|
||||
/* asynchronous output pthread thread */
|
||||
static pthread_t async_output_thread;
|
||||
#endif
|
||||
|
||||
/* Initialize OK flag */
|
||||
static bool init_ok = false;
|
||||
/* asynchronous output mode's ring buffer */
|
||||
static char log_buf[ELOG_ASYNC_OUTPUT_BUF_SIZE] = { 0 };
|
||||
/* log ring buffer write index */
|
||||
static size_t write_index = 0;
|
||||
/* log ring buffer read index */
|
||||
static size_t read_index = 0;
|
||||
/* log ring buffer full flag */
|
||||
static bool buf_is_full = false;
|
||||
/* log ring buffer empty flag */
|
||||
static bool buf_is_empty = true;
|
||||
|
||||
extern void elog_port_output(const char *log, size_t size);
|
||||
extern void elog_output_lock(void);
|
||||
extern void elog_output_unlock(void);
|
||||
|
||||
/**
|
||||
* asynchronous output ring buffer used size
|
||||
*
|
||||
* @return used size
|
||||
*/
|
||||
static size_t elog_async_get_buf_used(void) {
|
||||
if (write_index > read_index) {
|
||||
return write_index - read_index;
|
||||
} else {
|
||||
if (!buf_is_full && !buf_is_empty) {
|
||||
return ELOG_ASYNC_OUTPUT_BUF_SIZE - (read_index - write_index);
|
||||
} else if (buf_is_full) {
|
||||
return ELOG_ASYNC_OUTPUT_BUF_SIZE;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* asynchronous output ring buffer remain space
|
||||
*
|
||||
* @return remain space
|
||||
*/
|
||||
static size_t async_get_buf_space(void) {
|
||||
return ELOG_ASYNC_OUTPUT_BUF_SIZE - elog_async_get_buf_used();
|
||||
}
|
||||
|
||||
/**
|
||||
* put log to asynchronous output ring buffer
|
||||
*
|
||||
* @param log put log buffer
|
||||
* @param size log size
|
||||
*
|
||||
* @return put log size, the log which beyond ring buffer space will be dropped
|
||||
*/
|
||||
static size_t async_put_log(const char *log, size_t size) {
|
||||
size_t space = 0;
|
||||
|
||||
space = async_get_buf_space();
|
||||
/* no space */
|
||||
if (!space) {
|
||||
size = 0;
|
||||
goto __exit;
|
||||
}
|
||||
/* drop some log */
|
||||
if (space <= size) {
|
||||
size = space;
|
||||
buf_is_full = true;
|
||||
}
|
||||
|
||||
if (write_index + size < ELOG_ASYNC_OUTPUT_BUF_SIZE) {
|
||||
memcpy(log_buf + write_index, log, size);
|
||||
write_index += size;
|
||||
} else {
|
||||
memcpy(log_buf + write_index, log, ELOG_ASYNC_OUTPUT_BUF_SIZE - write_index);
|
||||
memcpy(log_buf, log + ELOG_ASYNC_OUTPUT_BUF_SIZE - write_index,
|
||||
size - (ELOG_ASYNC_OUTPUT_BUF_SIZE - write_index));
|
||||
write_index += size - ELOG_ASYNC_OUTPUT_BUF_SIZE;
|
||||
}
|
||||
|
||||
buf_is_empty = false;
|
||||
|
||||
__exit:
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* get log from asynchronous output ring buffer
|
||||
*
|
||||
* @param log get log buffer
|
||||
* @param size log size
|
||||
*
|
||||
* @return get log size, the log size is less than ring buffer used size
|
||||
*/
|
||||
size_t elog_async_get_log(char *log, size_t size) {
|
||||
size_t used = 0;
|
||||
/* lock output */
|
||||
elog_output_lock();
|
||||
used = elog_async_get_buf_used();
|
||||
/* no log */
|
||||
if (!used) {
|
||||
size = 0;
|
||||
goto __exit;
|
||||
}
|
||||
/* less log */
|
||||
if (used <= size) {
|
||||
size = used;
|
||||
buf_is_empty = true;
|
||||
}
|
||||
|
||||
if (read_index + size < ELOG_ASYNC_OUTPUT_BUF_SIZE) {
|
||||
memcpy(log, log_buf + read_index, size);
|
||||
read_index += size;
|
||||
} else {
|
||||
memcpy(log, log_buf + read_index, ELOG_ASYNC_OUTPUT_BUF_SIZE - read_index);
|
||||
memcpy(log + ELOG_ASYNC_OUTPUT_BUF_SIZE - read_index, log_buf,
|
||||
size - (ELOG_ASYNC_OUTPUT_BUF_SIZE - read_index));
|
||||
read_index += size - ELOG_ASYNC_OUTPUT_BUF_SIZE;
|
||||
}
|
||||
|
||||
buf_is_full = false;
|
||||
|
||||
__exit:
|
||||
/* lock output */
|
||||
elog_output_unlock();
|
||||
return size;
|
||||
}
|
||||
|
||||
void elog_async_output(const char *log, size_t size) {
|
||||
/* this function must be implement by user when ELOG_ASYNC_OUTPUT_USING_PTHREAD is not defined */
|
||||
extern void elog_async_output_notice(void);
|
||||
size_t put_size;
|
||||
|
||||
put_size = async_put_log(log, size);
|
||||
/* notify output log thread */
|
||||
if (put_size > 0) {
|
||||
elog_async_output_notice();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
|
||||
void elog_async_output_notice(void) {
|
||||
sem_post(&output_notice);
|
||||
}
|
||||
|
||||
static void *async_output(void *arg) {
|
||||
size_t get_log_size = 0;
|
||||
static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE];
|
||||
|
||||
ELOG_ASSERT(init_ok);
|
||||
|
||||
while(true) {
|
||||
/* waiting log */
|
||||
sem_wait(&output_notice);
|
||||
/* polling gets and outputs the log */
|
||||
while(true) {
|
||||
get_log_size = elog_async_get_log(poll_get_buf, sizeof(poll_get_buf));
|
||||
if (get_log_size) {
|
||||
elog_port_output(poll_get_buf, get_log_size);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* asynchronous output mode initialize
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
ElogErrCode elog_async_init(void) {
|
||||
ElogErrCode result = ELOG_NO_ERR;
|
||||
|
||||
if (init_ok) {
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
|
||||
pthread_attr_t thread_attr;
|
||||
struct sched_param thread_sched_param;
|
||||
|
||||
sem_init(&output_notice, 0, 0);
|
||||
|
||||
pthread_attr_init(&thread_attr);
|
||||
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_attr_setstacksize(&thread_attr, ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE);
|
||||
pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
|
||||
thread_sched_param.sched_priority = ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY;
|
||||
pthread_attr_setschedparam(&thread_attr, &thread_sched_param);
|
||||
pthread_create(&async_output_thread, &thread_attr, async_output, NULL);
|
||||
pthread_attr_destroy(&thread_attr);
|
||||
#endif
|
||||
|
||||
init_ok = true;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* ELOG_ASYNC_OUTPUT_ENABLE */
|
||||
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* This file is part of the EasyLogger Library.
|
||||
*
|
||||
* Copyright (c) 2016, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Function: Logs buffered output.
|
||||
* Created on: 2016-11-09
|
||||
*/
|
||||
|
||||
#include <elog.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef ELOG_BUF_OUTPUT_ENABLE
|
||||
#if !defined(ELOG_BUF_OUTPUT_BUF_SIZE)
|
||||
#error "Please configure buffer size for buffered output mode (in elog_cfg.h)"
|
||||
#endif
|
||||
|
||||
/* buffered output mode's buffer */
|
||||
static char log_buf[ELOG_BUF_OUTPUT_BUF_SIZE] = { 0 };
|
||||
/* log buffer current write size */
|
||||
static size_t buf_write_size = 0;
|
||||
|
||||
extern void elog_port_output(const char *log, size_t size);
|
||||
extern void elog_output_lock(void);
|
||||
extern void elog_output_unlock(void);
|
||||
|
||||
/**
|
||||
* output buffered logs when buffer is full
|
||||
*
|
||||
* @param log will be buffered line's log
|
||||
* @param size log size
|
||||
*/
|
||||
void elog_buf_output(const char *log, size_t size) {
|
||||
size_t write_size = 0, write_index = 0;
|
||||
|
||||
while (true) {
|
||||
if (buf_write_size + size > ELOG_BUF_OUTPUT_BUF_SIZE) {
|
||||
write_size = ELOG_BUF_OUTPUT_BUF_SIZE - buf_write_size;
|
||||
memcpy(log_buf + buf_write_size, log + write_index, write_size);
|
||||
write_index += write_size;
|
||||
size -= write_size;
|
||||
buf_write_size += write_size;
|
||||
/* output log */
|
||||
elog_port_output(log_buf, buf_write_size);
|
||||
/* reset write index */
|
||||
buf_write_size = 0;
|
||||
} else {
|
||||
memcpy(log_buf + buf_write_size, log + write_index, size);
|
||||
buf_write_size += size;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* flush all buffered logs to output device
|
||||
*/
|
||||
void elog_flush(void) {
|
||||
/* lock output */
|
||||
elog_output_lock();
|
||||
/* output log */
|
||||
elog_port_output(log_buf, buf_write_size);
|
||||
/* reset write index */
|
||||
buf_write_size = 0;
|
||||
/* unlock output */
|
||||
elog_output_unlock();
|
||||
}
|
||||
#endif /* ELOG_BUF_OUTPUT_ENABLE */
|
||||
Loading…
Reference in New Issue