logging.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/logging.h>
  6. #include <aws/common/file.h>
  7. #include <aws/common/log_channel.h>
  8. #include <aws/common/log_formatter.h>
  9. #include <aws/common/log_writer.h>
  10. #include <aws/common/mutex.h>
  11. #include <aws/common/string.h>
  12. #include <errno.h>
  13. #include <stdarg.h>
  14. #ifdef _MSC_VER
  15. # pragma warning(disable : 4204) /* non-constant aggregate initializer */
  16. #endif
  17. /*
  18. * Null logger implementation
  19. */
  20. static enum aws_log_level s_null_logger_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) {
  21. (void)logger;
  22. (void)subject;
  23. return AWS_LL_NONE;
  24. }
  25. static int s_null_logger_log(
  26. struct aws_logger *logger,
  27. enum aws_log_level log_level,
  28. aws_log_subject_t subject,
  29. const char *format,
  30. ...) {
  31. (void)logger;
  32. (void)log_level;
  33. (void)subject;
  34. (void)format;
  35. return AWS_OP_SUCCESS;
  36. }
  37. static void s_null_logger_clean_up(struct aws_logger *logger) {
  38. (void)logger;
  39. }
  40. static struct aws_logger_vtable s_null_vtable = {
  41. .get_log_level = s_null_logger_get_log_level,
  42. .log = s_null_logger_log,
  43. .clean_up = s_null_logger_clean_up,
  44. };
  45. static struct aws_logger s_null_logger = {
  46. .vtable = &s_null_vtable,
  47. .allocator = NULL,
  48. .p_impl = NULL,
  49. };
  50. /*
  51. * Pipeline logger implementation
  52. */
  53. static void s_aws_logger_pipeline_owned_clean_up(struct aws_logger *logger) {
  54. struct aws_logger_pipeline *impl = logger->p_impl;
  55. AWS_ASSERT(impl->channel->vtable->clean_up != NULL);
  56. (impl->channel->vtable->clean_up)(impl->channel);
  57. AWS_ASSERT(impl->formatter->vtable->clean_up != NULL);
  58. (impl->formatter->vtable->clean_up)(impl->formatter);
  59. AWS_ASSERT(impl->writer->vtable->clean_up != NULL);
  60. (impl->writer->vtable->clean_up)(impl->writer);
  61. aws_mem_release(impl->allocator, impl->channel);
  62. aws_mem_release(impl->allocator, impl->formatter);
  63. aws_mem_release(impl->allocator, impl->writer);
  64. aws_mem_release(impl->allocator, impl);
  65. }
  66. /*
  67. * Pipeline logger implementation
  68. */
  69. static int s_aws_logger_pipeline_log(
  70. struct aws_logger *logger,
  71. enum aws_log_level log_level,
  72. aws_log_subject_t subject,
  73. const char *format,
  74. ...) {
  75. va_list format_args;
  76. va_start(format_args, format);
  77. struct aws_logger_pipeline *impl = logger->p_impl;
  78. struct aws_string *output = NULL;
  79. AWS_ASSERT(impl->formatter->vtable->format != NULL);
  80. int result = (impl->formatter->vtable->format)(impl->formatter, &output, log_level, subject, format, format_args);
  81. va_end(format_args);
  82. if (result != AWS_OP_SUCCESS || output == NULL) {
  83. return AWS_OP_ERR;
  84. }
  85. AWS_ASSERT(impl->channel->vtable->send != NULL);
  86. if ((impl->channel->vtable->send)(impl->channel, output)) {
  87. /*
  88. * failure to send implies failure to transfer ownership
  89. */
  90. aws_string_destroy(output);
  91. return AWS_OP_ERR;
  92. }
  93. return AWS_OP_SUCCESS;
  94. }
  95. static enum aws_log_level s_aws_logger_pipeline_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) {
  96. (void)subject;
  97. struct aws_logger_pipeline *impl = logger->p_impl;
  98. return (enum aws_log_level)aws_atomic_load_int(&impl->level);
  99. }
  100. static int s_aws_logger_pipeline_set_log_level(struct aws_logger *logger, enum aws_log_level level) {
  101. struct aws_logger_pipeline *impl = logger->p_impl;
  102. aws_atomic_store_int(&impl->level, (size_t)level);
  103. return AWS_OP_SUCCESS;
  104. }
  105. struct aws_logger_vtable g_pipeline_logger_owned_vtable = {
  106. .get_log_level = s_aws_logger_pipeline_get_log_level,
  107. .log = s_aws_logger_pipeline_log,
  108. .clean_up = s_aws_logger_pipeline_owned_clean_up,
  109. .set_log_level = s_aws_logger_pipeline_set_log_level,
  110. };
  111. int aws_logger_init_standard(
  112. struct aws_logger *logger,
  113. struct aws_allocator *allocator,
  114. struct aws_logger_standard_options *options) {
  115. #ifdef ANDROID
  116. (void)options;
  117. extern int aws_logger_init_logcat(
  118. struct aws_logger *, struct aws_allocator *, struct aws_logger_standard_options *);
  119. return aws_logger_init_logcat(logger, allocator, options);
  120. #endif
  121. struct aws_logger_pipeline *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_logger_pipeline));
  122. if (impl == NULL) {
  123. return AWS_OP_ERR;
  124. }
  125. struct aws_log_writer *writer = aws_mem_acquire(allocator, sizeof(struct aws_log_writer));
  126. if (writer == NULL) {
  127. goto on_allocate_writer_failure;
  128. }
  129. struct aws_log_writer_file_options file_writer_options = {
  130. .filename = options->filename,
  131. .file = options->file,
  132. };
  133. if (aws_log_writer_init_file(writer, allocator, &file_writer_options)) {
  134. goto on_init_writer_failure;
  135. }
  136. struct aws_log_formatter *formatter = aws_mem_acquire(allocator, sizeof(struct aws_log_formatter));
  137. if (formatter == NULL) {
  138. goto on_allocate_formatter_failure;
  139. }
  140. struct aws_log_formatter_standard_options formatter_options = {.date_format = AWS_DATE_FORMAT_ISO_8601};
  141. if (aws_log_formatter_init_default(formatter, allocator, &formatter_options)) {
  142. goto on_init_formatter_failure;
  143. }
  144. struct aws_log_channel *channel = aws_mem_acquire(allocator, sizeof(struct aws_log_channel));
  145. if (channel == NULL) {
  146. goto on_allocate_channel_failure;
  147. }
  148. if (aws_log_channel_init_background(channel, allocator, writer) == AWS_OP_SUCCESS) {
  149. impl->formatter = formatter;
  150. impl->channel = channel;
  151. impl->writer = writer;
  152. impl->allocator = allocator;
  153. aws_atomic_store_int(&impl->level, (size_t)options->level);
  154. logger->vtable = &g_pipeline_logger_owned_vtable;
  155. logger->allocator = allocator;
  156. logger->p_impl = impl;
  157. return AWS_OP_SUCCESS;
  158. }
  159. aws_mem_release(allocator, channel);
  160. on_allocate_channel_failure:
  161. aws_log_formatter_clean_up(formatter);
  162. on_init_formatter_failure:
  163. aws_mem_release(allocator, formatter);
  164. on_allocate_formatter_failure:
  165. aws_log_writer_clean_up(writer);
  166. on_init_writer_failure:
  167. aws_mem_release(allocator, writer);
  168. on_allocate_writer_failure:
  169. aws_mem_release(allocator, impl);
  170. return AWS_OP_ERR;
  171. }
  172. /*
  173. * Pipeline logger implementation where all the components are externally owned. No clean up
  174. * is done on the components. Useful for tests where components are on the stack and often mocked.
  175. */
  176. static void s_aws_pipeline_logger_unowned_clean_up(struct aws_logger *logger) {
  177. struct aws_logger_pipeline *impl = (struct aws_logger_pipeline *)logger->p_impl;
  178. aws_mem_release(impl->allocator, impl);
  179. }
  180. static struct aws_logger_vtable s_pipeline_logger_unowned_vtable = {
  181. .get_log_level = s_aws_logger_pipeline_get_log_level,
  182. .log = s_aws_logger_pipeline_log,
  183. .clean_up = s_aws_pipeline_logger_unowned_clean_up,
  184. .set_log_level = s_aws_logger_pipeline_set_log_level,
  185. };
  186. int aws_logger_init_from_external(
  187. struct aws_logger *logger,
  188. struct aws_allocator *allocator,
  189. struct aws_log_formatter *formatter,
  190. struct aws_log_channel *channel,
  191. struct aws_log_writer *writer,
  192. enum aws_log_level level) {
  193. struct aws_logger_pipeline *impl = aws_mem_acquire(allocator, sizeof(struct aws_logger_pipeline));
  194. if (impl == NULL) {
  195. return AWS_OP_ERR;
  196. }
  197. impl->formatter = formatter;
  198. impl->channel = channel;
  199. impl->writer = writer;
  200. impl->allocator = allocator;
  201. aws_atomic_store_int(&impl->level, (size_t)level);
  202. logger->vtable = &s_pipeline_logger_unowned_vtable;
  203. logger->allocator = allocator;
  204. logger->p_impl = impl;
  205. return AWS_OP_SUCCESS;
  206. }
  207. /*
  208. * Global API
  209. */
  210. static struct aws_logger *s_root_logger_ptr = &s_null_logger;
  211. void aws_logger_set(struct aws_logger *logger) {
  212. if (logger != NULL) {
  213. s_root_logger_ptr = logger;
  214. } else {
  215. s_root_logger_ptr = &s_null_logger;
  216. }
  217. }
  218. struct aws_logger *aws_logger_get(void) {
  219. return s_root_logger_ptr;
  220. }
  221. struct aws_logger *aws_logger_get_conditional(aws_log_subject_t subject, enum aws_log_level level) {
  222. if (s_root_logger_ptr == NULL) {
  223. return NULL;
  224. }
  225. if (s_root_logger_ptr->vtable->get_log_level(s_root_logger_ptr, subject) < level) {
  226. return NULL;
  227. }
  228. return s_root_logger_ptr;
  229. }
  230. void aws_logger_clean_up(struct aws_logger *logger) {
  231. AWS_ASSERT(logger->vtable->clean_up != NULL);
  232. logger->vtable->clean_up(logger);
  233. }
  234. static const char *s_log_level_strings[AWS_LL_COUNT] = {"NONE", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"};
  235. int aws_log_level_to_string(enum aws_log_level log_level, const char **level_string) {
  236. AWS_ERROR_PRECONDITION(log_level < AWS_LL_COUNT);
  237. if (level_string != NULL) {
  238. *level_string = s_log_level_strings[log_level];
  239. }
  240. return AWS_OP_SUCCESS;
  241. }
  242. int aws_string_to_log_level(const char *level_string, enum aws_log_level *log_level) {
  243. if (level_string != NULL && log_level != NULL) {
  244. size_t level_length = strlen(level_string);
  245. for (int i = 0; i < AWS_LL_COUNT; ++i) {
  246. if (aws_array_eq_c_str_ignore_case(level_string, level_length, s_log_level_strings[i])) {
  247. *log_level = i;
  248. return AWS_OP_SUCCESS;
  249. }
  250. }
  251. }
  252. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  253. return AWS_OP_ERR;
  254. }
  255. int aws_thread_id_t_to_string(aws_thread_id_t thread_id, char *buffer, size_t bufsz) {
  256. AWS_ERROR_PRECONDITION(AWS_THREAD_ID_T_REPR_BUFSZ == bufsz);
  257. AWS_ERROR_PRECONDITION(buffer && AWS_MEM_IS_WRITABLE(buffer, bufsz));
  258. size_t current_index = 0;
  259. unsigned char *bytes = (unsigned char *)&thread_id;
  260. for (size_t i = sizeof(aws_thread_id_t); i != 0; --i) {
  261. unsigned char c = bytes[i - 1];
  262. int written = snprintf(buffer + current_index, bufsz - current_index, "%02x", c);
  263. if (written < 0) {
  264. return AWS_OP_ERR;
  265. }
  266. current_index += written;
  267. if (bufsz <= current_index) {
  268. return AWS_OP_ERR;
  269. }
  270. }
  271. return AWS_OP_SUCCESS;
  272. }
  273. #define AWS_LOG_SUBJECT_SPACE_MASK (AWS_LOG_SUBJECT_STRIDE - 1)
  274. static const uint32_t S_MAX_LOG_SUBJECT = AWS_LOG_SUBJECT_STRIDE * AWS_PACKAGE_SLOTS - 1;
  275. static const struct aws_log_subject_info_list *volatile s_log_subject_slots[AWS_PACKAGE_SLOTS] = {0};
  276. static const struct aws_log_subject_info *s_get_log_subject_info_by_id(aws_log_subject_t subject) {
  277. if (subject > S_MAX_LOG_SUBJECT) {
  278. return NULL;
  279. }
  280. uint32_t slot_index = subject >> AWS_LOG_SUBJECT_STRIDE_BITS;
  281. uint32_t subject_index = subject & AWS_LOG_SUBJECT_SPACE_MASK;
  282. const struct aws_log_subject_info_list *subject_slot = s_log_subject_slots[slot_index];
  283. if (!subject_slot || subject_index >= subject_slot->count) {
  284. return NULL;
  285. }
  286. return &subject_slot->subject_list[subject_index];
  287. }
  288. const char *aws_log_subject_name(aws_log_subject_t subject) {
  289. const struct aws_log_subject_info *subject_info = s_get_log_subject_info_by_id(subject);
  290. if (subject_info != NULL) {
  291. return subject_info->subject_name;
  292. }
  293. return "Unknown";
  294. }
  295. void aws_register_log_subject_info_list(struct aws_log_subject_info_list *log_subject_list) {
  296. /*
  297. * We're not so worried about these asserts being removed in an NDEBUG build
  298. * - we'll either segfault immediately (for the first two) or for the count
  299. * assert, the registration will be ineffective.
  300. */
  301. AWS_FATAL_ASSERT(log_subject_list);
  302. AWS_FATAL_ASSERT(log_subject_list->subject_list);
  303. AWS_FATAL_ASSERT(log_subject_list->count);
  304. const uint32_t min_range = log_subject_list->subject_list[0].subject_id;
  305. const uint32_t slot_index = min_range >> AWS_LOG_SUBJECT_STRIDE_BITS;
  306. #if DEBUG_BUILD
  307. for (uint32_t i = 0; i < log_subject_list->count; ++i) {
  308. const struct aws_log_subject_info *info = &log_subject_list->subject_list[i];
  309. uint32_t expected_id = min_range + i;
  310. if (expected_id != info->subject_id) {
  311. fprintf(stderr, "\"%s\" is at wrong index in aws_log_subject_info[]\n", info->subject_name);
  312. AWS_FATAL_ASSERT(0);
  313. }
  314. }
  315. #endif /* DEBUG_BUILD */
  316. if (slot_index >= AWS_PACKAGE_SLOTS) {
  317. /* This is an NDEBUG build apparently. Kill the process rather than
  318. * corrupting heap. */
  319. fprintf(stderr, "Bad log subject slot index 0x%016x\n", slot_index);
  320. abort();
  321. }
  322. s_log_subject_slots[slot_index] = log_subject_list;
  323. }
  324. void aws_unregister_log_subject_info_list(struct aws_log_subject_info_list *log_subject_list) {
  325. /*
  326. * We're not so worried about these asserts being removed in an NDEBUG build
  327. * - we'll either segfault immediately (for the first two) or for the count
  328. * assert, the registration will be ineffective.
  329. */
  330. AWS_FATAL_ASSERT(log_subject_list);
  331. AWS_FATAL_ASSERT(log_subject_list->subject_list);
  332. AWS_FATAL_ASSERT(log_subject_list->count);
  333. const uint32_t min_range = log_subject_list->subject_list[0].subject_id;
  334. const uint32_t slot_index = min_range >> AWS_LOG_SUBJECT_STRIDE_BITS;
  335. if (slot_index >= AWS_PACKAGE_SLOTS) {
  336. /* This is an NDEBUG build apparently. Kill the process rather than
  337. * corrupting heap. */
  338. fprintf(stderr, "Bad log subject slot index 0x%016x\n", slot_index);
  339. AWS_FATAL_ASSERT(false);
  340. }
  341. s_log_subject_slots[slot_index] = NULL;
  342. }
  343. /*
  344. * no alloc implementation
  345. */
  346. struct aws_logger_noalloc {
  347. struct aws_atomic_var level;
  348. FILE *file;
  349. bool should_close;
  350. struct aws_mutex lock;
  351. };
  352. static enum aws_log_level s_noalloc_stderr_logger_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) {
  353. (void)subject;
  354. struct aws_logger_noalloc *impl = logger->p_impl;
  355. return (enum aws_log_level)aws_atomic_load_int(&impl->level);
  356. }
  357. #define MAXIMUM_NO_ALLOC_LOG_LINE_SIZE 8192
  358. static int s_noalloc_stderr_logger_log(
  359. struct aws_logger *logger,
  360. enum aws_log_level log_level,
  361. aws_log_subject_t subject,
  362. const char *format,
  363. ...) {
  364. char format_buffer[MAXIMUM_NO_ALLOC_LOG_LINE_SIZE];
  365. va_list format_args;
  366. va_start(format_args, format);
  367. #ifdef _MSC_VER
  368. # pragma warning(push)
  369. # pragma warning(disable : 4221) /* allow struct member to reference format_buffer */
  370. #endif
  371. struct aws_logging_standard_formatting_data format_data = {
  372. .log_line_buffer = format_buffer,
  373. .total_length = MAXIMUM_NO_ALLOC_LOG_LINE_SIZE,
  374. .level = log_level,
  375. .subject_name = aws_log_subject_name(subject),
  376. .format = format,
  377. .date_format = AWS_DATE_FORMAT_ISO_8601,
  378. .allocator = logger->allocator,
  379. .amount_written = 0,
  380. };
  381. #ifdef _MSC_VER
  382. # pragma warning(pop) /* disallow struct member to reference local value */
  383. #endif
  384. int result = aws_format_standard_log_line(&format_data, format_args);
  385. va_end(format_args);
  386. if (result == AWS_OP_ERR) {
  387. return AWS_OP_ERR;
  388. }
  389. struct aws_logger_noalloc *impl = logger->p_impl;
  390. aws_mutex_lock(&impl->lock);
  391. int write_result = AWS_OP_SUCCESS;
  392. if (fwrite(format_buffer, 1, format_data.amount_written, impl->file) < format_data.amount_written) {
  393. int errno_value = errno; /* Always cache errno before potential side-effect */
  394. aws_translate_and_raise_io_error(errno_value);
  395. write_result = AWS_OP_ERR;
  396. }
  397. aws_mutex_unlock(&impl->lock);
  398. return write_result;
  399. }
  400. static void s_noalloc_stderr_logger_clean_up(struct aws_logger *logger) {
  401. if (logger == NULL) {
  402. return;
  403. }
  404. struct aws_logger_noalloc *impl = logger->p_impl;
  405. if (impl->should_close) {
  406. fclose(impl->file);
  407. }
  408. aws_mutex_clean_up(&impl->lock);
  409. aws_mem_release(logger->allocator, impl);
  410. AWS_ZERO_STRUCT(*logger);
  411. }
  412. int s_no_alloc_stderr_logger_set_log_level(struct aws_logger *logger, enum aws_log_level level) {
  413. struct aws_logger_noalloc *impl = logger->p_impl;
  414. aws_atomic_store_int(&impl->level, (size_t)level);
  415. return AWS_OP_SUCCESS;
  416. }
  417. static struct aws_logger_vtable s_noalloc_stderr_vtable = {
  418. .get_log_level = s_noalloc_stderr_logger_get_log_level,
  419. .log = s_noalloc_stderr_logger_log,
  420. .clean_up = s_noalloc_stderr_logger_clean_up,
  421. .set_log_level = s_no_alloc_stderr_logger_set_log_level,
  422. };
  423. int aws_logger_init_noalloc(
  424. struct aws_logger *logger,
  425. struct aws_allocator *allocator,
  426. struct aws_logger_standard_options *options) {
  427. struct aws_logger_noalloc *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_logger_noalloc));
  428. if (impl == NULL) {
  429. return AWS_OP_ERR;
  430. }
  431. aws_atomic_store_int(&impl->level, (size_t)options->level);
  432. if (options->file != NULL) {
  433. impl->file = options->file;
  434. impl->should_close = false;
  435. } else { /* _MSC_VER */
  436. if (options->filename != NULL) {
  437. impl->file = aws_fopen(options->filename, "w");
  438. if (!impl->file) {
  439. aws_mem_release(allocator, impl);
  440. return AWS_OP_ERR;
  441. }
  442. impl->should_close = true;
  443. } else {
  444. impl->file = stderr;
  445. impl->should_close = false;
  446. }
  447. }
  448. aws_mutex_init(&impl->lock);
  449. logger->vtable = &s_noalloc_stderr_vtable;
  450. logger->allocator = allocator;
  451. logger->p_impl = impl;
  452. return AWS_OP_SUCCESS;
  453. }
  454. int aws_logger_set_log_level(struct aws_logger *logger, enum aws_log_level level) {
  455. if (logger == NULL || logger->vtable == NULL) {
  456. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  457. }
  458. if (logger->vtable->set_log_level == NULL) {
  459. return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
  460. }
  461. return logger->vtable->set_log_level(logger, level);
  462. }