buf.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #ifndef __zlog_buf_h
  9. #define __zlog_buf_h
  10. /* buf, is a dynamic expand buffer for one single log,
  11. * as one single log will interlace if use multiple write() to file.
  12. * and buf is always keep in a thread, to make each thread has its
  13. * own buffer to avoid lock.
  14. */
  15. #include <stdarg.h>
  16. #include <stdint.h>
  17. typedef struct zlog_buf_s {
  18. char *start;
  19. char *tail;
  20. char *end;
  21. char *end_plus_1;
  22. size_t size_min;
  23. size_t size_max;
  24. size_t size_real;
  25. char truncate_str[MAXLEN_PATH + 1];
  26. size_t truncate_str_len;
  27. } zlog_buf_t;
  28. zlog_buf_t *zlog_buf_new(size_t min, size_t max, const char *truncate_str);
  29. void zlog_buf_del(zlog_buf_t * a_buf);
  30. void zlog_buf_profile(zlog_buf_t * a_buf, int flag);
  31. int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args);
  32. int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len);
  33. int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
  34. int left_adjust, int zero_pad, size_t in_width, size_t out_width);
  35. int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width);
  36. int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width);
  37. int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width);
  38. #define zlog_buf_restart(a_buf) do { \
  39. a_buf->tail = a_buf->start; \
  40. } while(0)
  41. #define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start)
  42. #define zlog_buf_str(a_buf) (a_buf->start)
  43. #define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0)
  44. #endif