diff --git a/demo/os/windows/README.md b/demo/os/windows/README.md new file mode 100644 index 0000000..179096e --- /dev/null +++ b/demo/os/windows/README.md @@ -0,0 +1,24 @@ +# windows Demo + +--- + +## 1、简介 + +使用GCC编译。通过 `main.c` 的 `test_elog()` 方法来测试日志的输出。 + +### 1.1、使用方法 + +使用前需提前配置好编译环境,配置成功后,点击 `make.bat` 脚本,等待编译完成后,打开 `out\EasyLoggerWinDemo.exe` 即可看到运行结果。 + +## 2、文件(夹)说明 + +`\easylogger\port\elog_port.c` 移植参考文件 + +## 3、其他功能 + +可以打开 `main.c` 的59-66行中的部分注释,来测试以下功能。 + +- 第60行:动态使能或失能日志输出 +- 第62行:动态设置过滤优先级 +- 第64行:动态设置过滤标签 +- 第66行:动态设置过滤关键词 diff --git a/demo/os/windows/easylogger/inc/elog_cfg.h b/demo/os/windows/easylogger/inc/elog_cfg.h new file mode 100644 index 0000000..f7c2579 --- /dev/null +++ b/demo/os/windows/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/windows/easylogger/port/elog_port.c b/demo/os/windows/easylogger/port/elog_port.c new file mode 100644 index 0000000..26969b5 --- /dev/null +++ b/demo/os/windows/easylogger/port/elog_port.c @@ -0,0 +1,115 @@ +/* + * 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 each platform. + * Created on: 2015-04-28 + */ + +#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 }; + static SYSTEMTIME currTime; + + GetLocalTime(&currTime); + snprintf(cur_system_time, 24, "%02d-%02d %02d:%02d:%02d.%03d", currTime.wMonth, currTime.wDay, + currTime.wHour, currTime.wMinute, currTime.wSecond, currTime.wMilliseconds); + + 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:%04ld", GetCurrentProcessId()); + + 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", GetCurrentThreadId()); + + return cur_thread_info; +} diff --git a/demo/os/windows/main.c b/demo/os/windows/main.c new file mode 100644 index 0000000..bf6e5b3 --- /dev/null +++ b/demo/os/windows/main.c @@ -0,0 +1,89 @@ +/* + * 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: windows 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); + elog_set_filter_lvl(ELOG_LVL_VERBOSE); + /* 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(5000); + } +} diff --git a/demo/os/windows/make.bat b/demo/os/windows/make.bat new file mode 100644 index 0000000..4b0cabd --- /dev/null +++ b/demo/os/windows/make.bat @@ -0,0 +1,5 @@ +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\EasyLoggerWinDemo.exe "out\main.o" "out\elog_utils.o" "out\elog.o" "out\elog_port.o" -lpthread diff --git a/demo/os/windows/out/.gitignore b/demo/os/windows/out/.gitignore new file mode 100644 index 0000000..25a7384 --- /dev/null +++ b/demo/os/windows/out/.gitignore @@ -0,0 +1,2 @@ +*.o +*.exe