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
9 #include <pthread.h>
10 #include <errno.h>
11
12 #include "zc_defs.h"
13 #include "event.h"
14 #include "buf.h"
15 #include "thread.h"
16 #include "mdc.h"
17
zlog_thread_profile(zlog_thread_t * a_thread,int flag)18 void zlog_thread_profile(zlog_thread_t * a_thread, int flag)
19 {
20 zc_assert(a_thread,);
21 zc_profile(flag, "--thread[%p][%p][%p][%p,%p,%p,%p,%p]--",
22 a_thread,
23 a_thread->mdc,
24 a_thread->event,
25 a_thread->pre_path_buf,
26 a_thread->path_buf,
27 a_thread->archive_path_buf,
28 a_thread->pre_msg_buf,
29 a_thread->msg_buf);
30
31 zlog_mdc_profile(a_thread->mdc, flag);
32 zlog_event_profile(a_thread->event, flag);
33 zlog_buf_profile(a_thread->pre_path_buf, flag);
34 zlog_buf_profile(a_thread->path_buf, flag);
35 zlog_buf_profile(a_thread->archive_path_buf, flag);
36 zlog_buf_profile(a_thread->pre_msg_buf, flag);
37 zlog_buf_profile(a_thread->msg_buf, flag);
38 return;
39 }
40 /*******************************************************************************/
zlog_thread_del(zlog_thread_t * a_thread)41 void zlog_thread_del(zlog_thread_t * a_thread)
42 {
43 zc_assert(a_thread,);
44 if (a_thread->mdc)
45 zlog_mdc_del(a_thread->mdc);
46 if (a_thread->event)
47 zlog_event_del(a_thread->event);
48 if (a_thread->pre_path_buf)
49 zlog_buf_del(a_thread->pre_path_buf);
50 if (a_thread->path_buf)
51 zlog_buf_del(a_thread->path_buf);
52 if (a_thread->archive_path_buf)
53 zlog_buf_del(a_thread->archive_path_buf);
54 if (a_thread->pre_msg_buf)
55 zlog_buf_del(a_thread->pre_msg_buf);
56 if (a_thread->msg_buf)
57 zlog_buf_del(a_thread->msg_buf);
58
59 free(a_thread);
60 zc_debug("zlog_thread_del[%p]", a_thread);
61 return;
62 }
63
zlog_thread_new(int init_version,size_t buf_size_min,size_t buf_size_max,int time_cache_count)64 zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max, int time_cache_count)
65 {
66 zlog_thread_t *a_thread;
67
68 a_thread = calloc(1, sizeof(zlog_thread_t));
69 if (!a_thread) {
70 zc_error("calloc fail, errno[%d]", errno);
71 return NULL;
72 }
73
74 a_thread->init_version = init_version;
75
76 a_thread->mdc = zlog_mdc_new();
77 if (!a_thread->mdc) {
78 zc_error("zlog_mdc_new fail");
79 goto err;
80 }
81
82 a_thread->event = zlog_event_new(time_cache_count);
83 if (!a_thread->event) {
84 zc_error("zlog_event_new fail");
85 goto err;
86 }
87
88 a_thread->pre_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
89 if (!a_thread->pre_path_buf) {
90 zc_error("zlog_buf_new fail");
91 goto err;
92 }
93
94 a_thread->path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
95 if (!a_thread->path_buf) {
96 zc_error("zlog_buf_new fail");
97 goto err;
98 }
99
100 a_thread->archive_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL);
101 if (!a_thread->archive_path_buf) {
102 zc_error("zlog_buf_new fail");
103 goto err;
104 }
105
106 a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
107 if (!a_thread->pre_msg_buf) {
108 zc_error("zlog_buf_new fail");
109 goto err;
110 }
111
112 a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
113 if (!a_thread->msg_buf) {
114 zc_error("zlog_buf_new fail");
115 goto err;
116 }
117
118
119 //zlog_thread_profile(a_thread, ZC_DEBUG);
120 return a_thread;
121 err:
122 zlog_thread_del(a_thread);
123 return NULL;
124 }
125
126 /*******************************************************************************/
zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread,size_t buf_size_min,size_t buf_size_max)127 int zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max)
128 {
129 zlog_buf_t *pre_msg_buf_new = NULL;
130 zlog_buf_t *msg_buf_new = NULL;
131 zc_assert(a_thread, -1);
132
133 if ( (a_thread->msg_buf->size_min == buf_size_min)
134 && (a_thread->msg_buf->size_max == buf_size_max)) {
135 zc_debug("buf size not changed, no need rebuild");
136 return 0;
137 }
138
139 pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
140 if (!pre_msg_buf_new) {
141 zc_error("zlog_buf_new fail");
142 goto err;
143 }
144
145 msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
146 if (!msg_buf_new) {
147 zc_error("zlog_buf_new fail");
148 goto err;
149 }
150
151 zlog_buf_del(a_thread->pre_msg_buf);
152 a_thread->pre_msg_buf = pre_msg_buf_new;
153
154 zlog_buf_del(a_thread->msg_buf);
155 a_thread->msg_buf = msg_buf_new;
156
157 return 0;
158 err:
159 if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new);
160 if (msg_buf_new) zlog_buf_del(msg_buf_new);
161 return -1;
162 }
163
zlog_thread_rebuild_event(zlog_thread_t * a_thread,int time_cache_count)164 int zlog_thread_rebuild_event(zlog_thread_t * a_thread, int time_cache_count)
165 {
166 zlog_event_t *event_new = NULL;
167 zc_assert(a_thread, -1);
168
169 event_new = zlog_event_new(time_cache_count);
170 if (!event_new) {
171 zc_error("zlog_event_new fail");
172 goto err;
173 }
174
175 zlog_event_del(a_thread->event);
176 a_thread->event = event_new;
177 return 0;
178 err:
179 if (event_new) zlog_event_del(event_new);
180 return -1;
181 }
182
183
184 /*******************************************************************************/
185