static int zlog_spec_write_srcfunc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { if (!a_thread->event->file) { return zlog_buf_append(a_buf, "(func=null)", sizeof("(func=null)") - 1); } else { return zlog_buf_append(a_buf, a_thread->event->func, a_thread->event->func_len); } }
static int zlog_spec_write_srcfile_neat(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { char *p; if ((p = strrchr(a_thread->event->file, '/')) != NULL) { return zlog_buf_append(a_buf, p + 1, (char*)a_thread->event->file + a_thread->event->file_len - p - 1); } else { if (!a_thread->event->file) { return zlog_buf_append(a_buf, "(file=null)", sizeof("(file=null)") - 1); } else { return zlog_buf_append(a_buf, a_thread->event->file, a_thread->event->file_len); } } }
static int zlog_spec_write_time(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { if (!a_thread->event->time_stamp.tv_sec) { gettimeofday(&(a_thread->event->time_stamp), NULL); } /* a_event->time_str is a cache, as strftime is slow. * It is modified when this event meets another time specifier, or time slips one second. * So it is a weak cache, just work when a event goes through only one time specifier. * If there is any better way, please tell me! */ if ( (a_thread->event->time_stamp.tv_sec != a_thread->event->time_last) || (a_spec->time_fmt != a_thread->event->time_fmt_last) ) { a_thread->event->time_last = a_thread->event->time_stamp.tv_sec; a_thread->event->time_fmt_last = a_spec->time_fmt; localtime_r(&(a_thread->event->time_stamp.tv_sec), &(a_thread->event->time_local)); a_thread->event->time_str_len = strftime(a_thread->event->time_str, sizeof(a_thread->event->time_str), a_spec->time_fmt, &(a_thread->event->time_local)); } return zlog_buf_append(a_buf, a_thread->event->time_str, a_thread->event->time_str_len); }
static int zlog_spec_write_level_uppercase(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { zlog_level_t *a_level; a_level = zlog_level_list_get(zlog_env_conf->levels, a_thread->event->level); return zlog_buf_append(a_buf, a_level->str_uppercase, a_level->str_len); }
static int zlog_spec_write_tid_long(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { /* don't need to get tid again, as tmap_new_thread fetch it already */ /* and fork not change tid */ return zlog_buf_append(a_buf, a_thread->event->tid_str, a_thread->event->tid_str_len); }
static int zlog_spec_write_time_D(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { /* do fetch time every event once */ zlog_spec_fetch_time; return zlog_buf_append(a_buf, a_thread->event->D_time_str, sizeof(a_thread->event->D_time_str) - 1); }
static int zlog_spec_write_mdc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { zlog_mdc_kv_t *a_mdc_kv; a_mdc_kv = zlog_mdc_get_kv(a_thread->mdc, a_spec->mdc_key); if (!a_mdc_kv) { zc_error("zlog_mdc_get_kv key[%s] fail", a_spec->mdc_key); return 0; } return zlog_buf_append(a_buf, a_mdc_kv->value, a_mdc_kv->value_len); }
static int zlog_spec_write_pid(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { /* 1st in event lifecycle */ if (!a_thread->event->pid) { a_thread->event->pid = getpid(); /* compare with previous event */ if (a_thread->event->pid != a_thread->event->last_pid) { a_thread->event->last_pid = a_thread->event->pid; a_thread->event->pid_str_len = sprintf(a_thread->event->pid_str, "%u", a_thread->event->pid); } } return zlog_buf_append(a_buf, a_thread->event->pid_str, a_thread->event->pid_str_len); }
static int zlog_spec_write_time(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { /* do fetch time every event once */ zlog_spec_fetch_time; /* strftime %d() is slow too, do it when * time_fmt changed(event go through another spec) */ if (a_thread->event->last_time_fmt != a_spec->time_fmt) { /* The last_time_fmt memory can be free'd when zlog_reload deletes the formats */ /* disable this for now. */ //a_thread->event->last_time_fmt = a_spec->time_fmt; a_thread->event->time_str_len = strftime(a_thread->event->time_str, sizeof(a_thread->event->time_str), a_spec->time_fmt, &(a_thread->event->local_time)); } return zlog_buf_append(a_buf, a_thread->event->time_str, a_thread->event->time_str_len); }
static int zlog_spec_write_time_D(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { if (!a_thread->event->time_stamp.tv_sec) { gettimeofday(&(a_thread->event->time_stamp), NULL); } /* a_event->time_str_D is also a cache. * It is modified when time slips one second. * So it is a strong cache, as Default time format is always %F %T. * That's why I said %D is faster than %d() */ if (a_thread->event->time_stamp.tv_sec != a_thread->event->time_last_D) { a_thread->event->time_last_D = a_thread->event->time_stamp.tv_sec; localtime_r(&(a_thread->event->time_stamp.tv_sec), &(a_thread->event->time_local)); strftime(a_thread->event->time_str_D, sizeof(a_thread->event->time_str_D), ZLOG_DEFAULT_TIME_FMT, &(a_thread->event->time_local) ); } return zlog_buf_append(a_buf, a_thread->event->time_str_D, sizeof(a_thread->event->time_str_D) - 1); }
static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { if (a_thread->event->generate_cmd == ZLOG_FMT) { if (a_thread->event->str_format) { return zlog_buf_vprintf(a_buf, a_thread->event->str_format, a_thread->event->str_args); } else { return zlog_buf_append(a_buf, "format=(null)", sizeof("format=(null)")-1); } } else if (a_thread->event->generate_cmd == ZLOG_HEX) { int rc; long line_offset; long byte_offset; /* thread buf start == null or len <= 0 */ if (a_thread->event->hex_buf == NULL) { rc = zlog_buf_append(a_buf, "buf=(null)", sizeof("buf=(null)")-1); goto zlog_hex_exit; } rc = zlog_buf_append(a_buf, ZLOG_HEX_HEAD, sizeof(ZLOG_HEX_HEAD)-1); if (rc) { goto zlog_hex_exit; } line_offset = 0; byte_offset = 0; while (1) { unsigned char c; rc = zlog_buf_append(a_buf, "\n", 1); if (rc) goto zlog_hex_exit; rc = zlog_buf_printf_dec64(a_buf, line_offset + 1, 10); if (rc) goto zlog_hex_exit; rc = zlog_buf_append(a_buf, " ", 3); if (rc) goto zlog_hex_exit; for (byte_offset = 0; byte_offset < 16; byte_offset++) { if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) { c = *((unsigned char *)a_thread->event->hex_buf + line_offset * 16 + byte_offset); rc = zlog_buf_printf_hex(a_buf, c, 2); if (rc) goto zlog_hex_exit; rc = zlog_buf_append(a_buf, " ", 1); if (rc) goto zlog_hex_exit; } else { rc = zlog_buf_append(a_buf, " ", 3); if (rc) goto zlog_hex_exit; } } rc = zlog_buf_append(a_buf, " ", 2); if (rc) goto zlog_hex_exit; for (byte_offset = 0; byte_offset < 16; byte_offset++) { if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) { c = *((unsigned char *)a_thread->event->hex_buf + line_offset * 16 + byte_offset); if (c >= 32 && c <= 126) { rc = zlog_buf_append(a_buf,(char*)&c, 1); if (rc) goto zlog_hex_exit; } else { rc = zlog_buf_append(a_buf, ".", 1); if (rc) goto zlog_hex_exit; } } else { rc = zlog_buf_append(a_buf, " ", 1); if (rc) goto zlog_hex_exit; } } if (line_offset * 16 + byte_offset >= a_thread->event->hex_buf_len) { break; } line_offset++; } zlog_hex_exit: if (rc < 0) { zc_error("write hex msg fail"); return -1; } else if (rc > 0) { zc_error("write hex msg, buf is full"); return 1; } return 0; } return 0; }
static int zlog_spec_write_percent(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { return zlog_buf_append(a_buf, "%", 1); }
static int zlog_spec_write_hostname(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { return zlog_buf_append(a_buf, a_thread->event->host_name, a_thread->event->host_name_len); }
static int zlog_spec_write_category(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { return zlog_buf_append(a_buf, a_thread->event->category_name, a_thread->event->category_name_len); }
static int zlog_spec_write_str(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { return zlog_buf_append(a_buf, a_spec->str, a_spec->len); }
int main(int argc, char** argv) { zlog_buf_t *a_buf; char *aa; a_buf = zlog_buf_new(10, 20, "ABC"); if (!a_buf) { zc_error("zlog_buf_new fail"); return -1; } #if 0 zlog_buf_printf(a_buf, "1234567890"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "123456789012345"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "1234567890123456789"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "12345678901234567890"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "1234567890123456789012345"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "123456789"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "0"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "12345"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "6789"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "0"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "22345"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "abc"; int i,j; for (i = 0; i <= 5; i++) { for (j = 0; j <= 5; j++) { zlog_buf_restart(a_buf); zc_error("left[1],max[%d],min[%d]", i, j); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, i, j); zc_error("a_buf->start[%s]", a_buf->start); zc_error("-----"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", i, j); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, i, j); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); } } #endif aa = "1234567890"; zc_error("left[0],max[%d],min[%d]", 15, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 15, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "1234567890"; zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 25, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, 25, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 19, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 19, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 20, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 20, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_del(a_buf); return 0; }
static int zlog_spec_write_newline(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { return zlog_buf_append(a_buf, FILE_NEWLINE, FILE_NEWLINE_LEN); }