1
0
mirror of https://github.com/armink/EasyLogger.git synced 2025-02-01 05:52:52 +08:00

1、【增加】Linux平台下的Demo。

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink 2015-07-31 15:43:56 +08:00
parent 73e988d92c
commit f4af95b656
6 changed files with 289 additions and 0 deletions

24
demo/os/linux/README.md Normal file
View File

@ -0,0 +1,24 @@
# linux demo
---
## 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ʹ<EFBFBD><EFBFBD>GCC<EFBFBD><EFBFBD><EFBFBD>롣ͨ<EFBFBD><EFBFBD> `main.c` <20><> `test_elog()` <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
### 1.1<EFBFBD><EFBFBD>ʹ<EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD>
ʹ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>úñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>óɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD> `make.sh` <20>ű<EFBFBD><C5B1><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> `out\EasyLoggerLinuxDemo` <20><><EFBFBD>ɿ<EFBFBD><C9BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>
## 2<><32><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>У<EFBFBD>˵<EFBFBD><CBB5>
`easylogger\port\elog_port.c` <20><>ֲ<EFBFBD>ο<EFBFBD><CEBF>ļ<EFBFBD>
## 3<><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD> `main.c` <20><>58-65<36><35><EFBFBD>еIJ<D0B5><C4B2><EFBFBD>ע<EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¹<EFBFBD><C2B9>ܡ<EFBFBD>
- <20><>59<35>У<EFBFBD><D0A3><EFBFBD>̬ʹ<CCAC>ܻ<EFBFBD>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
- <20><>61<36>У<EFBFBD><D0A3><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>
- <20><>63<36>У<EFBFBD><D0A3><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˱<EFBFBD>ǩ
- <20><>65<36>У<EFBFBD><D0A3><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ù<EFBFBD><C3B9>˹ؼ<CBB9><D8BC><EFBFBD>

View File

@ -0,0 +1,47 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015, 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: It is the configure head file for this library.
* Created on: 2015-07-30
*/
#ifndef _ELOG_CFG_H_
#define _ELOG_CFG_H_
/* setting static output log level */
#define ELOG_OUTPUT_LVL ELOG_LVL_VERBOSE
/* enable log output. default open this macro */
#define ELOG_OUTPUT_ENABLE
/* output line number max length */
#define ELOG_LINE_NUM_MAX_LEN 5
/* log buffer size */
#define ELOG_BUF_SIZE 512
/* output filter's tag max length */
#define ELOG_FILTER_TAG_MAX_LEN 16
/* output filter's keyword max length */
#define ELOG_FILTER_KW_MAX_LEN 16
/* output newline sign */
#define ELOG_NEWLINE_SIGN "\n"
#endif /* _ELOG_CFG_H_ */

View File

@ -0,0 +1,121 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015, 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: Portable interface for linux.
* Created on: 2015-04-28
*/
#include <elog.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
static pthread_mutex_t output_lock;
/**
* EasyLogger port initialize
*
* @return result
*/
ElogErrCode elog_port_init(void) {
ElogErrCode result = ELOG_NO_ERR;
pthread_mutex_init(&output_lock, NULL);
return result;
}
/**
* output log port interface
*
* @param log output of log
* @param size log size
*/
void elog_port_output(const char *log, size_t size) {
/* output to terminal */
printf("%.*s", size, log);
}
/**
* output lock
*/
void elog_port_output_lock(void) {
pthread_mutex_lock(&output_lock);
}
/**
* output unlock
*/
void elog_port_output_unlock(void) {
pthread_mutex_unlock(&output_lock);
}
/**
* get current time interface
*
* @return current time
*/
const char *elog_port_get_time(void) {
static char cur_system_time[24] = { 0 };
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
if (p == NULL) {
return "";
}
snprintf(cur_system_time, 18, "%02d-%02d %02d:%02d:%02d", p->tm_mon + 1, p->tm_mday,
p->tm_hour, p->tm_min, p->tm_sec);
return cur_system_time;
}
/**
* get current process name interface
*
* @return current process name
*/
const char *elog_port_get_p_info(void) {
static char cur_process_info[10] = { 0 };
snprintf(cur_process_info, 10, "pid:%04d", getpid());
return cur_process_info;
}
/**
* get current thread name interface
*
* @return current thread name
*/
const char *elog_port_get_t_info(void) {
static char cur_thread_info[10] = { 0 };
snprintf(cur_thread_info, 10, "tid:%04ld", pthread_self());
return cur_thread_info;
}

88
demo/os/linux/main.c Normal file
View File

@ -0,0 +1,88 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015, 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: linux demo.
* Created on: 2015-07-30
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "elog.h"
#define log_a(...) elog_a("main.test.a", __VA_ARGS__)
#define log_e(...) elog_e("main.test.e", __VA_ARGS__)
#define log_w(...) elog_w("main.test.w", __VA_ARGS__)
#define log_i(...) elog_i("main.test.i", __VA_ARGS__)
#define log_d(...) elog_d("main.test.d", __VA_ARGS__)
#define log_v(...) elog_v("main.test.v", __VA_ARGS__)
static void test_elog(void);
int main(void) {
/* close printf buffer */
setbuf(stdout, NULL);
/* initialize EasyLogger */
elog_init();
/* set EasyLogger log format */
elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
/* start EasyLogger */
elog_start();
/* dynamic set enable or disable for output logs (true or false) */
// elog_set_output_enabled(false);
/* dynamic set output logs's level (from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE) */
// elog_set_filter_lvl(ELOG_LVL_WARN);
/* dynamic set output logs's filter for tag */
// elog_set_filter_tag("main.test.a");
/* dynamic set output logs's filter for keyword */
// elog_set_filter_kw("Hello");
/* test logger output */
test_elog();
return EXIT_SUCCESS;
}
/**
* EasyLogger demo
*/
void test_elog(void) {
while(true) {
/* test log output for all level */
log_a("Hello EasyLogger!");
log_e("Hello EasyLogger!");
log_w("Hello EasyLogger!");
log_i("Hello EasyLogger!");
log_d("Hello EasyLogger!");
log_v("Hello EasyLogger!");
// elog_raw("Hello EasyLogger!");
sleep(5);
}
}

6
demo/os/linux/make.sh Normal file
View File

@ -0,0 +1,6 @@
#!/bin/bash
gcc -I "easylogger/inc" -I "../../../easylogger/inc" -O0 -g3 -Wall -c "../../../easylogger/src/elog.c" -o "out/elog.o"
gcc -I "easylogger/inc" -I "../../../easylogger/inc" -O0 -g3 -Wall -c "easylogger/port/elog_port.c" -o "out/elog_port.o"
gcc -I "easylogger/inc" -I "../../../easylogger/inc" -O0 -g3 -Wall -c "../../../easylogger/src/elog_utils.c" -o "out/elog_utils.o"
gcc -I "easylogger/inc" -I "../../../easylogger/inc" -O0 -g3 -Wall -c "main.c" -o "out/main.o"
gcc -o out/EasyLoggerLinuxDemo "out/main.o" "out/elog_utils.o" "out/elog.o" "out/elog_port.o" -lpthread

3
demo/os/linux/out/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
*.exe
EasyLoggerLinuxDemo