diff --git a/demo/os/linux/README.md b/demo/os/linux/README.md new file mode 100644 index 0000000..7076611 --- /dev/null +++ b/demo/os/linux/README.md @@ -0,0 +1,24 @@ +# linux demo + +--- + +## 1、简介 + +使用GCC编译。通过 `main.c` 的 `test_elog()` 方法来测试日志的输出。 + +### 1.1、使用方法 + +使用前需提前配置好编译环境,配置成功后,执行 `make.sh` 脚本,等待编译完成后,运行 `out\EasyLoggerLinuxDemo` 即可看到运行结果。 + +## 2、文件(夹)说明 + +`easylogger\port\elog_port.c` 移植参考文件 + +## 3、其他功能 + +可以打开 `main.c` 的58-65行中的部分注释,来测试以下功能。 + +- 第59行:动态使能或失能日志输出 +- 第61行:动态设置过滤优先级 +- 第63行:动态设置过滤标签 +- 第65行:动态设置过滤关键词 diff --git a/demo/os/linux/easylogger/inc/elog_cfg.h b/demo/os/linux/easylogger/inc/elog_cfg.h new file mode 100644 index 0000000..f7c2579 --- /dev/null +++ b/demo/os/linux/easylogger/inc/elog_cfg.h @@ -0,0 +1,47 @@ +/* + * This file is part of the EasyLogger Library. + * + * Copyright (c) 2015, Armink, + * + * 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_ */ diff --git a/demo/os/linux/easylogger/port/elog_port.c b/demo/os/linux/easylogger/port/elog_port.c new file mode 100644 index 0000000..4c8c183 --- /dev/null +++ b/demo/os/linux/easylogger/port/elog_port.c @@ -0,0 +1,121 @@ +/* + * This file is part of the EasyLogger Library. + * + * Copyright (c) 2015, Armink, + * + * 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 +#include +#include +#include +#include + +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; +} diff --git a/demo/os/linux/main.c b/demo/os/linux/main.c new file mode 100644 index 0000000..526f7fc --- /dev/null +++ b/demo/os/linux/main.c @@ -0,0 +1,88 @@ +/* + * This file is part of the EasyLogger Library. + * + * Copyright (c) 2015, Armink, + * + * 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 +#include +#include +#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); + } +} diff --git a/demo/os/linux/make.sh b/demo/os/linux/make.sh new file mode 100644 index 0000000..e758bae --- /dev/null +++ b/demo/os/linux/make.sh @@ -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 diff --git a/demo/os/linux/out/.gitignore b/demo/os/linux/out/.gitignore new file mode 100644 index 0000000..15b0305 --- /dev/null +++ b/demo/os/linux/out/.gitignore @@ -0,0 +1,3 @@ +*.o +*.exe +EasyLoggerLinuxDemo