buf.h 1.62 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
 */

Hardy Simpson's avatar
Hardy Simpson committed
9 10
#ifndef __zlog_buf_h
#define __zlog_buf_h
Hardy Simpson's avatar
Hardy Simpson committed
11

Hardy Simpson's avatar
Hardy Simpson committed
12 13 14 15 16 17
/* buf, is a dynamic expand buffer for one single log,
 * as one single log will interlace if use multiple write() to file.
 * and buf is always keep in a thread, to make each thread has its
 * own buffer to avoid lock.
 */

Hardy Simpson's avatar
Hardy Simpson committed
18
#include <stdarg.h>
19
#include <stdint.h>
Hardy Simpson's avatar
Hardy Simpson committed
20

21
typedef struct zlog_buf_s {
22 23 24 25 26
	char *start;
	char *tail;
	char *end;
	char *end_plus_1;

27 28
	size_t size_min;
	size_t size_max;
29 30
	size_t size_real;

31 32
	char truncate_str[MAXLEN_PATH + 1];
	size_t truncate_str_len;
Hardy Simpson's avatar
Hardy Simpson committed
33
} zlog_buf_t;
Hardy Simpson's avatar
Hardy Simpson committed
34

Hardy Simpson's avatar
Hardy Simpson committed
35 36

zlog_buf_t *zlog_buf_new(size_t min, size_t max, const char *truncate_str);
Hardy Simpson's avatar
Hardy Simpson committed
37
void zlog_buf_del(zlog_buf_t * a_buf);
38 39
void zlog_buf_profile(zlog_buf_t * a_buf, int flag);

Hardy Simpson's avatar
Hardy Simpson committed
40 41 42 43 44 45 46 47
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args);
int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len);
int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
			int left_adjust, size_t in_width, size_t out_width);
int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width);
int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width);
int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width);

48
#define zlog_buf_restart(a_buf) do { \
49
	a_buf->tail = a_buf->start; \
50
} while(0)
51

52 53
#define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start)
#define zlog_buf_str(a_buf) (a_buf->start)
54
#define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0)
55

Hardy Simpson's avatar
Hardy Simpson committed
56
#endif