From fa14a2a10828e71632845717fab4cd0445d1ca3a Mon Sep 17 00:00:00 2001 From: Allenhe123 <931317255@qq.com> Date: Thu, 19 Aug 2021 17:58:39 +0800 Subject: [PATCH] add examples --- example/demo.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 example/demo.c diff --git a/example/demo.c b/example/demo.c new file mode 100644 index 0000000..a921f21 --- /dev/null +++ b/example/demo.c @@ -0,0 +1,61 @@ +#include "../src/log.h" +#include +#include + +bool s_stop = false; +pthread_mutex_t MUTEX_LOG; +void log_lock(bool lock, void *udata); + +void log_lock(bool lock, void* udata) { + pthread_mutex_t *LOCK = (pthread_mutex_t*)(udata); + if (lock) + pthread_mutex_lock(LOCK); + else + pthread_mutex_unlock(LOCK); +} + +void run(void *arg) { + int id = *(int*)arg; + log_debug("+++++id: %d", id); + unsigned int idx = 0; + for (;;) { + log_debug("thread id: %d idx:%u", id, idx++); + usleep(1000 * 100); + if (s_stop) break; + } +} + +void stop(int signo) { + (void)signo; + s_stop = true; +} + +int main() { + pthread_mutex_init(&MUTEX_LOG, NULL); + log_set_lock(log_lock, &MUTEX_LOG); + + FILE* fp = fopen ("demo.log", "a+"); + log_set_quiet(true); + log_set_level(LOG_DEBUG); + log_set_fullpath(false); + log_add_fp(fp, LOG_DEBUG); + + signal(SIGINT, stop); + signal(SIGKILL, stop); + signal(SIGTERM, stop); + + pthread_t h[8]; + int id_array[8] = {0,1,2,3,4,5,6,7}; + for (int i= 0; i<8; i++) { + pthread_create(&h[i], NULL, run, &id_array[i]); + } + + void* ret[8] = {NULL}; + for (int i=0; i<8; i++) { + pthread_join(h[i], &ret); + } + + pthread_mutex_destroy(&MUTEX_LOG); + + fclose(fp); +}