test_log_message_content.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #if defined(_WIN32) || defined(_WIN64)
  2. #define _CRT_NONSTDC_NO_WARNINGS
  3. #endif
  4. #define ZF_LOG_ANDROID_LOG 0
  5. #define ZF_LOG_BUF_SZ 128
  6. #define ZF_LOG_MEM_WIDTH 16
  7. #define ZF_LOG_INSTRUMENTED 1
  8. #define ZF_LOG_LEVEL ZF_LOG_INFO
  9. #define ZF_LOG_TAG "TAG"
  10. #include <zf_log.c>
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(snprintf)
  15. #define snprintf(buf, len, ...) _snprintf_s(buf, len, _TRUNCATE, __VA_ARGS__)
  16. #endif
  17. #define MESSAGE_EXPECTED_PRINTF_FMT__ ""
  18. #define MESSAGE_EXPECTED_PRINTF_FMT__YEAR "2016"
  19. #define MESSAGE_EXPECTED_PRINTF_FMT__MONTH "12"
  20. #define MESSAGE_EXPECTED_PRINTF_FMT__DAY "23"
  21. #define MESSAGE_EXPECTED_PRINTF_FMT__HOUR "12"
  22. #define MESSAGE_EXPECTED_PRINTF_FMT__MINUTE "34"
  23. #define MESSAGE_EXPECTED_PRINTF_FMT__SECOND "56"
  24. #define MESSAGE_EXPECTED_PRINTF_FMT__MILLISECOND "789"
  25. #define MESSAGE_EXPECTED_PRINTF_FMT__PID " 9876"
  26. #define MESSAGE_EXPECTED_PRINTF_FMT__TID " 5432"
  27. #define MESSAGE_EXPECTED_PRINTF_FMT__LEVEL "I"
  28. #define MESSAGE_EXPECTED_PRINTF_FMT__TAG(ps, ts) "prefix" ps "TAG" ts
  29. #define MESSAGE_EXPECTED_PRINTF_FMT__FUNCTION "function"
  30. #define MESSAGE_EXPECTED_PRINTF_FMT__FILENAME "file"
  31. #define MESSAGE_EXPECTED_PRINTF_FMT__FILELINE "42"
  32. #define MESSAGE_EXPECTED_PRINTF_FMT__S(s) s
  33. #define MESSAGE_EXPECTED_PRINTF_FMT__F_INIT(expr) ""
  34. #define MESSAGE_EXPECTED_PRINTF_FMT__F_UINT(w, v) "%" #w "u"
  35. #define MESSAGE_EXPECTED_PRINTF_FMT(field) \
  36. _PP_CONCAT_3(MESSAGE_EXPECTED_PRINTF_FMT_, _, field)
  37. #define MESSAGE_EXPECTED_PRINTF_VAL__
  38. #define MESSAGE_EXPECTED_PRINTF_VAL__YEAR
  39. #define MESSAGE_EXPECTED_PRINTF_VAL__MONTH
  40. #define MESSAGE_EXPECTED_PRINTF_VAL__DAY
  41. #define MESSAGE_EXPECTED_PRINTF_VAL__HOUR
  42. #define MESSAGE_EXPECTED_PRINTF_VAL__MINUTE
  43. #define MESSAGE_EXPECTED_PRINTF_VAL__SECOND
  44. #define MESSAGE_EXPECTED_PRINTF_VAL__MILLISECOND
  45. #define MESSAGE_EXPECTED_PRINTF_VAL__PID
  46. #define MESSAGE_EXPECTED_PRINTF_VAL__TID
  47. #define MESSAGE_EXPECTED_PRINTF_VAL__LEVEL
  48. #define MESSAGE_EXPECTED_PRINTF_VAL__TAG(ps, ts)
  49. #define MESSAGE_EXPECTED_PRINTF_VAL__FUNCTION
  50. #define MESSAGE_EXPECTED_PRINTF_VAL__FILENAME
  51. #define MESSAGE_EXPECTED_PRINTF_VAL__FILELINE
  52. #define MESSAGE_EXPECTED_PRINTF_VAL__S(s)
  53. #define MESSAGE_EXPECTED_PRINTF_VAL__F_INIT(expr)
  54. #define MESSAGE_EXPECTED_PRINTF_VAL__F_UINT(w, v) ,v
  55. #define MESSAGE_EXPECTED_PRINTF_VAL(field) \
  56. _PP_CONCAT_3(MESSAGE_EXPECTED_PRINTF_VAL_, _, field)
  57. #define MESSAGE_EXPECTED_PRINTF_FMTS \
  58. _PP_MAP(MESSAGE_EXPECTED_PRINTF_FMT, ZF_LOG_MESSAGE_CTX_FORMAT) \
  59. _PP_MAP(MESSAGE_EXPECTED_PRINTF_FMT, ZF_LOG_MESSAGE_TAG_FORMAT) \
  60. _PP_MAP(MESSAGE_EXPECTED_PRINTF_FMT, ZF_LOG_MESSAGE_SRC_FORMAT)
  61. #define MESSAGE_EXPECTED_PRINTF_VALS \
  62. _PP_MAP(MESSAGE_EXPECTED_PRINTF_VAL, ZF_LOG_MESSAGE_CTX_FORMAT) \
  63. _PP_MAP(MESSAGE_EXPECTED_PRINTF_VAL, ZF_LOG_MESSAGE_TAG_FORMAT) \
  64. _PP_MAP(MESSAGE_EXPECTED_PRINTF_VAL, ZF_LOG_MESSAGE_SRC_FORMAT)
  65. static const char c_test_fmt[] =
  66. "Lorem ipsum dolor sit amet.";
  67. static const char c_test_mem[] =
  68. "Here's to the crazy ones.";
  69. static const char *c_msg_expected_lines[1];
  70. static const char *c_mem_expected_lines[3];
  71. #define MAX_LINES 4
  72. static char g_lines[MAX_LINES][ZF_LOG_BUF_SZ];
  73. static size_t g_len[MAX_LINES];
  74. static size_t g_null_pos[MAX_LINES];
  75. static size_t g_line;
  76. static size_t memchk(const void *const b, const int c, const size_t sz)
  77. {
  78. const unsigned char v = (unsigned char)c;
  79. const unsigned char *const s = (const unsigned char *)b;
  80. const unsigned char *const e = s + sz;
  81. const unsigned char *p = s;
  82. for (;p != e && v == *p; ++p) {}
  83. return (size_t)(p - s);
  84. }
  85. static size_t common_prefix(const char *const s1, const size_t s1_len,
  86. const char *const s2, const size_t s2_len)
  87. {
  88. const char *const e1 = s1 + s1_len;
  89. const char *const e2 = s2 + s2_len;
  90. const char *c1 = s1, *c2 = s2;
  91. for (;e1 != c1 && e2 != c2 && *c1 == *c2; ++c1, ++c2) {}
  92. return (size_t)(c1 - s1);
  93. }
  94. static void reset()
  95. {
  96. g_buf_sz = ZF_LOG_BUF_SZ;
  97. for (size_t i = 0; MAX_LINES > i; ++i)
  98. {
  99. memset(g_lines[i], -1, ZF_LOG_BUF_SZ);
  100. g_len[i] = 0;
  101. g_null_pos[i] = 0;
  102. }
  103. g_line = 0;
  104. }
  105. static void mock_time_callback(struct tm *const tm, unsigned *const msec)
  106. {
  107. memset(tm, 0, sizeof(*tm));
  108. tm->tm_sec = 56;
  109. tm->tm_min = 34;
  110. tm->tm_hour = 12;
  111. tm->tm_mday = 23;
  112. tm->tm_mon = 11;
  113. tm->tm_year = 2016 - 1900;
  114. *msec = 789;
  115. }
  116. static void mock_pid_callback(int *const pid, int *const tid)
  117. {
  118. *pid = 9876;
  119. *tid = 5432;
  120. }
  121. static void mock_buffer_callback(zf_log_message *msg, char *buf)
  122. {
  123. memset(buf, -1, ZF_LOG_BUF_SZ);
  124. buffer_callback(msg, buf);
  125. }
  126. static void mock_output_callback(const zf_log_message *msg, void *arg)
  127. {
  128. (void)arg;
  129. const size_t i = g_line++;
  130. if (MAX_LINES <= i)
  131. {
  132. fprintf(stderr, "too many lines produced\n");
  133. exit(1);
  134. }
  135. char *const line = g_lines[i];
  136. memcpy(line, msg->buf, ZF_LOG_BUF_SZ);
  137. const size_t len = (size_t)(msg->p - msg->buf);
  138. size_t null_pos;
  139. for (null_pos = 0; len > null_pos && 0 != line[null_pos]; ++null_pos) {}
  140. g_len[i] = len;
  141. g_null_pos[i] = null_pos;
  142. }
  143. static void verify_log_output(const size_t buf_sz,
  144. const char *const expected[],
  145. const size_t expected_n)
  146. {
  147. const size_t modifiable = buf_sz + 1;
  148. const size_t unmodifiable = ZF_LOG_BUF_SZ - modifiable;
  149. if (g_line > expected_n)
  150. {
  151. fprintf(stderr, "Lines produced: actual=%u, expected=<%u\n",
  152. (unsigned)g_line, (unsigned)expected_n);
  153. exit(1);
  154. }
  155. size_t complete_lines = 0;
  156. for (size_t i = 0; g_line > i; ++i)
  157. {
  158. const char *const line = g_lines[i];
  159. const size_t line_len = strlen(expected[i]);
  160. const size_t untouched = memchk(line + modifiable, -1, unmodifiable);
  161. const size_t match = common_prefix(expected[i], line_len,
  162. line, g_len[i]);
  163. if (untouched != unmodifiable)
  164. {
  165. fprintf(stderr, "Untouched bytes: actual=%u, expected=%u\n",
  166. (unsigned)untouched, (unsigned)unmodifiable);
  167. exit(1);
  168. }
  169. if (g_null_pos[i] != g_len[i])
  170. {
  171. fprintf(stderr, "Null position: actual=%u, expected=%u\n",
  172. (unsigned)g_null_pos[i], (unsigned)g_len[i]);
  173. exit(1);
  174. }
  175. if (match < g_len[i])
  176. {
  177. fprintf(stderr, "Line partial match: actual=%u, expected=>%u\n",
  178. (unsigned)match, (unsigned)g_len[i]);
  179. exit(1);
  180. }
  181. if (line_len <= buf_sz)
  182. {
  183. ++complete_lines;
  184. if (line_len <= buf_sz && match != g_len[i])
  185. {
  186. fprintf(stderr, "Line complete match: actual=%u, expected=%u\n",
  187. (unsigned)match, (unsigned)g_len[i]);
  188. exit(1);
  189. }
  190. }
  191. }
  192. if (expected_n == complete_lines && g_line != expected_n)
  193. {
  194. fprintf(stderr, "Complete lines produced: actual=%u, expected=<%u\n",
  195. (unsigned)g_line, (unsigned)expected_n);
  196. exit(1);
  197. }
  198. }
  199. static void test_msg_output()
  200. {
  201. for (unsigned buf_sz = 0; ZF_LOG_BUF_SZ - ZF_LOG_EOL_SZ >= buf_sz; ++buf_sz)
  202. {
  203. reset();
  204. g_buf_sz = buf_sz;
  205. _zf_log_write_d("function", "file", 42, ZF_LOG_INFO, ZF_LOG_TAG,
  206. c_test_fmt);
  207. verify_log_output(buf_sz,
  208. c_msg_expected_lines, _countof(c_msg_expected_lines));
  209. }
  210. }
  211. static void test_mem_output()
  212. {
  213. for (unsigned buf_sz = 0; ZF_LOG_BUF_SZ - ZF_LOG_EOL_SZ >= buf_sz; ++buf_sz)
  214. {
  215. reset();
  216. g_buf_sz = buf_sz;
  217. _zf_log_write_mem_d("function", "file", 42, ZF_LOG_INFO, ZF_LOG_TAG,
  218. c_test_mem, sizeof(c_test_mem),
  219. c_test_fmt);
  220. verify_log_output(buf_sz,
  221. c_mem_expected_lines, _countof(c_mem_expected_lines));
  222. }
  223. }
  224. static void init_expected_lines()
  225. {
  226. char expected_header[256];
  227. char line[512];
  228. _PP_MAP(_ZF_LOG_MESSAGE_FORMAT_INIT, ZF_LOG_MESSAGE_CTX_FORMAT)
  229. _PP_MAP(_ZF_LOG_MESSAGE_FORMAT_INIT, ZF_LOG_MESSAGE_TAG_FORMAT)
  230. _PP_MAP(_ZF_LOG_MESSAGE_FORMAT_INIT, ZF_LOG_MESSAGE_SRC_FORMAT)
  231. #if _ZF_LOG_MESSAGE_FORMAT_FIELDS(ZF_LOG_MESSAGE_CTX_FORMAT) || \
  232. _ZF_LOG_MESSAGE_FORMAT_FIELDS(ZF_LOG_MESSAGE_TAG_FORMAT) || \
  233. _ZF_LOG_MESSAGE_FORMAT_FIELDS(ZF_LOG_MESSAGE_SRC_FORMAT)
  234. snprintf(expected_header, sizeof(expected_header),
  235. MESSAGE_EXPECTED_PRINTF_FMTS
  236. MESSAGE_EXPECTED_PRINTF_VALS);
  237. #else
  238. *expected_header ='\0';
  239. #endif
  240. snprintf(line, sizeof(line), "%s%s", expected_header,
  241. "Lorem ipsum dolor sit amet.");
  242. c_msg_expected_lines[0] = strdup(line);
  243. snprintf(line, sizeof(line), "%s%s", expected_header,
  244. "Lorem ipsum dolor sit amet.");
  245. c_mem_expected_lines[0] = strdup(line);
  246. snprintf(line, sizeof(line), "%s%s", expected_header,
  247. "48657265277320746f20746865206372 Here's to the cr");
  248. c_mem_expected_lines[1] = strdup(line);
  249. snprintf(line, sizeof(line), "%s%s", expected_header,
  250. "617a79206f6e65732e00 azy ones.?");
  251. c_mem_expected_lines[2] = strdup(line);
  252. }
  253. int main(int argc, char *argv[])
  254. {
  255. (void)argc; (void)argv;
  256. g_time_cb = mock_time_callback;
  257. g_pid_cb = mock_pid_callback;
  258. g_buffer_cb = mock_buffer_callback;
  259. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, mock_output_callback);
  260. zf_log_set_tag_prefix("prefix");
  261. init_expected_lines();
  262. test_msg_output();
  263. test_mem_output();
  264. return 0;
  265. }