c-template
logger_test.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <stdarg.h>
5 #include <stddef.h>
6 #include <setjmp.h>
7 #include <cmocka.h>
8 #include <assert.h>
9 #include <pthread.h>
10 #include "../../include/utils/logger.h"
11 
12 void *test_thread_log(void *data) {
13  thread_logger *thl = (thread_logger *)data;
14  thl->log(thl, 0, "this is an info log", LOG_LEVELS_INFO);
15  thl->logf(thl, 0, LOG_LEVELS_INFO, "%s\t%s", "one", "two");
16 
17  thl->log(thl, 0, "this is a warn log", LOG_LEVELS_WARN);
18  thl->logf(thl, 0, LOG_LEVELS_WARN, "%s\t%s", "one", "two");
19 
20  thl->log(thl, 0, "this is an error log", LOG_LEVELS_ERROR);
21  thl->logf(thl, 0, LOG_LEVELS_ERROR, "%s\t%s", "one", "two");
22 
23  thl->log(thl, 0, "this is a debug log", LOG_LEVELS_DEBUG);
24  thl->logf(thl, 0, LOG_LEVELS_DEBUG, "%s\t%s", "one", "two");
25  // commenting this out seems to get rid of memleaks reported by valgrind
26  // pthread_exit(NULL);
27  return NULL;
28 }
29 
30 void *test_file_log(void *data) {
31  file_logger *fhl = (file_logger *)data;
32  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is an info log", LOG_LEVELS_INFO);
33  fhl->thl->logf(fhl->thl, fhl->file_descriptor, LOG_LEVELS_INFO, "%s\t%s", "one", "two");
34 
35  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is a warn log", LOG_LEVELS_WARN);
36  fhl->thl->logf(fhl->thl, fhl->file_descriptor, LOG_LEVELS_WARN, "%s\t%s", "one", "two");
37 
38  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is an error log", LOG_LEVELS_ERROR);
39  fhl->thl->logf(fhl->thl, fhl->file_descriptor, LOG_LEVELS_ERROR, "%s\t%s", "one", "two");
40 
41  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is a debug log", LOG_LEVELS_DEBUG);
42  fhl->thl->logf(fhl->thl, fhl->file_descriptor, LOG_LEVELS_DEBUG, "%s\t%s", "one", "two");
43  // commenting this out seems to get rid of memleaks reported by valgrind
44  // pthread_exit(NULL);
45  return NULL;
46 }
47 
48 
49 #pragma GCC diagnostic ignored "-Wunused-parameter"
50 void test_thread_logger(void **state) {
51  bool args[2] = {false, true};
52  for (int i = 0; i < 2; i++) {
54  assert(thl != NULL);
55  thl->log(thl, 0, "this is an info log", LOG_LEVELS_INFO);
56  thl->log(thl, 0, "this is a warn log", LOG_LEVELS_WARN);
57  thl->log(thl, 0, "this is an error log", LOG_LEVELS_ERROR);
58  thl->log(thl, 0, "this is a debug log", LOG_LEVELS_DEBUG);
59  pthread_t threads[4];
60  pthread_attr_t attrs[4];
61  for (int i = 0; i < 4; i++) {
62  pthread_attr_init(&attrs[i]);
63  pthread_create(&threads[i], &attrs[i], test_thread_log, thl);
64  }
65  for (int i = 0; i < 4; i++) {
66  pthread_join(threads[i], NULL);
67  pthread_attr_destroy(&attrs[i]);
68  }
70  }
71 }
72 
73 #pragma GCC diagnostic ignored "-Wunused-parameter"
74 void test_file_logger(void **state) {
75  bool args[2] = {false, true};
76  for (int i = 0; i < 2; i++) {
77  file_logger *fhl = new_file_logger("file_logger_test.log", args[i]);
78  assert(fhl != NULL);
79  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is an info log", LOG_LEVELS_INFO);
80  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is a warn log", LOG_LEVELS_WARN);
81  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is an error log", LOG_LEVELS_ERROR);
82  fhl->thl->log(fhl->thl, fhl->file_descriptor, "this is a debug log", LOG_LEVELS_DEBUG);
83  pthread_t threads[4];
84  pthread_attr_t attrs[4];
85  for (int i = 0; i < 4; i++) {
86  pthread_attr_init(&attrs[i]);
87  pthread_create(&threads[i], &attrs[i], test_file_log, fhl);
88  }
89  for (int i = 0; i < 4; i++) {
90  pthread_join(threads[i], NULL);
91  pthread_attr_destroy(&attrs[i]);
92  }
93  clear_file_logger(fhl);
94  }
95 }
96 
97 int main(void) {
98  const struct CMUnitTest tests[] = {
99  cmocka_unit_test(test_thread_logger),
100  cmocka_unit_test(test_file_logger),
101  };
102  return cmocka_run_group_tests(tests, NULL, NULL);
103 }
clear_thread_logger
void clear_thread_logger(thread_logger *thl)
free resources for the threaded logger
Definition: logger.c:224
test_thread_logger
void test_thread_logger(void **state)
Definition: logger_test.c:50
LOG_LEVELS_DEBUG
@ LOG_LEVELS_DEBUG
Definition: logger.h:34
clear_file_logger
void clear_file_logger(file_logger *fhl)
free resources for the file ogger
Definition: logger.c:228
LOG_LEVELS_ERROR
@ LOG_LEVELS_ERROR
Definition: logger.h:32
file_logger::thl
thread_logger * thl
Definition: logger.h:79
thread_logger::log
log_fn log
Definition: logger.h:69
thread_logger::logf
log_fnf logf
Definition: logger.h:70
test_file_logger
void test_file_logger(void **state)
Definition: logger_test.c:74
args
Definition: colors_test.c:12
test_thread_log
void * test_thread_log(void *data)
Definition: logger_test.c:12
new_file_logger
file_logger * new_file_logger(char *output_file, bool with_debug)
returns a new file_logger Calls new_thread_logger internally
Definition: logger.c:43
thread_logger
Definition: logger.h:63
test_file_log
void * test_file_log(void *data)
Definition: logger_test.c:30
file_logger::file_descriptor
int file_descriptor
Definition: logger.h:80
LOG_LEVELS_INFO
@ LOG_LEVELS_INFO
Definition: logger.h:28
main
int main(void)
Definition: logger_test.c:97
file_logger
Definition: logger.h:78
LOG_LEVELS_WARN
@ LOG_LEVELS_WARN
Definition: logger.h:30
new_thread_logger
thread_logger * new_thread_logger(bool with_debug)
returns a new thread safe logger if with_debug is false, then all debug_log calls will be ignored
Definition: logger.c:28