thread.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * This file is part of the zlog Library.
  3. *
  4. * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING in base directory.
  7. */
  8. #include <pthread.h>
  9. #include <errno.h>
  10. #include "zc_defs.h"
  11. #include "event.h"
  12. #include "buf.h"
  13. #include "thread.h"
  14. #include "mdc.h"
  15. void zlog_thread_profile(zlog_thread_t * a_thread, int flag)
  16. {
  17. zc_assert(a_thread,);
  18. zc_profile(flag, "--thread[%p][%p][%p][%p,%p,%p,%p,%p]--",
  19. a_thread,
  20. a_thread->mdc,
  21. a_thread->event,
  22. a_thread->pre_path_buf,
  23. a_thread->path_buf,
  24. a_thread->archive_path_buf,
  25. a_thread->pre_msg_buf,
  26. a_thread->msg_buf);
  27. zlog_mdc_profile(a_thread->mdc, flag);
  28. zlog_event_profile(a_thread->event, flag);
  29. zlog_buf_profile(a_thread->pre_path_buf, flag);
  30. zlog_buf_profile(a_thread->path_buf, flag);
  31. zlog_buf_profile(a_thread->archive_path_buf, flag);
  32. zlog_buf_profile(a_thread->pre_msg_buf, flag);
  33. zlog_buf_profile(a_thread->msg_buf, flag);
  34. return;
  35. }
  36. /*******************************************************************************/
  37. void zlog_thread_del(zlog_thread_t * a_thread)
  38. {
  39. zc_assert(a_thread,);
  40. if (a_thread->mdc)
  41. zlog_mdc_del(a_thread->mdc);
  42. if (a_thread->event)
  43. zlog_event_del(a_thread->event);
  44. if (a_thread->pre_path_buf)
  45. zlog_buf_del(a_thread->pre_path_buf);
  46. if (a_thread->path_buf)
  47. zlog_buf_del(a_thread->path_buf);
  48. if (a_thread->archive_path_buf)
  49. zlog_buf_del(a_thread->archive_path_buf);
  50. if (a_thread->pre_msg_buf)
  51. zlog_buf_del(a_thread->pre_msg_buf);
  52. if (a_thread->msg_buf)
  53. zlog_buf_del(a_thread->msg_buf);
  54. zc_debug("zlog_thread_del[%p]", a_thread);
  55. free(a_thread);
  56. return;
  57. }
  58. zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max, int time_cache_count)
  59. {
  60. zlog_thread_t *a_thread;
  61. a_thread = calloc(1, sizeof(zlog_thread_t));
  62. if (!a_thread) {
  63. zc_error("calloc fail, errno[%d]", errno);
  64. return NULL;
  65. }
  66. a_thread->init_version = init_version;
  67. a_thread->mdc = zlog_mdc_new();
  68. if (!a_thread->mdc) {
  69. zc_error("zlog_mdc_new fail");
  70. goto err;
  71. }
  72. a_thread->event = zlog_event_new(time_cache_count);
  73. if (!a_thread->event) {
  74. zc_error("zlog_event_new fail");
  75. goto err;
  76. }
  77. a_thread->pre_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
  78. if (!a_thread->pre_path_buf) {
  79. zc_error("zlog_buf_new fail");
  80. goto err;
  81. }
  82. a_thread->path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
  83. if (!a_thread->path_buf) {
  84. zc_error("zlog_buf_new fail");
  85. goto err;
  86. }
  87. a_thread->archive_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
  88. if (!a_thread->archive_path_buf) {
  89. zc_error("zlog_buf_new fail");
  90. goto err;
  91. }
  92. a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
  93. if (!a_thread->pre_msg_buf) {
  94. zc_error("zlog_buf_new fail");
  95. goto err;
  96. }
  97. a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
  98. if (!a_thread->msg_buf) {
  99. zc_error("zlog_buf_new fail");
  100. goto err;
  101. }
  102. //zlog_thread_profile(a_thread, ZC_DEBUG);
  103. return a_thread;
  104. err:
  105. zlog_thread_del(a_thread);
  106. return NULL;
  107. }
  108. /*******************************************************************************/
  109. int zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max)
  110. {
  111. zlog_buf_t *pre_msg_buf_new = NULL;
  112. zlog_buf_t *msg_buf_new = NULL;
  113. zc_assert(a_thread, -1);
  114. if ( (a_thread->msg_buf->size_min == buf_size_min)
  115. && (a_thread->msg_buf->size_max == buf_size_max)) {
  116. zc_debug("buf size not changed, no need rebuild");
  117. return 0;
  118. }
  119. pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
  120. if (!pre_msg_buf_new) {
  121. zc_error("zlog_buf_new fail");
  122. goto err;
  123. }
  124. msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
  125. if (!msg_buf_new) {
  126. zc_error("zlog_buf_new fail");
  127. goto err;
  128. }
  129. zlog_buf_del(a_thread->pre_msg_buf);
  130. a_thread->pre_msg_buf = pre_msg_buf_new;
  131. zlog_buf_del(a_thread->msg_buf);
  132. a_thread->msg_buf = msg_buf_new;
  133. return 0;
  134. err:
  135. if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new);
  136. if (msg_buf_new) zlog_buf_del(msg_buf_new);
  137. return -1;
  138. }
  139. int zlog_thread_rebuild_event(zlog_thread_t * a_thread, int time_cache_count)
  140. {
  141. zlog_event_t *event_new = NULL;
  142. zc_assert(a_thread, -1);
  143. event_new = zlog_event_new(time_cache_count);
  144. if (!event_new) {
  145. zc_error("zlog_event_new fail");
  146. goto err;
  147. }
  148. zlog_event_del(a_thread->event);
  149. a_thread->event = event_new;
  150. return 0;
  151. err:
  152. if (event_new) zlog_event_del(event_new);
  153. return -1;
  154. }
  155. /*******************************************************************************/