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

9
#include "fmacros.h"
Hardy Simpson's avatar
Hardy Simpson committed
10 11 12 13 14
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
15

Hardy Simpson's avatar
Hardy Simpson committed
16
#include <pthread.h>
17 18
#include <unistd.h>
#include <sys/time.h>
Hardy Simpson's avatar
Hardy Simpson committed
19 20 21 22

#include "zc_defs.h"
#include "event.h"

23 24 25
void zlog_event_profile(zlog_event_t * a_event, int flag)
{
	zc_assert(a_event,);
26
	zc_profile(flag, "---event[%p][%s,%s][%s(%ld),%s(%ld),%ld,%d][%p,%s][%ld,%ld][%ld,%ld][%d]---",
27 28
			a_event,
			a_event->category_name, a_event->host_name,
29 30 31
			a_event->file, a_event->file_len,
			a_event->func, a_event->func_len,
			a_event->line, a_event->level,
32 33
			a_event->hex_buf, a_event->str_format,	
			a_event->time_stamp.tv_sec, a_event->time_stamp.tv_usec,
34 35
			(long)a_event->pid, (long)a_event->tid,
			a_event->time_cache_count);
36 37 38 39 40 41 42 43
	return;
}

/*******************************************************************************/

void zlog_event_del(zlog_event_t * a_event)
{
	zc_assert(a_event,);
44
	if (a_event->time_caches) free(a_event->time_caches);
45 46 47 48 49
	free(a_event);
	zc_debug("zlog_event_del[%p]", a_event);
	return;
}

50
zlog_event_t *zlog_event_new(int time_cache_count)
Hardy Simpson's avatar
Hardy Simpson committed
51
{
Hardy Simpson's avatar
Hardy Simpson committed
52
	zlog_event_t *a_event;
Hardy Simpson's avatar
Hardy Simpson committed
53

Hardy Simpson's avatar
Hardy Simpson committed
54
	a_event = calloc(1, sizeof(zlog_event_t));
Hardy Simpson's avatar
Hardy Simpson committed
55 56 57 58 59
	if (!a_event) {
		zc_error("calloc fail, errno[%d]", errno);
		return NULL;
	}

60 61 62 63 64 65 66
	a_event->time_caches = calloc(time_cache_count, sizeof(zlog_time_cache_t));
	if (!a_event->time_caches) {
		zc_error("calloc fail, errno[%d]", errno);
		return NULL;
	}
	a_event->time_cache_count = time_cache_count;

Hardy Simpson's avatar
Hardy Simpson committed
67
	/*
Hardy Simpson's avatar
Hardy Simpson committed
68
	 * at the zlog_init we gethostname,
Hardy Simpson's avatar
Hardy Simpson committed
69 70
	 * u don't always change your hostname, eh?
	 */
Hardy Simpson's avatar
Hardy Simpson committed
71 72 73
	if (gethostname(a_event->host_name, sizeof(a_event->host_name) - 1)) {
		zc_error("gethostname fail, errno[%d]", errno);
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
74 75 76 77 78 79 80 81 82 83
	}

	a_event->host_name_len = strlen(a_event->host_name);

	/* tid is bound to a_event
	 * as in whole lifecycle event persists
	 * even fork to oth pid, tid not change
	 */
	a_event->tid = pthread_self();

Hardy Simpson's avatar
Hardy Simpson committed
84 85 86
	a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid);
	a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "0x%x", (unsigned int)a_event->tid);

87
	//zlog_event_profile(a_event, ZC_DEBUG);
Hardy Simpson's avatar
Hardy Simpson committed
88 89 90 91
	return a_event;
err:
	zlog_event_del(a_event);
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
92 93
}

94
/*******************************************************************************/
95
void zlog_event_set_fmt(zlog_event_t * a_event,
Hardy Simpson's avatar
Hardy Simpson committed
96 97
			char *category_name, size_t category_name_len,
			const char *file, size_t file_len, const char *func, size_t func_len,  long line, int level,
98
			const char *str_format, va_list str_args)
Hardy Simpson's avatar
Hardy Simpson committed
99 100
{
	/*
Hardy Simpson's avatar
Hardy Simpson committed
101
	 * category_name point to zlog_category_output's category.name
Hardy Simpson's avatar
Hardy Simpson committed
102 103 104 105
	 */
	a_event->category_name = category_name;
	a_event->category_name_len = category_name_len;

Hardy Simpson's avatar
Hardy Simpson committed
106 107 108 109
	a_event->file = (char *) file;
	a_event->file_len = file_len;
	a_event->func = (char *) func;
	a_event->func_len = func_len;
Hardy Simpson's avatar
Hardy Simpson committed
110
	a_event->line = line;
Hardy Simpson's avatar
Hardy Simpson committed
111
	a_event->level = level;
Hardy Simpson's avatar
Hardy Simpson committed
112

113 114 115 116 117 118 119 120 121 122 123 124
	a_event->generate_cmd = ZLOG_FMT;
	a_event->str_format = str_format;
	va_copy(a_event->str_args, str_args);

	/* pid should fetch eveytime, as no one knows,
	 * when does user fork his process
	 * so clean here, and fetch at spec.c
	 */
	a_event->pid = (pid_t) 0;

	/* in a event's life cycle, time will be get when spec need,
	 * and keep unchange though all event's life cycle
125
	 * zlog_spec_write_time gettimeofday
126
	 */
127
	a_event->time_stamp.tv_sec = 0;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	return;
}

void zlog_event_set_hex(zlog_event_t * a_event,
			char *category_name, size_t category_name_len,
			const char *file, size_t file_len, const char *func, size_t func_len,  long line, int level,
			const void *hex_buf, size_t hex_buf_len)
{
	/*
	 * category_name point to zlog_category_output's category.name
	 */
	a_event->category_name = category_name;
	a_event->category_name_len = category_name_len;

	a_event->file = (char *) file;
	a_event->file_len = file_len;
	a_event->func = (char *) func;
	a_event->func_len = func_len;
	a_event->line = line;
	a_event->level = level;

	a_event->generate_cmd = ZLOG_HEX;
	a_event->hex_buf = hex_buf;
	a_event->hex_buf_len = hex_buf_len;
Hardy Simpson's avatar
Hardy Simpson committed
152

153 154
	/* pid should fetch eveytime, as no one knows,
	 * when does user fork his process
155
	 * so clean here, and fetch at spec.c
156
	 */
157
	a_event->pid = (pid_t) 0;
158 159

	/* in a event's life cycle, time will be get when spec need,
Hardy Simpson's avatar
Hardy Simpson committed
160
	 * and keep unchange though all event's life cycle
161
	 */
162
	a_event->time_stamp.tv_sec = 0;
Hardy Simpson's avatar
Hardy Simpson committed
163 164
	return;
}