zlog.c 24.9 KB
Newer Older
Hardy Simpson's avatar
Hardy Simpson committed
1 2 3 4 5
/*
 * This file is part of the zlog Library.
 *
 * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
 *
6
 * Licensed under the LGPL v2.1, see the file COPYING in base directory.
Hardy Simpson's avatar
Hardy Simpson committed
7 8
 */

9 10
#include "fmacros.h"

Hardy Simpson's avatar
Hardy Simpson committed
11 12 13 14 15 16 17
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>

#include "conf.h"
18
#include "category_table.h"
19
#include "record_table.h"
Hardy Simpson's avatar
Hardy Simpson committed
20
#include "mdc.h"
21
#include "zc_defs.h"
22
#include "rule.h"
23
#include "version.h"
Hardy Simpson's avatar
Hardy Simpson committed
24

25 26
/*******************************************************************************/
extern char *zlog_git_sha1;
Hardy Simpson's avatar
Hardy Simpson committed
27 28
/*******************************************************************************/
static pthread_rwlock_t zlog_env_lock = PTHREAD_RWLOCK_INITIALIZER;
29
zlog_conf_t *zlog_env_conf;
30
static pthread_key_t zlog_thread_key;
31
static zc_hashtable_t *zlog_env_categories;
32
static zc_hashtable_t *zlog_env_records;
33
static zlog_category_t *zlog_default_category;
34
static size_t zlog_env_reload_conf_count;
35 36
static int zlog_env_is_init = 0;
static int zlog_env_init_version = 0;
Hardy Simpson's avatar
Hardy Simpson committed
37
/*******************************************************************************/
38
/* inner no need thread-safe */
39 40
static void zlog_fini_inner(void)
{
41 42 43 44 45 46 47
	/* pthread_key_delete(zlog_thread_key); */
	/* never use pthread_key_delete,
	 * it will cause other thread can't release zlog_thread_t 
	 * after one thread call pthread_key_delete
	 * also key not init will cause a core dump
	 */
	
48
	if (zlog_env_categories) zlog_category_table_del(zlog_env_categories);
49 50
	zlog_env_categories = NULL;
	zlog_default_category = NULL;
51
	if (zlog_env_records) zlog_record_table_del(zlog_env_records);
52
	zlog_env_records = NULL;
53 54
	if (zlog_env_conf) zlog_conf_del(zlog_env_conf);
	zlog_env_conf = NULL;
55 56 57
	return;
}

58 59 60 61 62 63 64 65 66
static void zlog_clean_rest_thread(void)
{
	zlog_thread_t *a_thread;
	a_thread = pthread_getspecific(zlog_thread_key);
	if (!a_thread) return;
	zlog_thread_del(a_thread);
	return;
}

Hardy Simpson's avatar
Hardy Simpson committed
67
static int zlog_init_inner(const char *confpath)
Hardy Simpson's avatar
Hardy Simpson committed
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	int rc = 0;

	/* the 1st time in the whole process do init */
	if (zlog_env_init_version == 0) {
		/* clean up is done by OS when a thread call pthread_exit */
		rc = pthread_key_create(&zlog_thread_key, (void (*) (void *)) zlog_thread_del);
		if (rc) {
			zc_error("pthread_key_create fail, rc[%d]", rc);
			goto err;
		}

		/* if some thread do not call pthread_exit, like main thread
		 * atexit will clean it 
		 */
		rc = atexit(zlog_clean_rest_thread);
		if (rc) {
			zc_error("atexit fail, rc[%d]", rc);
			goto err;
		}
88
		zlog_env_init_version++;
89
	} /* else maybe after zlog_fini() and need not create pthread_key */
Hardy Simpson's avatar
Hardy Simpson committed
90

Hardy Simpson's avatar
Hardy Simpson committed
91
	zlog_env_conf = zlog_conf_new(confpath);
92
	if (!zlog_env_conf) {
Hardy Simpson's avatar
Hardy Simpson committed
93
		zc_error("zlog_conf_new[%s] fail", confpath);
94
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
95 96
	}

97 98 99
	zlog_env_categories = zlog_category_table_new();
	if (!zlog_env_categories) {
		zc_error("zlog_category_table_new fail");
100
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
101 102
	}

103 104 105
	zlog_env_records = zlog_record_table_new();
	if (!zlog_env_records) {
		zc_error("zlog_record_table_new fail");
106
		goto err;
107 108
	}

109 110 111 112
	return 0;
err:
	zlog_fini_inner();
	return -1;
113 114 115
}

/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
116
int zlog_init(const char *confpath)
117
{
118
	int rc;
119
	zc_debug("------zlog_init start------");
120
	zc_debug("------compile time[%s %s], version[%s]------", __DATE__, __TIME__, ZLOG_VERSION);
121

122 123 124
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
125 126 127
		return -1;
	}

128
	if (zlog_env_is_init) {
129
		zc_error("already init, use zlog_reload pls");
130
		goto err;
131 132
	}

133

134
	if (zlog_init_inner(confpath)) {
Hardy Simpson's avatar
Hardy Simpson committed
135
		zc_error("zlog_init_inner[%s] fail", confpath);
136
		goto err;
137
	}
Hardy Simpson's avatar
Hardy Simpson committed
138

139 140
	zlog_env_is_init = 1;
	zlog_env_init_version++;
141

142 143 144 145
	zc_debug("------zlog_init success end------");
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
146 147
		return -1;
	}
148 149 150 151 152 153 154 155 156
	return 0;
err:
	zc_error("------zlog_init fail end------");
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
	}
	return -1;
157 158
}

Hardy Simpson's avatar
Hardy Simpson committed
159
int dzlog_init(const char *confpath, const char *cname)
160 161
{
	int rc = 0;
162 163 164
	zc_debug("------dzlog_init start------");
	zc_debug("------compile time[%s %s], version[%s]------",
			__DATE__, __TIME__, ZLOG_VERSION);
165

166 167 168
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
169 170 171
		return -1;
	}

172
	if (zlog_env_is_init) {
173
		zc_error("already init, use zlog_reload pls");
174
		goto err;
175 176
	}

177
	if (zlog_init_inner(confpath)) {
Hardy Simpson's avatar
Hardy Simpson committed
178
		zc_error("zlog_init_inner[%s] fail", confpath);
179
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
180 181
	}

182 183
	zlog_default_category = zlog_category_table_fetch_category(
				zlog_env_categories,
Hardy Simpson's avatar
Hardy Simpson committed
184
				cname,
185
				zlog_env_conf->rules);
186
	if (!zlog_default_category) {
187 188
		zc_error("zlog_category_table_fetch_category[%s] fail", cname);
		goto err;
189 190
	}

191 192
	zlog_env_is_init = 1;
	zlog_env_init_version++;
193

194 195 196 197
	zc_debug("------dzlog_init success end------");
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
198 199
		return -1;
	}
200 201 202 203 204 205 206 207 208
	return 0;
err:
	zc_error("------dzlog_init fail end------");
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
	}
	return -1;
Hardy Simpson's avatar
Hardy Simpson committed
209
}
210
/*******************************************************************************/
211
int zlog_reload(const char *confpath)
Hardy Simpson's avatar
Hardy Simpson committed
212 213
{
	int rc = 0;
214
	int i = 0;
215
	zlog_conf_t *new_conf = NULL;
216
	zlog_rule_t *a_rule;
217
	int c_up = 0;
Hardy Simpson's avatar
Hardy Simpson committed
218

219
	zc_debug("------zlog_reload start------");
220 221 222
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
223 224 225
		return -1;
	}

226 227
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
228
		goto quit;
Hardy Simpson's avatar
Hardy Simpson committed
229 230
	}

231 232 233 234 235 236 237 238 239 240
	/* use last conf file */
	if (confpath == NULL) confpath = zlog_env_conf->file;

	/* reach reload period */
	if (confpath == (char*)-1) {
		/* test again, avoid other threads already reloaded */
		if (zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period) {
			confpath = zlog_env_conf->file;
		} else {
			/* do nothing, already done */
241
			goto quit;
242
		}
243 244
	}

245 246 247
	/* reset counter, whether automaticlly or mannually */
	zlog_env_reload_conf_count = 0;

Hardy Simpson's avatar
Hardy Simpson committed
248
	new_conf = zlog_conf_new(confpath);
249 250
	if (!new_conf) {
		zc_error("zlog_conf_new fail");
251
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
252 253
	}

254
	zc_arraylist_foreach(new_conf->rules, i, a_rule) {
255 256 257
		zlog_rule_set_record(a_rule, zlog_env_records);
	}

258 259
	if (zlog_category_table_update_rules(zlog_env_categories, new_conf->rules)) {
		c_up = 0;
260
		zc_error("zlog_category_table_update fail");
261 262 263
		goto err;
	} else {
		c_up = 1;
Hardy Simpson's avatar
Hardy Simpson committed
264 265
	}

266
	zlog_env_init_version++;
Hardy Simpson's avatar
Hardy Simpson committed
267

268 269 270
	if (c_up) zlog_category_table_commit_rules(zlog_env_categories);
	zlog_conf_del(zlog_env_conf);
	zlog_env_conf = new_conf;
271
	zc_debug("------zlog_reload success, total init verison[%d] ------", zlog_env_init_version);
272
	rc = pthread_rwlock_unlock(&zlog_env_lock);
Hardy Simpson's avatar
Hardy Simpson committed
273
	if (rc) {
274 275
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
Hardy Simpson's avatar
Hardy Simpson committed
276
	}
277 278 279 280 281 282
	return 0;
err:
	/* fail, roll back everything */
	zc_warn("zlog_reload fail, use old conf file, still working");
	if (new_conf) zlog_conf_del(new_conf);
	if (c_up) zlog_category_table_rollback_rules(zlog_env_categories);
283
	zc_error("------zlog_reload fail, total init version[%d] ------", zlog_env_init_version);
284 285 286 287 288 289 290 291 292 293 294
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
	}
	return -1;
quit:
	zc_debug("------zlog_reload do nothing------");
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
295 296
		return -1;
	}
297
	return 0;
Hardy Simpson's avatar
Hardy Simpson committed
298
}
299
/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
300 301
void zlog_fini(void)
{
302
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
303 304

	zc_debug("------zlog_fini start------");
305 306 307
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
308 309 310
		return;
	}

311 312
	if (!zlog_env_is_init) {
		zc_error("before finish, must zlog_init() or dzlog_init() fisrt");
313
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
314 315
	}

316
	zlog_fini_inner();
317
	zlog_env_is_init = 0;
Hardy Simpson's avatar
Hardy Simpson committed
318

319
exit:
320
	zc_debug("------zlog_fini end------");
321 322 323
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
324 325 326 327 328
		return;
	}
	return;
}
/*******************************************************************************/
329
zlog_category_t *zlog_get_category(const char *cname)
Hardy Simpson's avatar
Hardy Simpson committed
330
{
331
	int rc = 0;
332
	zlog_category_t *a_category = NULL;
Hardy Simpson's avatar
Hardy Simpson committed
333

Hardy Simpson's avatar
Hardy Simpson committed
334 335
	zc_assert(cname, NULL);
	zc_debug("------zlog_get_category[%s] start------", cname);
336 337 338
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
339 340 341
		return NULL;
	}

342 343
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
344
		a_category = NULL;
345
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
346 347
	}

348 349
	a_category = zlog_category_table_fetch_category(
				zlog_env_categories,
Hardy Simpson's avatar
Hardy Simpson committed
350
				cname,
351
				zlog_env_conf->rules);
352
	if (!a_category) {
Hardy Simpson's avatar
Hardy Simpson committed
353
		zc_error("zlog_category_table_fetch_category[%s] fail", cname);
354
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
355 356
	}

357
	zc_debug("------zlog_get_category[%s] success, end------ ", cname);
358 359 360
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
361 362
		return NULL;
	}
363
	return a_category;
364 365 366 367 368 369 370 371
err:
	zc_error("------zlog_get_category[%s] fail, end------ ", cname);
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return NULL;
	}
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
372 373
}

Hardy Simpson's avatar
Hardy Simpson committed
374
int dzlog_set_category(const char *cname)
375 376
{
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
377
	zc_assert(cname, -1);
378

Hardy Simpson's avatar
Hardy Simpson committed
379
	zc_debug("------dzlog_set_category[%s] start------", cname);
380 381 382
	rc = pthread_rwlock_wrlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
383 384 385
		return -1;
	}

386 387
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
388
		goto err;
389 390
	}

391 392
	zlog_default_category = zlog_category_table_fetch_category(
				zlog_env_categories,
Hardy Simpson's avatar
Hardy Simpson committed
393
				cname,
394
				zlog_env_conf->rules);
395
	if (!zlog_default_category) {
Hardy Simpson's avatar
Hardy Simpson committed
396
		zc_error("zlog_category_table_fetch_category[%s] fail", cname);
397
		goto err;
398 399
	}

400 401 402 403
	zc_debug("------dzlog_set_category[%s] end, success------ ", cname);
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
404 405
		return -1;
	}
406 407 408 409 410 411 412 413 414
	return 0;
err:
	zc_error("------dzlog_set_category[%s] end, fail------ ", cname);
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
	}
	return -1;
415
}
416
/*******************************************************************************/
417
#define zlog_fetch_thread(a_thread, fail_goto) do {  \
418 419 420 421
	int rd = 0;  \
	a_thread = pthread_getspecific(zlog_thread_key);  \
	if (!a_thread) {  \
		a_thread = zlog_thread_new(zlog_env_init_version,  \
422 423
				zlog_env_conf->buf_size_min, zlog_env_conf->buf_size_max, \
				zlog_env_conf->time_cache_count); \
424 425 426 427 428 429 430 431 432 433 434 435 436 437
		if (!a_thread) {  \
			zc_error("zlog_thread_new fail");  \
			goto fail_goto;  \
		}  \
  \
		rd = pthread_setspecific(zlog_thread_key, a_thread);  \
		if (rd) {  \
			zlog_thread_del(a_thread);  \
			zc_error("pthread_setspecific fail, rd[%d]", rd);  \
			goto fail_goto;  \
		}  \
	}  \
  \
	if (a_thread->init_version != zlog_env_init_version) {  \
438 439 440 441 442 443 444 445 446 447
		/* as mdc is still here, so can not easily del and new */ \
		rd = zlog_thread_rebuild_msg_buf(a_thread, \
				zlog_env_conf->buf_size_min, \
				zlog_env_conf->buf_size_max);  \
		if (rd) {  \
			zc_error("zlog_thread_resize_msg_buf fail, rd[%d]", rd);  \
			goto fail_goto;  \
		}  \
  \
		rd = zlog_thread_rebuild_event(a_thread, zlog_env_conf->time_cache_count);  \
448 449 450 451
		if (rd) {  \
			zc_error("zlog_thread_resize_msg_buf fail, rd[%d]", rd);  \
			goto fail_goto;  \
		}  \
452
		a_thread->init_version = zlog_env_init_version;  \
453 454 455
	}  \
} while (0)

Hardy Simpson's avatar
Hardy Simpson committed
456
/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
457
int zlog_put_mdc(const char *key, const char *value)
Hardy Simpson's avatar
Hardy Simpson committed
458 459
{
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
460
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
461

462 463
	zc_assert(key, -1);
	zc_assert(value, -1);
Hardy Simpson's avatar
Hardy Simpson committed
464

465 466 467
	rc = pthread_rwlock_rdlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
468 469 470
		return -1;
	}

471 472
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
473
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
474 475
	}

476
	zlog_fetch_thread(a_thread, err);
Hardy Simpson's avatar
Hardy Simpson committed
477

478
	if (zlog_mdc_put(a_thread->mdc, key, value)) {
Hardy Simpson's avatar
Hardy Simpson committed
479
		zc_error("zlog_mdc_put fail, key[%s], value[%s]", key, value);
480
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
481 482
	}

483 484 485
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
486 487
		return -1;
	}
488 489 490 491 492 493 494 495
	return 0;
err:
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return -1;
	}
	return -1;
Hardy Simpson's avatar
Hardy Simpson committed
496 497 498 499
}

char *zlog_get_mdc(char *key)
{
500
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
501
	char *value = NULL;
Hardy Simpson's avatar
Hardy Simpson committed
502
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
503

504
	zc_assert(key, NULL);
Hardy Simpson's avatar
Hardy Simpson committed
505

506 507 508
	rc = pthread_rwlock_rdlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_rdlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
509 510 511
		return NULL;
	}

512 513
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
514
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
515 516
	}

517
	a_thread = pthread_getspecific(zlog_thread_key);
Hardy Simpson's avatar
Hardy Simpson committed
518
	if (!a_thread) {
519
		zc_error("thread not found, maybe not use zlog_put_mdc before");
520
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
521 522 523 524
	}

	value = zlog_mdc_get(a_thread->mdc, key);
	if (!value) {
525
		zc_error("key[%s] not found in mdc", key);
526
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
527 528
	}

529 530 531
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
532 533 534
		return NULL;
	}
	return value;
535 536 537 538 539 540 541
err:
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
		return NULL;
	}
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
542 543 544 545
}

void zlog_remove_mdc(char *key)
{
546
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
547
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
548

549
	zc_assert(key, );
Hardy Simpson's avatar
Hardy Simpson committed
550

551 552 553
	rc = pthread_rwlock_rdlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_rdlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
554 555 556
		return;
	}

557 558
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
559
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
560 561
	}

562
	a_thread = pthread_getspecific(zlog_thread_key);
Hardy Simpson's avatar
Hardy Simpson committed
563
	if (!a_thread) {
564
		zc_error("thread not found, maybe not use zlog_put_mdc before");
565
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
566 567 568 569
	}

	zlog_mdc_remove(a_thread->mdc, key);

570 571 572 573
exit:
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
574 575 576 577 578 579 580
		return;
	}
	return;
}

void zlog_clean_mdc(void)
{
581
	int rc = 0;
Hardy Simpson's avatar
Hardy Simpson committed
582
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
583

584 585 586
	rc = pthread_rwlock_rdlock(&zlog_env_lock);
	if (rc) {;
		zc_error("pthread_rwlock_rdlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
587 588 589
		return;
	}

590 591
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
592
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
593 594
	}

595
	a_thread = pthread_getspecific(zlog_thread_key);
Hardy Simpson's avatar
Hardy Simpson committed
596
	if (!a_thread) {
597
		zc_error("thread not found, maybe not use zlog_put_mdc before");
598
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
599 600 601 602
	}

	zlog_mdc_clean(a_thread->mdc);

603 604 605 606
exit:
	rc = pthread_rwlock_unlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_unlock fail, rc=[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
607 608 609 610 611 612
		return;
	}
	return;
}

/*******************************************************************************/
613 614 615
void vzlog(zlog_category_t * category,
	const char *file, size_t filelen,
	const char *func, size_t funclen,
616
	long line, int level,
617
	const char *format, va_list args)
Hardy Simpson's avatar
Hardy Simpson committed
618
{
Hardy Simpson's avatar
Hardy Simpson committed
619
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
620

Hardy Simpson's avatar
Hardy Simpson committed
621 622 623 624 625 626 627 628 629 630 631 632
	/* The bitmap determination here is not under the protection of rdlock.
	 * It may be changed by other CPU by zlog_reload() halfway.
	 *
	 * Old or strange value may be read here,
	 * but it is safe, the bitmap is valid as long as category exist,
	 * And will be the right value after zlog_reload()
	 *
	 * For speed up, if one log will not be ouput,
	 * There is no need to aquire rdlock.
	 */
	if (zlog_category_needless_level(category, level)) return;

633
	pthread_rwlock_rdlock(&zlog_env_lock);
Hardy Simpson's avatar
Hardy Simpson committed
634

635 636
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
637
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
638 639
	}

640
	zlog_fetch_thread(a_thread, exit);
Hardy Simpson's avatar
Hardy Simpson committed
641

642
	zlog_event_set_fmt(a_thread->event,
643 644 645
		category->name, category->name_len,
		file, filelen, func, funclen, line, level,
		format, args);
Hardy Simpson's avatar
Hardy Simpson committed
646

647
	if (zlog_category_output(category, a_thread)) {
Hardy Simpson's avatar
Hardy Simpson committed
648
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
649
		goto exit;
Hardy Simpson's avatar
Hardy Simpson committed
650 651
	}

652 653 654
	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
655
		goto reload;
656 657
	}

658 659 660 661
exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
662
	pthread_rwlock_unlock(&zlog_env_lock);
663
	/* will be wrlock, so after unlock */
664 665
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
666
	}
667 668
	return;
}
669

670 671 672
void hzlog(zlog_category_t *category,
	const char *file, size_t filelen,
	const char *func, size_t funclen,
673
	long line, int level,
674
	const void *buf, size_t buflen)
675 676 677
{
	zlog_thread_t *a_thread;

Hardy Simpson's avatar
Hardy Simpson committed
678 679
	if (zlog_category_needless_level(category, level)) return;

680 681
	pthread_rwlock_rdlock(&zlog_env_lock);

682 683
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
684 685 686
		goto exit;
	}

687
	zlog_fetch_thread(a_thread, exit);
688 689

	zlog_event_set_hex(a_thread->event,
690 691 692
		category->name, category->name_len,
		file, filelen, func, funclen, line, level,
		buf, buflen);
693

694
	if (zlog_category_output(category, a_thread)) {
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
		goto exit;
	}

	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
		goto reload;
	}

exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
	pthread_rwlock_unlock(&zlog_env_lock);
	/* will be wrlock, so after unlock */
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
	}
	return;
Hardy Simpson's avatar
Hardy Simpson committed
715
}
Hardy Simpson's avatar
Hardy Simpson committed
716

717 718
/*******************************************************************************/
/* for speed up, copy from vzlog */
719 720
void vdzlog(const char *file, size_t filelen,
	const char *func, size_t funclen,
721
	long line, int level,
722
	const char *format, va_list args)
723 724 725
{
	zlog_thread_t *a_thread;

Hardy Simpson's avatar
Hardy Simpson committed
726 727
	if (zlog_category_needless_level(zlog_default_category, level)) return;

728
	pthread_rwlock_rdlock(&zlog_env_lock);
729

730 731
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
732
		goto exit;
733 734
	}

Hardy Simpson's avatar
Hardy Simpson committed
735
	/* that's the differnce, must judge default_category in lock */
736 737 738
	if (!zlog_default_category) {
		zc_error("zlog_default_category is null,"
			"dzlog_init() or dzlog_set_cateogry() is not called above");
739
		goto exit;
740 741
	}

742
	zlog_fetch_thread(a_thread, exit);
743

744 745
	zlog_event_set_fmt(a_thread->event,
		zlog_default_category->name, zlog_default_category->name_len,
746 747
		file, filelen, func, funclen, line, level,
		format, args);
748

749 750 751
	if (zlog_category_output(zlog_default_category, a_thread)) {
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
		goto exit;
752 753
	}

754 755 756
	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
757
		goto reload;
758 759
	}

760 761 762 763
exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
764
	pthread_rwlock_unlock(&zlog_env_lock);
765
	/* will be wrlock, so after unlock */
766 767 768 769 770 771
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
	}
	return;
}

772 773
void hdzlog(const char *file, size_t filelen,
	const char *func, size_t funclen,
774
	long line, int level,
775
	const void *buf, size_t buflen)
776 777 778
{
	zlog_thread_t *a_thread;

Hardy Simpson's avatar
Hardy Simpson committed
779 780
	if (zlog_category_needless_level(zlog_default_category, level)) return;

781 782
	pthread_rwlock_rdlock(&zlog_env_lock);

783 784
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
785 786 787 788 789 790 791 792
		goto exit;
	}

	/* that's the differnce, must judge default_category in lock */
	if (!zlog_default_category) {
		zc_error("zlog_default_category is null,"
			"dzlog_init() or dzlog_set_cateogry() is not called above");
		goto exit;
793 794
	}

795
	zlog_fetch_thread(a_thread, exit);
796 797 798

	zlog_event_set_hex(a_thread->event,
		zlog_default_category->name, zlog_default_category->name_len,
799 800
		file, filelen, func, funclen, line, level,
		buf, buflen);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

	if (zlog_category_output(zlog_default_category, a_thread)) {
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
		goto exit;
	}

	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
		goto reload;
	}

exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
	pthread_rwlock_unlock(&zlog_env_lock);
	/* will be wrlock, so after unlock */
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
	}
	return;
823 824
}

Hardy Simpson's avatar
Hardy Simpson committed
825
/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
826 827 828 829
void zlog(zlog_category_t * category,
	const char *file, size_t filelen, const char *func, size_t funclen,
	long line, const int level,
	const char *format, ...)
Hardy Simpson's avatar
Hardy Simpson committed
830
{
831
	zlog_thread_t *a_thread;
Hardy Simpson's avatar
Hardy Simpson committed
832
	va_list args;
833

834
	if (category && zlog_category_needless_level(category, level)) return;
Hardy Simpson's avatar
Hardy Simpson committed
835

836 837
	pthread_rwlock_rdlock(&zlog_env_lock);

838 839
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
840 841 842
		goto exit;
	}

843
	zlog_fetch_thread(a_thread, exit);
844

Hardy Simpson's avatar
Hardy Simpson committed
845
	va_start(args, format);
846
	zlog_event_set_fmt(a_thread->event, category->name, category->name_len,
847 848 849 850
		file, filelen, func, funclen, line, level,
		format, args);
	if (zlog_category_output(category, a_thread)) {
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
851
		va_end(args);
852 853
		goto exit;
	}
Hardy Simpson's avatar
Hardy Simpson committed
854
	va_end(args);
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
		goto reload;
	}

exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
	pthread_rwlock_unlock(&zlog_env_lock);
	/* will be wrlock, so after unlock */
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
	}
	return;
Hardy Simpson's avatar
Hardy Simpson committed
872 873
}

874
/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
875 876
void dzlog(const char *file, size_t filelen, const char *func, size_t funclen, long line, int level,
	const char *format, ...)
877
{
878
	zlog_thread_t *a_thread;
879
	va_list args;
880

Hardy Simpson's avatar
Hardy Simpson committed
881

882 883
	pthread_rwlock_rdlock(&zlog_env_lock);

884 885
	if (!zlog_env_is_init) {
		zc_error("never call zlog_init() or dzlog_init() before");
886 887 888 889 890 891 892 893 894 895
		goto exit;
	}

	/* that's the differnce, must judge default_category in lock */
	if (!zlog_default_category) {
		zc_error("zlog_default_category is null,"
			"dzlog_init() or dzlog_set_cateogry() is not called above");
		goto exit;
	}

896 897
	if (zlog_category_needless_level(zlog_default_category, level)) goto exit;

898
	zlog_fetch_thread(a_thread, exit);
899

900
	va_start(args, format);
901 902 903 904 905 906 907
	zlog_event_set_fmt(a_thread->event,
		zlog_default_category->name, zlog_default_category->name_len,
		file, filelen, func, funclen, line, level,
		format, args);

	if (zlog_category_output(zlog_default_category, a_thread)) {
		zc_error("zlog_output fail, srcfile[%s], srcline[%ld]", file, line);
908
		va_end(args);
909 910
		goto exit;
	}
911
	va_end(args);
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928

	if (zlog_env_conf->reload_conf_period &&
		++zlog_env_reload_conf_count > zlog_env_conf->reload_conf_period ) {
		/* under the protection of lock read env conf */
		goto reload;
	}

exit:
	pthread_rwlock_unlock(&zlog_env_lock);
	return;
reload:
	pthread_rwlock_unlock(&zlog_env_lock);
	/* will be wrlock, so after unlock */
	if (zlog_reload((char *)-1)) {
		zc_error("reach reload-conf-period but zlog_reload fail, zlog-chk-conf [file] see detail");
	}
	return;
929 930
}

Hardy Simpson's avatar
Hardy Simpson committed
931 932 933
/*******************************************************************************/
void zlog_profile(void)
{
934 935 936 937
	int rc = 0;
	rc = pthread_rwlock_rdlock(&zlog_env_lock);
	if (rc) {
		zc_error("pthread_rwlock_wrlock fail, rc[%d]", rc);
Hardy Simpson's avatar
Hardy Simpson committed
938 939
		return;
	}
940
	zc_warn("------zlog_profile start------ ");
941 942
	zc_warn("is init:[%d]", zlog_env_is_init);
	zc_warn("init version:[%d]", zlog_env_init_version);
943
	zlog_conf_profile(zlog_env_conf, ZC_WARN);
944
	zlog_record_table_profile(zlog_env_records, ZC_WARN);