event.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #define _GNU_SOURCE // For distros like Centos for syscall interface
  9. #include "fmacros.h"
  10. #include <string.h>
  11. #include <stdarg.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <pthread.h>
  16. #include <unistd.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <sys/syscall.h>
  20. #include "zc_defs.h"
  21. #include "event.h"
  22. void zlog_event_profile(zlog_event_t * a_event, int flag)
  23. {
  24. zc_assert(a_event,);
  25. zc_profile(flag, "---event[%p][%s,%s][%s(%ld),%s(%ld),%ld,%d][%p,%s][%ld,%ld][%ld,%ld][%d]---",
  26. a_event,
  27. a_event->category_name, a_event->host_name,
  28. a_event->file, a_event->file_len,
  29. a_event->func, a_event->func_len,
  30. a_event->line, a_event->level,
  31. a_event->hex_buf, a_event->str_format,
  32. a_event->time_stamp.tv_sec, a_event->time_stamp.tv_usec,
  33. (long)a_event->pid, (long)a_event->tid,
  34. a_event->time_cache_count);
  35. return;
  36. }
  37. /*******************************************************************************/
  38. void zlog_event_del(zlog_event_t * a_event)
  39. {
  40. zc_assert(a_event,);
  41. if (a_event->time_caches) free(a_event->time_caches);
  42. zc_debug("zlog_event_del[%p]", a_event);
  43. free(a_event);
  44. return;
  45. }
  46. zlog_event_t *zlog_event_new(int time_cache_count)
  47. {
  48. zlog_event_t *a_event;
  49. a_event = calloc(1, sizeof(zlog_event_t));
  50. if (!a_event) {
  51. zc_error("calloc fail, errno[%d]", errno);
  52. return NULL;
  53. }
  54. a_event->time_caches = calloc(time_cache_count, sizeof(zlog_time_cache_t));
  55. if (!a_event->time_caches) {
  56. zc_error("calloc fail, errno[%d]", errno);
  57. free(a_event);
  58. return NULL;
  59. }
  60. a_event->time_cache_count = time_cache_count;
  61. /*
  62. * at the zlog_init we gethostname,
  63. * u don't always change your hostname, eh?
  64. */
  65. if (gethostname(a_event->host_name, sizeof(a_event->host_name) - 1)) {
  66. zc_error("gethostname fail, errno[%d]", errno);
  67. goto err;
  68. }
  69. a_event->host_name_len = strlen(a_event->host_name);
  70. /* tid is bound to a_event
  71. * as in whole lifecycle event persists
  72. * even fork to oth pid, tid not change
  73. */
  74. a_event->tid = pthread_self();
  75. a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid);
  76. a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "%x", (unsigned int)a_event->tid);
  77. #ifdef __linux__
  78. a_event->ktid = syscall(SYS_gettid);
  79. #elif __APPLE__
  80. uint64_t tid64;
  81. pthread_threadid_np(NULL, &tid64);
  82. a_event->tid = (pid_t)tid64;
  83. #endif
  84. #if defined __linux__ || __APPLE__
  85. a_event->ktid_str_len = sprintf(a_event->ktid_str, "%u", (unsigned int)a_event->ktid);
  86. #endif
  87. //zlog_event_profile(a_event, ZC_DEBUG);
  88. return a_event;
  89. err:
  90. zlog_event_del(a_event);
  91. return NULL;
  92. }
  93. /*******************************************************************************/
  94. void zlog_event_set_fmt(zlog_event_t * a_event,
  95. char *category_name, size_t category_name_len,
  96. const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
  97. const char *str_format, va_list str_args)
  98. {
  99. /*
  100. * category_name point to zlog_category_output's category.name
  101. */
  102. a_event->category_name = category_name;
  103. a_event->category_name_len = category_name_len;
  104. a_event->file = (char *) file;
  105. a_event->file_len = file_len;
  106. a_event->func = (char *) func;
  107. a_event->func_len = func_len;
  108. a_event->line = line;
  109. a_event->level = level;
  110. a_event->generate_cmd = ZLOG_FMT;
  111. a_event->str_format = str_format;
  112. va_copy(a_event->str_args, str_args);
  113. /* pid should fetch eveytime, as no one knows,
  114. * when does user fork his process
  115. * so clean here, and fetch at spec.c
  116. */
  117. a_event->pid = (pid_t) 0;
  118. /* in a event's life cycle, time will be get when spec need,
  119. * and keep unchange though all event's life cycle
  120. * zlog_spec_write_time gettimeofday
  121. */
  122. a_event->time_stamp.tv_sec = 0;
  123. return;
  124. }
  125. void zlog_event_set_hex(zlog_event_t * a_event,
  126. char *category_name, size_t category_name_len,
  127. const char *file, size_t file_len, const char *func, size_t func_len, long line, int level,
  128. const void *hex_buf, size_t hex_buf_len)
  129. {
  130. /*
  131. * category_name point to zlog_category_output's category.name
  132. */
  133. a_event->category_name = category_name;
  134. a_event->category_name_len = category_name_len;
  135. a_event->file = (char *) file;
  136. a_event->file_len = file_len;
  137. a_event->func = (char *) func;
  138. a_event->func_len = func_len;
  139. a_event->line = line;
  140. a_event->level = level;
  141. a_event->generate_cmd = ZLOG_HEX;
  142. a_event->hex_buf = hex_buf;
  143. a_event->hex_buf_len = hex_buf_len;
  144. /* pid should fetch eveytime, as no one knows,
  145. * when does user fork his process
  146. * so clean here, and fetch at spec.c
  147. */
  148. a_event->pid = (pid_t) 0;
  149. /* in a event's life cycle, time will be get when spec need,
  150. * and keep unchange though all event's life cycle
  151. */
  152. a_event->time_stamp.tv_sec = 0;
  153. return;
  154. }