format.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include "zc_defs.h"
  15. #include "thread.h"
  16. #include "spec.h"
  17. #include "format.h"
  18. void zlog_format_profile(zlog_format_t * a_format, int flag)
  19. {
  20. zc_assert(a_format,);
  21. zc_profile(flag, "---format[%p][%s = %s(%p)]---",
  22. a_format,
  23. a_format->name,
  24. a_format->pattern,
  25. a_format->pattern_specs);
  26. #if 0
  27. int i;
  28. zlog_spec_t *a_spec;
  29. zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) {
  30. zlog_spec_profile(a_spec, flag);
  31. }
  32. #endif
  33. return;
  34. }
  35. /*******************************************************************************/
  36. void zlog_format_del(zlog_format_t * a_format)
  37. {
  38. zc_assert(a_format,);
  39. if (a_format->pattern_specs) {
  40. zc_arraylist_del(a_format->pattern_specs);
  41. }
  42. zc_debug("zlog_format_del[%p]", a_format);
  43. free(a_format);
  44. return;
  45. }
  46. zlog_format_t *zlog_format_new(char *line, int * time_cache_count)
  47. {
  48. int nscan = 0;
  49. zlog_format_t *a_format = NULL;
  50. int nread = 0;
  51. const char *p_start;
  52. const char *p_end;
  53. char *p;
  54. char *q;
  55. zlog_spec_t *a_spec;
  56. zc_assert(line, NULL);
  57. a_format = calloc(1, sizeof(zlog_format_t));
  58. if (!a_format) {
  59. zc_error("calloc fail, errno[%d]", errno);
  60. return NULL;
  61. }
  62. /* line default = "%d(%F %X.%l) %-6V (%c:%F:%L) - %m%n"
  63. * name default
  64. * pattern %d(%F %X.%l) %-6V (%c:%F:%L) - %m%n
  65. */
  66. memset(a_format->name, 0x00, sizeof(a_format->name));
  67. nread = 0;
  68. nscan = sscanf(line, " %[^= \t] = %n", a_format->name, &nread);
  69. if (nscan != 1) {
  70. zc_error("format[%s], syntax wrong", line);
  71. goto err;
  72. }
  73. if (*(line + nread) != '"') {
  74. zc_error("the 1st char of pattern is not \", line+nread[%s]", line+nread);
  75. goto err;
  76. }
  77. for (p = a_format->name; *p != '\0'; p++) {
  78. if ((!isalnum(*p)) && (*p != '_')) {
  79. zc_error("a_format->name[%s] character is not in [a-Z][0-9][_]", a_format->name);
  80. goto err;
  81. }
  82. }
  83. p_start = line + nread + 1;
  84. p_end = strrchr(p_start, '"');
  85. if (!p_end) {
  86. zc_error("there is no \" at end of pattern, line[%s]", line);
  87. goto err;
  88. }
  89. if (p_end - p_start > sizeof(a_format->pattern) - 1) {
  90. zc_error("pattern is too long");
  91. goto err;
  92. }
  93. memset(a_format->pattern, 0x00, sizeof(a_format->pattern));
  94. memcpy(a_format->pattern, p_start, p_end - p_start);
  95. if (zc_str_replace_env(a_format->pattern, sizeof(a_format->pattern))) {
  96. zc_error("zc_str_replace_env fail");
  97. goto err;
  98. }
  99. a_format->pattern_specs =
  100. zc_arraylist_new((zc_arraylist_del_fn) zlog_spec_del);
  101. if (!(a_format->pattern_specs)) {
  102. zc_error("zc_arraylist_new fail");
  103. goto err;
  104. }
  105. for (p = a_format->pattern; *p != '\0'; p = q) {
  106. a_spec = zlog_spec_new(p, &q, time_cache_count);
  107. if (!a_spec) {
  108. zc_error("zlog_spec_new fail");
  109. goto err;
  110. }
  111. if (zc_arraylist_add(a_format->pattern_specs, a_spec)) {
  112. zlog_spec_del(a_spec);
  113. zc_error("zc_arraylist_add fail");
  114. goto err;
  115. }
  116. }
  117. zlog_format_profile(a_format, ZC_DEBUG);
  118. return a_format;
  119. err:
  120. zlog_format_del(a_format);
  121. return NULL;
  122. }
  123. /*******************************************************************************/
  124. /* return 0 success, or buf is full
  125. * return -1 fail
  126. */
  127. int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread)
  128. {
  129. int i;
  130. zlog_spec_t *a_spec;
  131. zlog_buf_restart(a_thread->msg_buf);
  132. zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) {
  133. if (zlog_spec_gen_msg(a_spec, a_thread) == 0) {
  134. continue;
  135. } else {
  136. return -1;
  137. }
  138. }
  139. return 0;
  140. }