2
0

switch_log.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2021, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Chris Rienzo <chris@signalwire.com>
  26. *
  27. *
  28. * switch_log.c -- tests core logging
  29. *
  30. */
  31. #include <switch.h>
  32. #include <stdlib.h>
  33. #include <test/switch_test.h>
  34. switch_memory_pool_t *pool = NULL;
  35. static switch_mutex_t *mutex = NULL;
  36. switch_thread_cond_t *cond = NULL;
  37. static cJSON *last_alert_log = NULL;
  38. static switch_log_json_format_t json_format = {
  39. { NULL, NULL }, // version
  40. { NULL, NULL }, // host
  41. { NULL, NULL }, // timestamp
  42. { "level", NULL }, // level
  43. { NULL, NULL }, // ident
  44. { NULL, NULL }, // pid
  45. { NULL, NULL }, // uuid
  46. { NULL, NULL }, // file
  47. { NULL, NULL }, // line
  48. { NULL, NULL }, // function
  49. { "message", NULL }, // full_message
  50. { NULL, NULL }, // short_message
  51. NULL, // custom_field_prefix
  52. 0.0, // timestamp_divisor
  53. { NULL, NULL } // sequence
  54. };
  55. static switch_status_t test_logger(const switch_log_node_t *node, switch_log_level_t level)
  56. {
  57. switch_mutex_lock(mutex);
  58. if (level == SWITCH_LOG_ALERT && !last_alert_log && node->content && strstr(node->content, "switch_log test: ")) {
  59. last_alert_log = switch_log_node_to_json(node, level, &json_format, NULL);
  60. switch_thread_cond_signal(cond);
  61. }
  62. switch_mutex_unlock(mutex);
  63. return SWITCH_STATUS_SUCCESS;
  64. }
  65. static char *wait_for_log(switch_interval_time_t timeout_ms)
  66. {
  67. char *log_str = NULL;
  68. cJSON *log = NULL;
  69. switch_time_t now = switch_time_now();
  70. switch_time_t expiration = now + (timeout_ms * 1000);
  71. switch_mutex_lock(mutex);
  72. while (!last_alert_log && (now = switch_time_now()) < expiration) {
  73. switch_interval_time_t timeout = expiration - now;
  74. switch_thread_cond_timedwait(cond, mutex, timeout);
  75. }
  76. log = last_alert_log;
  77. last_alert_log = NULL;
  78. switch_mutex_unlock(mutex);
  79. if (log) {
  80. log_str = cJSON_PrintUnformatted(log);
  81. cJSON_Delete(log);
  82. }
  83. return log_str;
  84. }
  85. FST_CORE_BEGIN("./conf")
  86. {
  87. FST_SUITE_BEGIN(switch_log)
  88. {
  89. switch_core_new_memory_pool(&pool);
  90. switch_mutex_init(&mutex, SWITCH_MUTEX_NESTED, pool);
  91. switch_thread_cond_create(&cond, pool);
  92. FST_SETUP_BEGIN()
  93. {
  94. json_format.custom_field_prefix = NULL;
  95. }
  96. FST_SETUP_END()
  97. FST_TEARDOWN_BEGIN()
  98. {
  99. }
  100. FST_TEARDOWN_END()
  101. FST_SESSION_BEGIN(switch_log_meta_printf)
  102. {
  103. cJSON *item = NULL;
  104. cJSON *meta = NULL;
  105. char *log = NULL;
  106. switch_log_bind_logger(test_logger, SWITCH_LOG_ALERT, SWITCH_FALSE);
  107. switch_log_meta_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ALERT, NULL, "switch_log test: Plain channel log %d\n", 0);
  108. log = wait_for_log(1000);
  109. fst_check_string_equals(log, "{\"level\":1,\"message\":\"switch_log test: Plain channel log 0\\n\"}");
  110. switch_safe_free(log);
  111. switch_log_meta_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_ALERT, NULL, "switch_log test: Plain session log %d\n", 1);
  112. log = wait_for_log(1000);
  113. fst_check_string_equals(log, "{\"level\":1,\"message\":\"switch_log test: Plain session log 1\\n\"}");
  114. switch_safe_free(log);
  115. switch_log_meta_printf(SWITCH_CHANNEL_UUID_LOG(switch_core_session_get_uuid(fst_session)), SWITCH_LOG_ALERT, NULL, "switch_log test: Plain uuid log %d\n", 2);
  116. log = wait_for_log(1000);
  117. fst_check_string_equals(log, "{\"level\":1,\"message\":\"switch_log test: Plain uuid log 2\\n\"}");
  118. switch_safe_free(log);
  119. meta = cJSON_CreateObject();
  120. cJSON_AddStringToObject(meta, "foo", "bar");
  121. cJSON_AddNumberToObject(meta, "measure", 3.14159);
  122. switch_log_meta_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ALERT, &meta, "switch_log test: channel log with metadata %d\n", 3);
  123. fst_xcheck(meta == NULL, "Expect logging meta data to be consumed by switch_log_meta_printf()");
  124. log = wait_for_log(1000);
  125. fst_check_string_equals(log, "{\"foo\":\"bar\",\"measure\":3.14159,\"level\":1,\"message\":\"switch_log test: channel log with metadata 3\\n\"}");
  126. switch_safe_free(log);
  127. meta = cJSON_CreateObject();
  128. cJSON_AddStringToObject(meta, "foo", "bar");
  129. cJSON_AddNumberToObject(meta, "measure", 3.14159);
  130. switch_log_meta_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_ALERT, &meta, "switch_log test: Session log with metadata %d\n", 4);
  131. fst_xcheck(meta == NULL, "Expect logging meta data to be consumed by switch_log_meta_printf()");
  132. log = wait_for_log(1000);
  133. fst_check_string_equals(log, "{\"foo\":\"bar\",\"measure\":3.14159,\"level\":1,\"message\":\"switch_log test: Session log with metadata 4\\n\"}");
  134. switch_safe_free(log);
  135. meta = cJSON_CreateObject();
  136. cJSON_AddStringToObject(meta, "foo", "bar");
  137. cJSON_AddNumberToObject(meta, "measure", 3.14159);
  138. switch_log_meta_printf(SWITCH_CHANNEL_UUID_LOG(switch_core_session_get_uuid(fst_session)), SWITCH_LOG_ALERT, &meta, "switch_log test: uuid log with metadata %d\n", 5);
  139. fst_xcheck(meta == NULL, "Expect logging meta data to be consumed by switch_log_meta_printf()");
  140. log = wait_for_log(1000);
  141. fst_check_string_equals(log, "{\"foo\":\"bar\",\"measure\":3.14159,\"level\":1,\"message\":\"switch_log test: uuid log with metadata 5\\n\"}");
  142. switch_safe_free(log);
  143. meta = cJSON_CreateObject();
  144. item = cJSON_AddObjectToObject(meta, "nested");
  145. cJSON_AddStringToObject(item, "stringval", "1234");
  146. item = cJSON_AddArrayToObject(item, "array");
  147. cJSON_AddItemToArray(item, cJSON_CreateString("12"));
  148. item = cJSON_AddArrayToObject(meta, "array2");
  149. cJSON_AddItemToArray(item, cJSON_CreateString("34"));
  150. switch_log_meta_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_ALERT, &meta, "switch_log test: session log with complex metadata %d\n", 6);
  151. fst_xcheck(meta == NULL, "Expect logging meta data to be consumed by switch_log_meta_printf()");
  152. log = wait_for_log(1000);
  153. fst_check_string_equals(log, "{\"nested\":{\"stringval\":\"1234\",\"array\":[\"12\"]},\"array2\":[\"34\"],\"level\":1,\"message\":\"switch_log test: session log with complex metadata 6\\n\"}");
  154. switch_safe_free(log);
  155. meta = cJSON_CreateObject();
  156. item = cJSON_AddObjectToObject(meta, "nested");
  157. cJSON_AddStringToObject(item, "stringval", "1234");
  158. item = cJSON_AddArrayToObject(item, "array");
  159. cJSON_AddItemToArray(item, cJSON_CreateString("12"));
  160. item = cJSON_AddArrayToObject(meta, "array2");
  161. cJSON_AddItemToArray(item, cJSON_CreateString("34"));
  162. json_format.custom_field_prefix = "prefix.";
  163. switch_log_meta_printf(SWITCH_CHANNEL_SESSION_LOG(fst_session), SWITCH_LOG_ALERT, &meta, "switch_log test: session log with prefixed complex metadata %d\n", 7);
  164. fst_xcheck(meta == NULL, "Expect logging meta data to be consumed by switch_log_meta_printf()");
  165. log = wait_for_log(1000);
  166. fst_check_string_equals(log, "{\"prefix.nested\":{\"stringval\":\"1234\",\"array\":[\"12\"]},\"prefix.array2\":[\"34\"],\"level\":1,\"message\":\"switch_log test: session log with prefixed complex metadata 7\\n\"}");
  167. switch_safe_free(log);
  168. cJSON_Delete(last_alert_log);
  169. last_alert_log = NULL;
  170. switch_log_unbind_logger(test_logger);
  171. }
  172. FST_SESSION_END()
  173. }
  174. FST_SUITE_END()
  175. switch_core_destroy_memory_pool(&pool);
  176. }
  177. FST_CORE_END()