log_single.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "precompile.h"
  2. #include "log_single.h"
  3. #include "log_base.h"
  4. #include "log.h"
  5. #include "spinlock.h"
  6. #include "fileutil.h"
  7. #include "strutil.h"
  8. #include <winpr/string.h>
  9. typedef struct singlelogfactory_t singlelogfactory_t;
  10. typedef struct singlelog_t {
  11. logbase_t base;
  12. char *file_path;
  13. FILE *fp;
  14. LONG use_lock;
  15. lock_t lock;
  16. LONG flush;
  17. int first_record;
  18. }singlelog_t;
  19. struct singlelogfactory_t {
  20. logfactory_t base;
  21. };
  22. static void singlefilefactory_destroy(void *self)
  23. {
  24. singlelogfactory_t *fac = (singlelogfactory_t *)self;
  25. if (fac) {
  26. if (fac->base.name)
  27. free(fac->base.name);
  28. free(fac);
  29. }
  30. }
  31. static void* singlefilefactory_create_log(void *self, const char* inst)
  32. {
  33. singlelog_t *log = MALLOC_T(singlelog_t);
  34. log->base.level = XLOG_LEVEL_ALL;
  35. log->base.factory = self;
  36. log->base.inst_name = _strdup(inst);
  37. log->file_path = 0;
  38. log->fp = NULL;
  39. log->use_lock = 0;
  40. log->flush = 1;
  41. log->first_record = 0;
  42. fastlock_init(log->lock);
  43. return log;
  44. }
  45. static int singlefilefactory_set_log_param(void *self,
  46. void *log,
  47. const char *key,
  48. const char *value)
  49. {
  50. singlelog_t *slog = (singlelog_t*)log;
  51. if (_stricmp(key, "file") == 0) {
  52. char *p;
  53. if (strchr(value, ':') == NULL) {
  54. char tmp[MAX_PATH];
  55. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  56. p = strrchr(tmp, '\\');
  57. *(p+1) = 0;
  58. slog->file_path = strdup_printf("%s%s", tmp, value);
  59. } else {
  60. slog->file_path = _strdup(value);
  61. }
  62. p = strrchr(slog->file_path, '\\');
  63. if (p) {
  64. *p = 0;
  65. CreateDirRecursiveA(slog->file_path);
  66. *p = '\\';
  67. }
  68. } else if (_stricmp(key, "use_lock") == 0) {
  69. if (_stricmp(value, "true") == 0 || _stricmp(value, "1") == 0) {
  70. slog->use_lock = 1;
  71. }
  72. } else if (_stricmp(key, "flush") == 0) {
  73. if (_stricmp(value, "true") == 0 || _stricmp(value, "1") == 0) {
  74. slog->flush = 1;
  75. }
  76. } else if (_stricmp(key, "level") == 0) {
  77. if (_stricmp(value, "all") == 0) {
  78. slog->base.level = 0;
  79. } else if (_stricmp(value, "trace") == 0) {
  80. slog->base.level = 1;
  81. } else if (_stricmp(value, "debug") == 0) {
  82. slog->base.level = 2;
  83. } else if (_stricmp(value, "info") == 0) {
  84. slog->base.level = 3;
  85. } else if (_stricmp(value, "warn") == 0) {
  86. slog->base.level = 4;
  87. } else if (_stricmp(value, "error") == 0) {
  88. slog->base.level = 5;
  89. } else if (_stricmp(value, "fatal") == 0) {
  90. slog->base.level = 6;
  91. } else if (_stricmp(value, "none") == 0) {
  92. slog->base.level = 7;
  93. } else {
  94. return -1;
  95. }
  96. } else {
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. static int singlefilefactory_init_log(void *self, void *log)
  102. {
  103. singlelog_t *slog = (singlelog_t*)log;
  104. slog->fp = fopen(slog->file_path, "ab");
  105. if (slog->fp) {
  106. slog->first_record = !!ftell(slog->fp);
  107. }
  108. return slog->fp ? 0 : -1;
  109. }
  110. static int singlefilefactory_term_log(void *self, void *log)
  111. {
  112. singlelog_t *slog = (singlelog_t*)log;
  113. if (slog->fp) {
  114. fclose(slog->fp);
  115. slog->fp = NULL;
  116. }
  117. slog->use_lock = 0;
  118. if (slog->file_path) {
  119. free(slog->file_path);
  120. slog->file_path = NULL;
  121. }
  122. return 0;
  123. }
  124. static void singlefilefactory_destroy_log(void *self, void *log)
  125. {
  126. singlelog_t *slog = (singlelog_t*)log;
  127. if (slog) {
  128. if (slog->base.inst_name)
  129. free(slog->base.inst_name);
  130. free(slog);
  131. }
  132. }
  133. static int singlefilefactory_record_log(void *self,
  134. void *log,
  135. int level,
  136. unsigned long ts_low,
  137. unsigned long ts_high,
  138. const char *s,
  139. int sn)
  140. {
  141. singlelog_t *slog = (singlelog_t*)log;
  142. if (slog->fp) {
  143. SYSTEMTIME st;
  144. FILETIME utc_ft, local_ft;
  145. char tmp[64];
  146. int n;
  147. if (slog->use_lock) {
  148. fastlock_enter(slog->lock);
  149. }
  150. #ifdef _WIN32
  151. utc_ft.dwLowDateTime = (DWORD)ts_low;
  152. utc_ft.dwHighDateTime = (DWORD)ts_high;
  153. FileTimeToLocalFileTime(&utc_ft, &local_ft);
  154. FileTimeToSystemTime(&local_ft, &st);
  155. #else
  156. GetLocalTime(&st);
  157. #endif // _WIN32
  158. if (slog->first_record) {
  159. n = sprintf(tmp, "[%02d:%02d:%02d.%03d][%s] ",
  160. st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
  161. slog->first_record = 0;
  162. } else {
  163. n = sprintf(tmp, "\r\n[%02d:%02d:%02d.%03d][%s] ",
  164. st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, log_level2str(level));
  165. }
  166. _fwrite_nolock(tmp, n, 1, slog->fp);
  167. _fwrite_nolock(s, sn, 1, slog->fp);
  168. if (slog->flush)
  169. _fflush_nolock(slog->fp);
  170. }
  171. if (slog->use_lock) {
  172. fastlock_leave(slog->lock);
  173. }
  174. return 0;
  175. }
  176. int singlefilefactory_create(logfactory_t **p_fac)
  177. {
  178. singlelogfactory_t *fac;
  179. if (!p_fac)
  180. return -1;
  181. fac = MALLOC_T(singlelogfactory_t);
  182. if (!fac) {
  183. return -1;
  184. }
  185. fac->base.name = _strdup("single");
  186. fac->base.record_log = &singlefilefactory_record_log;
  187. fac->base.init_log = &singlefilefactory_init_log;
  188. fac->base.set_log_param = &singlefilefactory_set_log_param;
  189. fac->base.term_log = &singlefilefactory_term_log;
  190. fac->base.create_log = &singlefilefactory_create_log;
  191. fac->base.destroy_log = &singlefilefactory_destroy_log;
  192. fac->base.destroy = &singlefilefactory_destroy;
  193. *p_fac = &fac->base;
  194. return 0;
  195. }