librfn
An ad-hoc utility library
mlogtest.c
Go to the documentation of this file.
1 /*
2  * mlogtest.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2012, 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #undef NDEBUG
15 
16 #include <librfn.h>
17 
18 bool compare_line(int n, const char *val)
19 {
20  char *logval = mlog_get_line(n);
21  bool result;
22 
23  if (logval == NULL || val == NULL)
24  result = logval == val;
25  else
26  result = 0 == strcmp(logval, val);
27 
28  if (!result) {
29  printf("failed\n");
30  printf("Expected: \"%s\"\n", val ? val : "<null>");
31  printf("Got: \"%s\"\n", logval ? logval : "<null>");
32  }
33 
34  if (logval)
35  free(logval);
36 
37  return result;
38 }
39 
40 int main()
41 {
42  /* very verbose... don't verify this */
43  for (int i=0; i<1024; i++)
44  assert(compare_line(i, NULL));
45 
46  mlog("logtest\n");
47  verify(compare_line(0, "logtest\n"));
48  verify(compare_line(1, NULL));
49 
50  mlog("char %c", 'A');
51  verify(compare_line(1, "char A"));
52  verify(compare_line(2, NULL));
53 
54  mlog("int %d", 1000000);
55  verify(compare_line(2, "int 1000000"));
56  verify(compare_line(3, NULL));
57 
58  mlog("long %ld", 1000000);
59  verify(compare_line(3, "long 1000000"));
60  verify(compare_line(4, NULL));
61 
62  mlog("ptr %p", 0x1234);
63  verify(compare_line(4, "ptr 0x1234"));
64  verify(compare_line(5, NULL));
65 
66  /* very verbose... don't verify this */
67  mlog_clear();
68  for (int i = 0; i < 1000 + 256; i++)
69  assert(compare_line(i, NULL));
70 
71  /* overflow the buffer */
72  for (int i = 0; i < 1000 + 256; i++)
73  mlog("%d", i);
74  verify(compare_line(0, "1000"));
75  verify(compare_line(1, "1001"));
76  verify(compare_line(254, "1254"));
77  verify(compare_line(255, "1255"));
78  verify(compare_line(256, NULL));
79 
80  /* very verbose final check */
81  for (int i=0; i<256; i++) {
82  char check[10];
83  sprintf(check, "%d", i+1000);
84  assert(compare_line(i, check));
85  }
86 
87  /* nice logging doesn't do anything (because the buffer is in overflow
88  * already)
89  */
90  mlog_nice("This message won't be logged");
91  verify(compare_line(0, "1000"));
92  verify(compare_line(255, "1255"));
93 
94  /* check that mlog_nice() does not overflow */
95  mlog_clear();
96  for (int i = 0; i < 1000 + 256; i++)
97  mlog_nice("%d", i);
98  verify(compare_line(0, "0"));
99  verify(compare_line(1, "1"));
100  verify(compare_line(254, "254"));
101  verify(compare_line(255, "255"));
102  verify(compare_line(256, NULL));
103 
104  return 0;
105 }
void mlog_nice(const char *fmt,...)
Log a message, if there is space to do so.
Definition: mlog.c:62
char * mlog_get_line(int n)
Format the Nth line of the log.
Definition: mlog.c:96
bool compare_line(int n, const char *val)
Definition: mlogtest.c:18
void mlog_clear(void)
Clear all data from the log.
Definition: mlog.c:71
#define verify(x)
Definition: util.h:55
void mlog(const char *fmt,...)
Log a message.
Definition: mlog.c:47
int main()
Definition: mlogtest.c:40