tlog.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * tinylog
  3. * Copyright (C) 2018-2024 Nick Peng <[email protected]>
  4. * https://github.com/pymumu/tinylog
  5. */
  6. #ifndef _GNU_SOURCE
  7. #define _GNU_SOURCE
  8. #endif
  9. #include "tlog.h"
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <libgen.h>
  14. #include <limits.h>
  15. #include <pthread.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/resource.h>
  21. #include <sys/stat.h>
  22. #include <sys/syscall.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <unistd.h>
  27. #ifndef likely
  28. #define likely(x) __builtin_expect(!!(x), 1)
  29. #endif
  30. #ifndef unlikely
  31. #define unlikely(x) __builtin_expect(!!(x), 0)
  32. #endif
  33. #define TLOG_BUFF_SIZE (1024 * 128)
  34. #define TLOG_TMP_LEN 128
  35. #define TLOG_LOG_SIZE (1024 * 1024 * 50)
  36. #define TLOG_LOG_COUNT 32
  37. #define TLOG_LOG_NAME_LEN 256
  38. #define TLOG_BUFF_LEN (PATH_MAX + TLOG_LOG_NAME_LEN * 3)
  39. #define TLOG_SUFFIX_GZ ".gz"
  40. #define TLOG_SUFFIX_LOG ""
  41. #define TLOG_MAX_LINE_SIZE_SET (1024 * 8)
  42. #define TLOG_MIN_LINE_SIZE_SET (128)
  43. #define TLOG_SEGMENT_MAGIC 0xFF446154
  44. struct linux_dirent64 {
  45. unsigned long long d_ino;
  46. long long d_off;
  47. unsigned short d_reclen;
  48. unsigned char d_type;
  49. char d_name[256];
  50. };
  51. struct tlog_log {
  52. char *buff;
  53. int buffsize;
  54. int start;
  55. int end;
  56. int ext_end;
  57. int fd;
  58. int fd_lock;
  59. off_t filesize;
  60. char logdir[PATH_MAX];
  61. char logname[TLOG_LOG_NAME_LEN];
  62. char suffix[TLOG_LOG_NAME_LEN];
  63. char pending_logfile[PATH_MAX];
  64. int rename_pending;
  65. int fail;
  66. int logsize;
  67. int logcount;
  68. int block;
  69. int dropped;
  70. int nocompress;
  71. int zip_pid;
  72. int multi_log;
  73. int logscreen;
  74. int logscreen_color;
  75. int segment_log;
  76. int max_line_size;
  77. int print_errmsg;
  78. tlog_output_func output_func;
  79. void *private_data;
  80. time_t last_try;
  81. time_t last_waitpid;
  82. mode_t file_perm;
  83. mode_t archive_perm;
  84. int mode_changed;
  85. int waiters;
  86. int is_exit;
  87. struct tlog_log *next;
  88. pthread_mutex_t lock;
  89. pthread_cond_t client_cond;
  90. };
  91. struct tlog {
  92. struct tlog_log *root;
  93. struct tlog_log *log;
  94. struct tlog_log *notify_log;
  95. int run;
  96. pthread_t tid;
  97. pthread_mutex_t lock;
  98. pthread_cond_t cond;
  99. tlog_log_output_func output_func;
  100. struct tlog_log *wait_on_log;
  101. int is_wait;
  102. int output_no_prefix;
  103. char gzip_cmd[PATH_MAX];
  104. tlog_format_func root_format;
  105. tlog_early_print_func tlog_early_print;
  106. tlog_log_output_func early_print_output;
  107. int early_print_disable;
  108. int early_print_with_screen;
  109. int early_print_no_prefix;
  110. int early_print_color;
  111. void *early_print_userptr;
  112. };
  113. struct tlog_segment_log_head {
  114. struct tlog_loginfo info;
  115. unsigned short len;
  116. char data[0];
  117. } __attribute__((packed));
  118. struct tlog_segment_head {
  119. unsigned int magic;
  120. unsigned short len;
  121. char data[0];
  122. } __attribute__((packed));
  123. struct oldest_log {
  124. char name[TLOG_LOG_NAME_LEN];
  125. time_t mtime;
  126. struct tlog_log *log;
  127. };
  128. struct count_log {
  129. int lognum;
  130. struct tlog_log *log;
  131. };
  132. struct tlog_info_inter {
  133. struct tlog_loginfo info;
  134. void *userptr;
  135. };
  136. typedef int (*list_callback)(const char *name, struct dirent *entry, void *user);
  137. typedef int (*vprint_callback)(char *buff, int maxlen, void *userptr, const char *format, va_list ap);
  138. static struct tlog tlog;
  139. static tlog_level tlog_set_level = TLOG_INFO;
  140. unsigned int tlog_localtime_lock;
  141. static const char *tlog_level_str[] = {
  142. "DEBUG",
  143. "INFO",
  144. "NOTICE",
  145. "WARN",
  146. "ERROR",
  147. "FATAL",
  148. };
  149. static inline void _tlog_spin_lock(unsigned int *lock)
  150. {
  151. while (1) {
  152. int i;
  153. for (i = 0; i < 10000; i++) {
  154. if (__sync_bool_compare_and_swap(lock, 0, 1)) {
  155. return;
  156. }
  157. }
  158. sched_yield();
  159. }
  160. }
  161. static inline void _tlog_spin_unlock(unsigned int *lock)
  162. {
  163. __sync_bool_compare_and_swap(lock, 1, 0);
  164. }
  165. static int _tlog_mkdir(const char *path)
  166. {
  167. char path_c[PATH_MAX];
  168. char *path_end;
  169. char str;
  170. int len;
  171. if (access(path, F_OK) == 0) {
  172. return 0;
  173. }
  174. while (*path == ' ') {
  175. path++;
  176. }
  177. strncpy(path_c, path, sizeof(path_c) - 1);
  178. path_c[sizeof(path_c) - 1] = '\0';
  179. len = strnlen(path_c, sizeof(path_c) - 1);
  180. path_c[len] = '/';
  181. path_c[len + 1] = '\0';
  182. path_end = path_c;
  183. /* create directory recursively */
  184. while (*path_end != 0) {
  185. if (*path_end != '/') {
  186. path_end++;
  187. continue;
  188. }
  189. if (path_end == path_c) {
  190. path_end++;
  191. continue;
  192. }
  193. str = *path_end;
  194. *path_end = '\0';
  195. if (access(path_c, F_OK) == 0) {
  196. *path_end = str;
  197. path_end++;
  198. continue;
  199. }
  200. if (mkdir(path_c, 0750) != 0) {
  201. return -1;
  202. }
  203. *path_end = str;
  204. path_end++;
  205. }
  206. return 0;
  207. }
  208. static struct tm *_tlog_localtime(time_t *timep, struct tm *tm)
  209. {
  210. static time_t last_time;
  211. static struct tm last_tm;
  212. /* localtime_r has a global timezone lock, it's about 8 times slower than gmtime
  213. * this code is used to speed up localtime_r call.
  214. */
  215. _tlog_spin_lock(&tlog_localtime_lock);
  216. if (*timep == last_time) {
  217. *tm = last_tm;
  218. } else {
  219. _tlog_spin_unlock(&tlog_localtime_lock);
  220. tm = localtime_r(timep, tm);
  221. _tlog_spin_lock(&tlog_localtime_lock);
  222. if (tm) {
  223. last_time = *timep;
  224. last_tm = *tm;
  225. }
  226. }
  227. _tlog_spin_unlock(&tlog_localtime_lock);
  228. return tm;
  229. }
  230. static int _tlog_getmtime(struct tlog_time *log_mtime, const char *file)
  231. {
  232. struct tm tm;
  233. struct stat sb;
  234. if (stat(file, &sb) != 0) {
  235. return -1;
  236. }
  237. if (_tlog_localtime(&sb.st_mtime, &tm) == NULL) {
  238. return -1;
  239. }
  240. log_mtime->year = tm.tm_year + 1900;
  241. log_mtime->mon = tm.tm_mon + 1;
  242. log_mtime->mday = tm.tm_mday;
  243. log_mtime->hour = tm.tm_hour;
  244. log_mtime->min = tm.tm_min;
  245. log_mtime->sec = tm.tm_sec;
  246. log_mtime->usec = 0;
  247. return 0;
  248. }
  249. static int _tlog_gettime(struct tlog_time *cur_time)
  250. {
  251. struct tm tm;
  252. struct timeval tmval;
  253. if (gettimeofday(&tmval, NULL) != 0) {
  254. return -1;
  255. }
  256. if (_tlog_localtime(&tmval.tv_sec, &tm) == NULL) {
  257. return -1;
  258. }
  259. cur_time->year = tm.tm_year + 1900;
  260. cur_time->mon = tm.tm_mon + 1;
  261. cur_time->mday = tm.tm_mday;
  262. cur_time->hour = tm.tm_hour;
  263. cur_time->min = tm.tm_min;
  264. cur_time->sec = tm.tm_sec;
  265. cur_time->usec = tmval.tv_usec;
  266. return 0;
  267. }
  268. void tlog_set_maxline_size(struct tlog_log *log, int size)
  269. {
  270. if (log == NULL) {
  271. return;
  272. }
  273. if (size < TLOG_MIN_LINE_SIZE_SET) {
  274. size = TLOG_MIN_LINE_SIZE_SET;
  275. } else if (size > TLOG_MAX_LINE_SIZE_SET) {
  276. size = TLOG_MAX_LINE_SIZE_SET;
  277. }
  278. log->max_line_size = size;
  279. }
  280. void tlog_logcount(struct tlog_log *log, int count)
  281. {
  282. if (log == NULL) {
  283. return;
  284. }
  285. if (count < 0) {
  286. count = 0;
  287. }
  288. log->logcount = count;
  289. }
  290. void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive)
  291. {
  292. log->file_perm = file;
  293. log->archive_perm = archive;
  294. log->mode_changed = 1;
  295. }
  296. int tlog_localtime(struct tlog_time *tm)
  297. {
  298. return _tlog_gettime(tm);
  299. }
  300. tlog_log *tlog_get_root(void)
  301. {
  302. return tlog.root;
  303. }
  304. void tlog_set_private(tlog_log *log, void *private_data)
  305. {
  306. if (log == NULL) {
  307. return;
  308. }
  309. log->private_data = private_data;
  310. }
  311. void *tlog_get_private(tlog_log *log)
  312. {
  313. if (log == NULL) {
  314. return NULL;
  315. }
  316. return log->private_data;
  317. }
  318. static int _tlog_root_default_format(char *buff, int maxlen, struct tlog_loginfo *info, void *userptr, const char *format, va_list ap)
  319. {
  320. int len = 0;
  321. int total_len = 0;
  322. struct tlog_time *tm = &info->time;
  323. void *unused __attribute__((unused));
  324. unused = userptr;
  325. if (tlog.output_no_prefix == 0) {
  326. if (tlog.root->multi_log) {
  327. /* format prefix */
  328. len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5d][%4s][%17s:%-4d] ",
  329. tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000, getpid(),
  330. tlog_get_level_string(info->level), info->file, info->line);
  331. } else {
  332. /* format prefix */
  333. len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
  334. tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000,
  335. tlog_get_level_string(info->level), info->file, info->line);
  336. }
  337. }
  338. if (len < 0 || len >= maxlen) {
  339. return -1;
  340. }
  341. buff += len;
  342. total_len += len;
  343. maxlen -= len;
  344. /* format log message */
  345. len = vsnprintf(buff, maxlen, format, ap);
  346. if (len < 0 || len == maxlen) {
  347. return -1;
  348. }
  349. buff += len;
  350. total_len += len;
  351. /* return total length */
  352. return total_len;
  353. }
  354. static int _tlog_root_log_buffer(char *buff, int maxlen, void *userptr, const char *format, va_list ap)
  355. {
  356. int len = 0;
  357. int log_len = 0;
  358. struct tlog_info_inter *info_inter = (struct tlog_info_inter *)userptr;
  359. struct tlog_segment_log_head *log_head = NULL;
  360. int max_format_len = 0;
  361. if (tlog.root_format == NULL) {
  362. return -1;
  363. }
  364. if (tlog.root->segment_log) {
  365. log_head = (struct tlog_segment_log_head *)buff;
  366. len += sizeof(*log_head);
  367. memcpy(&log_head->info, &info_inter->info, sizeof(log_head->info));
  368. }
  369. max_format_len = maxlen - len - 2;
  370. buff[maxlen - 1] = 0;
  371. log_len = tlog.root_format(buff + len, max_format_len, &info_inter->info, info_inter->userptr, format, ap);
  372. if (log_len < 0) {
  373. return -1;
  374. } else if (log_len >= max_format_len) {
  375. buff[len + max_format_len - 2] = '.';
  376. buff[len + max_format_len - 3] = '.';
  377. buff[len + max_format_len - 4] = '.';
  378. log_len = max_format_len - 1;
  379. }
  380. len += log_len;
  381. /* add new line character*/
  382. if (*(buff + len - 1) != '\n' && len + 1 < maxlen - 1) {
  383. *(buff + len) = '\n';
  384. len++;
  385. log_len++;
  386. }
  387. if (tlog.root->segment_log && log_head != NULL) {
  388. if (len + 1 < maxlen - 1) {
  389. *(buff + len) = '\0';
  390. len++;
  391. }
  392. log_head->len = log_len;
  393. }
  394. return len;
  395. }
  396. static int _tlog_print_buffer(char *buff, int maxlen, void *userptr, const char *format, va_list ap)
  397. {
  398. int len;
  399. int total_len = 0;
  400. void *unused __attribute__((unused));
  401. unused = userptr;
  402. /* format log message */
  403. len = vsnprintf(buff, maxlen, format, ap);
  404. if (len < 0 || len == maxlen) {
  405. return -1;
  406. }
  407. buff += len;
  408. total_len += len;
  409. /* return total length */
  410. return total_len;
  411. }
  412. static int _tlog_need_drop(struct tlog_log *log)
  413. {
  414. int maxlen = 0;
  415. int ret = -1;
  416. if (log->block) {
  417. return -1;
  418. }
  419. pthread_mutex_lock(&tlog.lock);
  420. if (log->end == log->start) {
  421. if (log->ext_end == 0) {
  422. /* if buffer is empty */
  423. maxlen = log->buffsize - log->end;
  424. }
  425. } else if (log->end > log->start) {
  426. maxlen = log->buffsize - log->end;
  427. } else {
  428. /* if reverse */
  429. maxlen = log->start - log->end;
  430. }
  431. /* if free buffer length is less than min line length */
  432. if (maxlen < log->max_line_size) {
  433. log->dropped++;
  434. ret = 0;
  435. }
  436. pthread_mutex_unlock(&tlog.lock);
  437. return ret;
  438. }
  439. static int _tlog_vprintf(struct tlog_log *log, vprint_callback print_callback, void *userptr, const char *format, va_list ap)
  440. {
  441. int len;
  442. int maxlen = 0;
  443. struct tlog_segment_head *segment_head = NULL;
  444. if (log == NULL || format == NULL) {
  445. return -1;
  446. }
  447. char buff[log->max_line_size];
  448. if (log->buff == NULL) {
  449. return -1;
  450. }
  451. if (unlikely(log->logcount <= 0 && log->logscreen == 0)) {
  452. return 0;
  453. }
  454. if (_tlog_need_drop(log) == 0) {
  455. return -1;
  456. }
  457. len = print_callback(buff, sizeof(buff), userptr, format, ap);
  458. if (len <= 0) {
  459. return -1;
  460. } else if (len >= log->max_line_size) {
  461. len = log->max_line_size;
  462. buff[len - 1] = '\0';
  463. buff[len - 2] = '\n';
  464. buff[len - 3] = '.';
  465. buff[len - 4] = '.';
  466. buff[len - 5] = '.';
  467. }
  468. pthread_mutex_lock(&tlog.lock);
  469. do {
  470. if (log->end == log->start) {
  471. if (log->ext_end == 0) {
  472. /* if buffer is empty */
  473. maxlen = log->buffsize - log->end;
  474. }
  475. } else if (log->end > log->start) {
  476. maxlen = log->buffsize - log->end;
  477. } else {
  478. /* if reverse */
  479. maxlen = log->start - log->end;
  480. }
  481. /* if free buffer length is less than min line length */
  482. if (maxlen < log->max_line_size) {
  483. if (log->end != log->start) {
  484. tlog.notify_log = log;
  485. pthread_cond_signal(&tlog.cond);
  486. }
  487. /* if drop message, increase statistics and return */
  488. if (log->block == 0) {
  489. log->dropped++;
  490. pthread_mutex_unlock(&tlog.lock);
  491. return -1;
  492. }
  493. pthread_mutex_unlock(&tlog.lock);
  494. pthread_mutex_lock(&log->lock);
  495. log->waiters++;
  496. /* block wait for free buffer */
  497. int ret = pthread_cond_wait(&log->client_cond, &log->lock);
  498. log->waiters--;
  499. pthread_mutex_unlock(&log->lock);
  500. if (ret < 0) {
  501. return -1;
  502. }
  503. pthread_mutex_lock(&tlog.lock);
  504. }
  505. } while (maxlen < log->max_line_size);
  506. if (log->segment_log) {
  507. segment_head = (struct tlog_segment_head *)(log->buff + log->end);
  508. memcpy(segment_head->data, buff, len);
  509. log->end += len + sizeof(*segment_head) + 1;
  510. segment_head->len = len + 1;
  511. segment_head->data[len] = '\0';
  512. segment_head->magic = TLOG_SEGMENT_MAGIC;
  513. } else {
  514. /* write log to buffer */
  515. memcpy(log->buff + log->end, buff, len);
  516. log->end += len;
  517. }
  518. /* if remain buffer is not enough for a line, move end to start of buffer. */
  519. if (log->end > log->buffsize - log->max_line_size) {
  520. log->ext_end = log->end;
  521. log->end = 0;
  522. }
  523. if (tlog.is_wait) {
  524. tlog.notify_log = log;
  525. pthread_cond_signal(&tlog.cond);
  526. }
  527. pthread_mutex_unlock(&tlog.lock);
  528. return len;
  529. }
  530. int tlog_vprintf(struct tlog_log *log, const char *format, va_list ap)
  531. {
  532. return _tlog_vprintf(log, _tlog_print_buffer, NULL, format, ap);
  533. }
  534. int tlog_printf(struct tlog_log *log, const char *format, ...)
  535. {
  536. int len;
  537. va_list ap;
  538. va_start(ap, format);
  539. len = tlog_vprintf(log, format, ap);
  540. va_end(ap);
  541. return len;
  542. }
  543. int tlog_stdout_with_color(tlog_level level, const char *buff, int bufflen)
  544. {
  545. int unused __attribute__((unused));
  546. const char *color = NULL;
  547. switch (level) {
  548. case TLOG_DEBUG:
  549. color = "\033[0;94m";
  550. break;
  551. case TLOG_NOTICE:
  552. color = "\033[0;97m";
  553. break;
  554. case TLOG_WARN:
  555. color = "\033[0;33m";
  556. break;
  557. case TLOG_ERROR:
  558. color = "\033[0;31m";
  559. break;
  560. case TLOG_FATAL:
  561. color = "\033[31;1m";
  562. break;
  563. default:
  564. unused = write(STDOUT_FILENO, buff, bufflen);
  565. return bufflen;
  566. }
  567. if (color != NULL) {
  568. fprintf(stdout, "%s%.*s\033[0m\n", color, bufflen - 1, buff);
  569. } else {
  570. fprintf(stdout, "%s", buff);
  571. }
  572. return bufflen;
  573. }
  574. static int _tlog_early_print(struct tlog_info_inter *info_inter, const char *format, va_list ap)
  575. {
  576. char log_buf[TLOG_MAX_LINE_LEN];
  577. size_t len = 0;
  578. size_t out_len = 0;
  579. struct tlog_time cur_time;
  580. int unused __attribute__((unused));
  581. if (tlog.early_print_disable) {
  582. return 0;
  583. }
  584. if (_tlog_gettime(&cur_time) != 0) {
  585. return -1;
  586. }
  587. if (tlog.tlog_early_print != NULL) {
  588. tlog.tlog_early_print(&info_inter->info, format, ap);
  589. return out_len;
  590. }
  591. if (tlog.early_print_no_prefix == 0) {
  592. len = snprintf(log_buf, sizeof(log_buf), "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
  593. cur_time.year, cur_time.mon, cur_time.mday, cur_time.hour, cur_time.min, cur_time.sec, cur_time.usec / 1000,
  594. tlog_get_level_string(info_inter->info.level), info_inter->info.file, info_inter->info.line);
  595. }
  596. out_len = len;
  597. len = vsnprintf(log_buf + out_len, sizeof(log_buf) - out_len - 1, format, ap);
  598. out_len += len;
  599. if (len <= 0) {
  600. return -1;
  601. } else if (len >= sizeof(log_buf) - 1) {
  602. out_len = sizeof(log_buf) - 1;
  603. }
  604. if (log_buf[out_len - 1] != '\n') {
  605. log_buf[out_len] = '\n';
  606. out_len++;
  607. }
  608. if (out_len + 1 < sizeof(log_buf) - out_len - 1) {
  609. log_buf[out_len] = '\0';
  610. }
  611. if (tlog.early_print_output != NULL) {
  612. len = tlog.early_print_output(&info_inter->info, log_buf, out_len, tlog.early_print_userptr);
  613. if (tlog.early_print_with_screen == 0) {
  614. return len;
  615. }
  616. }
  617. if (tlog.early_print_color) {
  618. unused = tlog_stdout_with_color(info_inter->info.level, log_buf, out_len);
  619. } else {
  620. unused = write(STDOUT_FILENO, log_buf, out_len);
  621. }
  622. return out_len;
  623. }
  624. int tlog_vext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, va_list ap)
  625. {
  626. struct tlog_info_inter info_inter;
  627. if (level < tlog_set_level) {
  628. return 0;
  629. }
  630. if (level >= TLOG_END) {
  631. return -1;
  632. }
  633. info_inter.info.file = file;
  634. info_inter.info.line = line;
  635. info_inter.info.func = func;
  636. info_inter.info.level = level;
  637. info_inter.userptr = userptr;
  638. if (_tlog_gettime(&info_inter.info.time) != 0) {
  639. return -1;
  640. }
  641. if (tlog.root == NULL) {
  642. return _tlog_early_print(&info_inter, format, ap);
  643. }
  644. if (unlikely(tlog.root->logsize <= 0)) {
  645. return 0;
  646. }
  647. return _tlog_vprintf(tlog.root, _tlog_root_log_buffer, &info_inter, format, ap);
  648. }
  649. int tlog_ext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, ...)
  650. {
  651. int len;
  652. va_list ap;
  653. va_start(ap, format);
  654. len = tlog_vext(level, file, line, func, userptr, format, ap);
  655. va_end(ap);
  656. return len;
  657. }
  658. static int _tlog_rename_logfile(struct tlog_log *log, const char *log_file)
  659. {
  660. char archive_file[TLOG_BUFF_LEN];
  661. struct tlog_time logtime;
  662. int i = 0;
  663. if (_tlog_getmtime(&logtime, log_file) != 0) {
  664. return -1;
  665. }
  666. snprintf(archive_file, sizeof(archive_file), "%s/%s-%.4d%.2d%.2d-%.2d%.2d%.2d%s",
  667. log->logdir, log->logname, logtime.year, logtime.mon, logtime.mday,
  668. logtime.hour, logtime.min, logtime.sec, log->suffix);
  669. while (access(archive_file, F_OK) == 0) {
  670. i++;
  671. snprintf(archive_file, sizeof(archive_file), "%s/%s-%.4d%.2d%.2d-%.2d%.2d%.2d-%d%s",
  672. log->logdir, log->logname, logtime.year, logtime.mon,
  673. logtime.mday, logtime.hour, logtime.min, logtime.sec, i, log->suffix);
  674. }
  675. if (rename(log_file, archive_file) != 0) {
  676. return -1;
  677. }
  678. chmod(archive_file, log->archive_perm);
  679. return 0;
  680. }
  681. static int _tlog_list_dir(const char *path, list_callback callback, void *userptr)
  682. {
  683. DIR *dir = NULL;
  684. struct dirent *ent;
  685. int ret = 0;
  686. const char *unused __attribute__((unused)) = path;
  687. dir = opendir(path);
  688. if (dir == NULL) {
  689. fprintf(stderr, "tlog: open directory failed, %s\n", strerror(errno));
  690. goto errout;
  691. }
  692. while ((ent = readdir(dir)) != NULL) {
  693. if (strncmp(".", ent->d_name, 2) == 0 || strncmp("..", ent->d_name, 3) == 0) {
  694. continue;
  695. }
  696. ret = callback(path, ent, userptr);
  697. if (ret != 0) {
  698. goto errout;
  699. }
  700. }
  701. closedir(dir);
  702. return 0;
  703. errout:
  704. if (dir) {
  705. closedir(dir);
  706. dir = NULL;
  707. }
  708. return -1;
  709. }
  710. static int _tlog_count_log_callback(const char *path, struct dirent *entry, void *userptr)
  711. {
  712. struct count_log *count_log = (struct count_log *)userptr;
  713. struct tlog_log *log = count_log->log;
  714. char logname[TLOG_LOG_NAME_LEN * 2];
  715. const char *unused __attribute__((unused)) = path;
  716. if (strstr(entry->d_name, log->suffix) == NULL) {
  717. return 0;
  718. }
  719. snprintf(logname, sizeof(logname), "%s-", log->logname);
  720. int len = strnlen(logname, sizeof(logname));
  721. if (strncmp(logname, entry->d_name, len) != 0) {
  722. return 0;
  723. }
  724. count_log->lognum++;
  725. return 0;
  726. }
  727. static int _tlog_get_oldest_callback(const char *path, struct dirent *entry, void *userptr)
  728. {
  729. struct stat sb;
  730. char filename[TLOG_BUFF_LEN];
  731. struct oldest_log *oldestlog = (struct oldest_log *)userptr;
  732. struct tlog_log *log = oldestlog->log;
  733. char logname[TLOG_LOG_NAME_LEN * 2];
  734. /* if not a log file, skip */
  735. if (strstr(entry->d_name, log->suffix) == NULL) {
  736. return 0;
  737. }
  738. /* if not tlog log file, skip */
  739. snprintf(logname, sizeof(logname), "%s-", log->logname);
  740. int len = strnlen(logname, sizeof(logname));
  741. if (strncmp(logname, entry->d_name, len) != 0) {
  742. return 0;
  743. }
  744. /* get log file mtime */
  745. snprintf(filename, sizeof(filename), "%s/%s", path, entry->d_name);
  746. if (stat(filename, &sb) != 0) {
  747. return -1;
  748. }
  749. if (oldestlog->mtime == 0 || oldestlog->mtime > sb.st_mtime) {
  750. oldestlog->mtime = sb.st_mtime;
  751. strncpy(oldestlog->name, entry->d_name, sizeof(oldestlog->name));
  752. oldestlog->name[sizeof(oldestlog->name) - 1] = '\0';
  753. return 0;
  754. }
  755. return 0;
  756. }
  757. static int _tlog_remove_oldestlog(struct tlog_log *log)
  758. {
  759. struct oldest_log oldestlog;
  760. oldestlog.name[0] = 0;
  761. oldestlog.mtime = 0;
  762. oldestlog.log = log;
  763. /* get oldest log file name */
  764. if (_tlog_list_dir(log->logdir, _tlog_get_oldest_callback, &oldestlog) != 0) {
  765. return -1;
  766. }
  767. char filename[PATH_MAX * 2];
  768. snprintf(filename, sizeof(filename), "%s/%s", log->logdir, oldestlog.name);
  769. /* delete */
  770. unlink(filename);
  771. return 0;
  772. }
  773. static int _tlog_remove_oldlog(struct tlog_log *log)
  774. {
  775. struct count_log count_log;
  776. int i = 0;
  777. count_log.lognum = 0;
  778. count_log.log = log;
  779. /* get total log file number */
  780. if (_tlog_list_dir(log->logdir, _tlog_count_log_callback, &count_log) != 0) {
  781. fprintf(stderr, "tlog: get log file count failed.\n");
  782. return -1;
  783. }
  784. /* remove last N log files */
  785. for (i = 0; i < count_log.lognum - log->logcount; i++) {
  786. _tlog_remove_oldestlog(log);
  787. }
  788. return 0;
  789. }
  790. static void _tlog_log_unlock(struct tlog_log *log)
  791. {
  792. char lock_file[PATH_MAX * 2];
  793. if (log->fd_lock <= 0) {
  794. return;
  795. }
  796. snprintf(lock_file, sizeof(lock_file), "%s/%s.lock", log->logdir, log->logname);
  797. unlink(lock_file);
  798. close(log->fd_lock);
  799. log->fd_lock = -1;
  800. }
  801. static int _tlog_log_lock(struct tlog_log *log)
  802. {
  803. char lock_file[PATH_MAX * 2];
  804. int fd;
  805. if (log->multi_log == 0) {
  806. return 0;
  807. }
  808. snprintf(lock_file, sizeof(lock_file), "%s/%s.lock", log->logdir, log->logname);
  809. fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
  810. if (fd == -1) {
  811. fprintf(stderr, "tlog: create lock file failed, %s", strerror(errno));
  812. return -1;
  813. }
  814. if (lockf(fd, F_TLOCK, 0) < 0) {
  815. goto errout;
  816. }
  817. log->fd_lock = fd;
  818. return 0;
  819. errout:
  820. if (fd > 0) {
  821. close(fd);
  822. }
  823. return -1;
  824. }
  825. static void _tlog_wait_pid(struct tlog_log *log, int wait_hang)
  826. {
  827. int status;
  828. if (log->zip_pid <= 0) {
  829. return;
  830. }
  831. int option = (wait_hang == 0) ? WNOHANG : 0;
  832. /* check and obtain gzip process status*/
  833. if (waitpid(log->zip_pid, &status, option) <= 0) {
  834. return;
  835. }
  836. /* gzip process exited */
  837. log->zip_pid = -1;
  838. char gzip_file[PATH_MAX * 2];
  839. /* rename zipped file */
  840. snprintf(gzip_file, sizeof(gzip_file), "%s/%s.pending.gz", log->logdir, log->logname);
  841. if (_tlog_rename_logfile(log, gzip_file) != 0) {
  842. _tlog_log_unlock(log);
  843. return;
  844. }
  845. /* remove oldest file */
  846. _tlog_remove_oldlog(log);
  847. _tlog_log_unlock(log);
  848. }
  849. static void _tlog_close_all_fd_by_res(void)
  850. {
  851. struct rlimit lim;
  852. int maxfd = 0;
  853. int i = 0;
  854. getrlimit(RLIMIT_NOFILE, &lim);
  855. maxfd = lim.rlim_cur;
  856. if (maxfd > 4096) {
  857. maxfd = 4096;
  858. }
  859. for (i = 3; i < maxfd; i++) {
  860. close(i);
  861. }
  862. }
  863. static int _tlog_str_to_int(const char *str)
  864. {
  865. int num = 0;
  866. while (*str >= '0' && *str <= '9') {
  867. num = num * 10 + (*str - '0');
  868. ++str;
  869. }
  870. if (*str) {
  871. return -1;
  872. }
  873. return num;
  874. }
  875. static void _tlog_close_all_fd(void)
  876. {
  877. #if defined(__linux__)
  878. int dir_fd = -1;
  879. dir_fd = open("/proc/self/fd/", O_RDONLY | O_DIRECTORY);
  880. if (dir_fd < 0) {
  881. goto errout;
  882. }
  883. char buffer[sizeof(struct linux_dirent64)];
  884. int bytes;
  885. while ((bytes = syscall(SYS_getdents64, dir_fd,
  886. (struct linux_dirent64 *)buffer,
  887. sizeof(buffer)))
  888. > 0) {
  889. struct linux_dirent64 *entry;
  890. int offset;
  891. for (offset = 0; offset < bytes; offset += entry->d_reclen) {
  892. int fd;
  893. entry = (struct linux_dirent64 *)(buffer + offset);
  894. if ((fd = _tlog_str_to_int(entry->d_name)) < 0) {
  895. continue;
  896. }
  897. if (fd == dir_fd || fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO) {
  898. continue;
  899. }
  900. close(fd);
  901. }
  902. }
  903. close(dir_fd);
  904. if (bytes < 0) {
  905. goto errout;
  906. }
  907. return;
  908. errout:
  909. if (dir_fd > 0) {
  910. close(dir_fd);
  911. }
  912. #endif
  913. _tlog_close_all_fd_by_res();
  914. return;
  915. }
  916. static int _tlog_archive_log_compressed(struct tlog_log *log)
  917. {
  918. char gzip_file[TLOG_BUFF_LEN];
  919. char log_file[TLOG_BUFF_LEN];
  920. char pending_file[TLOG_BUFF_LEN];
  921. snprintf(gzip_file, sizeof(gzip_file), "%s/%s.pending.gz", log->logdir, log->logname);
  922. snprintf(pending_file, sizeof(pending_file), "%s/%s.pending", log->logdir, log->logname);
  923. if (_tlog_log_lock(log) != 0) {
  924. return -1;
  925. }
  926. /* if pending.zip exists */
  927. if (access(gzip_file, F_OK) == 0) {
  928. /* rename it to standard name */
  929. if (_tlog_rename_logfile(log, gzip_file) != 0) {
  930. goto errout;
  931. }
  932. }
  933. if (access(pending_file, F_OK) != 0) {
  934. /* rename current log file to pending */
  935. snprintf(log_file, sizeof(log_file), "%s/%s", log->logdir, log->logname);
  936. if (rename(log_file, pending_file) != 0) {
  937. goto errout;
  938. }
  939. }
  940. /* start gzip process to compress log file */
  941. if (log->zip_pid <= 0) {
  942. int pid = vfork();
  943. if (pid == 0) {
  944. _tlog_close_all_fd();
  945. execl(tlog.gzip_cmd, tlog.gzip_cmd, "-1", pending_file, NULL);
  946. fprintf(stderr, "tlog: execl gzip failed, no compress\n");
  947. log->nocompress = 1;
  948. _exit(1);
  949. } else if (pid < 0) {
  950. if (errno == EPERM || errno == EACCES) {
  951. fprintf(stderr, "tlog: vfork failed, errno: %d, no compress\n", errno);
  952. log->nocompress = 1;
  953. }
  954. goto errout;
  955. }
  956. log->zip_pid = pid;
  957. }
  958. return 0;
  959. errout:
  960. _tlog_log_unlock(log);
  961. return -1;
  962. }
  963. static int _tlog_archive_log_nocompress(struct tlog_log *log)
  964. {
  965. char log_file[TLOG_BUFF_LEN];
  966. char pending_file[TLOG_BUFF_LEN];
  967. snprintf(pending_file, sizeof(pending_file), "%s/%s.pending", log->logdir, log->logname);
  968. if (_tlog_log_lock(log) != 0) {
  969. return -1;
  970. }
  971. if (access(pending_file, F_OK) != 0) {
  972. /* rename current log file to pending */
  973. snprintf(log_file, sizeof(log_file), "%s/%s", log->logdir, log->logname);
  974. if (rename(log_file, pending_file) != 0) {
  975. goto errout;
  976. }
  977. }
  978. /* rename pending file */
  979. if (_tlog_rename_logfile(log, pending_file) != 0) {
  980. goto errout;
  981. }
  982. /* remove oldest file */
  983. _tlog_remove_oldlog(log);
  984. _tlog_log_unlock(log);
  985. return 0;
  986. errout:
  987. _tlog_log_unlock(log);
  988. return -1;
  989. }
  990. static int _tlog_archive_log(struct tlog_log *log)
  991. {
  992. if (log->nocompress) {
  993. return _tlog_archive_log_nocompress(log);
  994. } else {
  995. return _tlog_archive_log_compressed(log);
  996. }
  997. }
  998. static void _tlog_get_log_name_dir(struct tlog_log *log)
  999. {
  1000. char log_file[PATH_MAX];
  1001. if (log->fd > 0) {
  1002. close(log->fd);
  1003. log->fd = -1;
  1004. }
  1005. pthread_mutex_lock(&tlog.lock);
  1006. strncpy(log_file, log->pending_logfile, sizeof(log_file) - 1);
  1007. log_file[sizeof(log_file) - 1] = '\0';
  1008. strncpy(log->logdir, dirname(log_file), sizeof(log->logdir) - 1);
  1009. log->logdir[sizeof(log->logdir) - 1] = '\0';
  1010. strncpy(log_file, log->pending_logfile, PATH_MAX);
  1011. log_file[sizeof(log_file) - 1] = '\0';
  1012. strncpy(log->logname, basename(log_file), sizeof(log->logname) - 1);
  1013. log->logname[sizeof(log->logname) - 1] = '\0';
  1014. pthread_mutex_unlock(&tlog.lock);
  1015. }
  1016. static int _tlog_write_screen(struct tlog_log *log, struct tlog_loginfo *info, const char *buff, int bufflen)
  1017. {
  1018. if (bufflen <= 0) {
  1019. return 0;
  1020. }
  1021. if (log->logscreen == 0) {
  1022. return 0;
  1023. }
  1024. if (info == NULL) {
  1025. return write(STDOUT_FILENO, buff, bufflen);;
  1026. }
  1027. return tlog_stdout_with_color(info->level, buff, bufflen);
  1028. }
  1029. static int _tlog_write_ext(struct tlog_log *log, struct tlog_loginfo *info, const char *buff, int bufflen)
  1030. {
  1031. int len;
  1032. int unused __attribute__((unused));
  1033. if (bufflen <= 0 || log->fail) {
  1034. return 0;
  1035. }
  1036. if (log->rename_pending) {
  1037. _tlog_get_log_name_dir(log);
  1038. log->rename_pending = 0;
  1039. }
  1040. if (log->logcount <= 0) {
  1041. return 0;
  1042. }
  1043. /* if log file size exceeds threshold, start to compress */
  1044. if (log->multi_log && log->fd > 0) {
  1045. log->filesize = lseek(log->fd, 0, SEEK_END);
  1046. }
  1047. if (log->filesize > log->logsize && log->zip_pid <= 0) {
  1048. if (log->filesize < lseek(log->fd, 0, SEEK_END) && log->multi_log == 0) {
  1049. const char *msg = "[Auto enable multi-process write mode, log may be lost, please enable multi-process write mode manually]\n";
  1050. log->multi_log = 1;
  1051. unused = write(log->fd, msg, strlen(msg));
  1052. }
  1053. close(log->fd);
  1054. log->fd = -1;
  1055. log->filesize = 0;
  1056. _tlog_archive_log(log);
  1057. }
  1058. if (log->fd <= 0) {
  1059. /* open a new log file to write */
  1060. time_t now;
  1061. time(&now);
  1062. if (now == log->last_try) {
  1063. return -1;
  1064. }
  1065. log->last_try = now;
  1066. char logfile[PATH_MAX * 2];
  1067. if (_tlog_mkdir(log->logdir) != 0) {
  1068. if (log->print_errmsg == 0) {
  1069. return -1;
  1070. }
  1071. log->print_errmsg = 0;
  1072. fprintf(stderr, "tlog: create log dir %s failed, %s\n", log->logdir, strerror(errno));
  1073. if (errno == EACCES && log->logscreen == 0) {
  1074. fprintf(stderr, "tlog: no permission to write log file, output log to console\n");
  1075. tlog_logscreen(log, 1);
  1076. tlog_logcount(log, 0);
  1077. }
  1078. return -1;
  1079. }
  1080. snprintf(logfile, sizeof(logfile), "%s/%s", log->logdir, log->logname);
  1081. log->filesize = 0;
  1082. log->fd = open(logfile, O_APPEND | O_CREAT | O_WRONLY | O_CLOEXEC, log->file_perm);
  1083. if (log->fd < 0) {
  1084. if (log->print_errmsg == 0) {
  1085. return -1;
  1086. }
  1087. fprintf(stderr, "tlog: open log file %s failed, %s\n", logfile, strerror(errno));
  1088. log->print_errmsg = 0;
  1089. return -1;
  1090. }
  1091. if (log->mode_changed != 0) {
  1092. fchmod(log->fd, log->file_perm);
  1093. }
  1094. log->last_try = 0;
  1095. log->print_errmsg = 1;
  1096. /* get log file size */
  1097. log->filesize = lseek(log->fd, 0, SEEK_END);
  1098. }
  1099. /* write log to file */
  1100. len = write(log->fd, buff, bufflen);
  1101. if (len > 0) {
  1102. log->filesize += len;
  1103. } else {
  1104. if (log->fd > 0 && errno == ENOSPC) {
  1105. close(log->fd);
  1106. log->fd = -1;
  1107. }
  1108. }
  1109. return len;
  1110. }
  1111. static inline int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
  1112. {
  1113. return _tlog_write_ext(log, NULL, buff, bufflen);
  1114. }
  1115. int tlog_write(struct tlog_log *log, const char *buff, int bufflen)
  1116. {
  1117. return _tlog_write(log, buff, bufflen);
  1118. }
  1119. static int _tlog_has_data(struct tlog_log *log)
  1120. {
  1121. if (log->end != log->start || log->ext_end > 0) {
  1122. return 1;
  1123. }
  1124. return 0;
  1125. }
  1126. static int _tlog_any_has_data_locked(void)
  1127. {
  1128. struct tlog_log *next = NULL;
  1129. next = tlog.log;
  1130. while (next) {
  1131. if (_tlog_has_data(next) == 1) {
  1132. return 1;
  1133. }
  1134. next = next->next;
  1135. }
  1136. return 0;
  1137. }
  1138. static int _tlog_any_has_data(void)
  1139. {
  1140. int ret = 0;
  1141. pthread_mutex_lock(&tlog.lock);
  1142. ret = _tlog_any_has_data_locked();
  1143. pthread_mutex_unlock(&tlog.lock);
  1144. return ret;
  1145. }
  1146. static int _tlog_wait_pids(void)
  1147. {
  1148. time_t now = time(NULL);
  1149. struct tlog_log *next = NULL;
  1150. static struct tlog_log *last_log = NULL;
  1151. pthread_mutex_lock(&tlog.lock);
  1152. for (next = tlog.log; next != NULL; next = next->next) {
  1153. if (next->zip_pid <= 0) {
  1154. continue;
  1155. }
  1156. if (next == last_log) {
  1157. continue;
  1158. }
  1159. if (next->last_waitpid == now) {
  1160. continue;
  1161. }
  1162. last_log = next;
  1163. next->last_waitpid = now;
  1164. pthread_mutex_unlock(&tlog.lock);
  1165. _tlog_wait_pid(next, 0);
  1166. return 0;
  1167. }
  1168. last_log = NULL;
  1169. pthread_mutex_unlock(&tlog.lock);
  1170. return 0;
  1171. }
  1172. static int _tlog_close(struct tlog_log *log, int wait_hang)
  1173. {
  1174. struct tlog_log *next = tlog.log;
  1175. if (log == NULL) {
  1176. return -1;
  1177. }
  1178. if (log->zip_pid > 0) {
  1179. _tlog_wait_pid(log, wait_hang);
  1180. if (log->zip_pid > 0) {
  1181. return -1;
  1182. }
  1183. }
  1184. if (log->fd > 0) {
  1185. close(log->fd);
  1186. log->fd = -1;
  1187. }
  1188. _tlog_log_unlock(log);
  1189. if (log->buff != NULL) {
  1190. free(log->buff);
  1191. log->buff = NULL;
  1192. }
  1193. if (next == log) {
  1194. tlog.log = next->next;
  1195. free(log);
  1196. return 0;
  1197. }
  1198. while (next) {
  1199. if (next->next == log) {
  1200. next->next = log->next;
  1201. free(log);
  1202. return -1;
  1203. }
  1204. next = next->next;
  1205. }
  1206. pthread_cond_destroy(&log->client_cond);
  1207. pthread_mutex_destroy(&log->lock);
  1208. return 0;
  1209. }
  1210. static struct tlog_log *_tlog_next_log(struct tlog_log *last_log)
  1211. {
  1212. if (last_log == NULL) {
  1213. return tlog.log;
  1214. }
  1215. return last_log->next;
  1216. }
  1217. static struct tlog_log *_tlog_wait_log_locked(struct tlog_log *last_log)
  1218. {
  1219. int ret = 0;
  1220. struct timespec tm;
  1221. struct tlog_log *log = NULL;
  1222. struct tlog_log *next = NULL;
  1223. int need_wait_pid = 0;
  1224. for (next = tlog.log; next != NULL; next = next->next) {
  1225. if (next->zip_pid > 0) {
  1226. need_wait_pid = 1;
  1227. break;
  1228. }
  1229. }
  1230. clock_gettime(CLOCK_REALTIME, &tm);
  1231. tm.tv_sec += 2;
  1232. tlog.is_wait = 1;
  1233. tlog.wait_on_log = last_log;
  1234. if (need_wait_pid != 0) {
  1235. ret = pthread_cond_timedwait(&tlog.cond, &tlog.lock, &tm);
  1236. } else {
  1237. ret = pthread_cond_wait(&tlog.cond, &tlog.lock);
  1238. }
  1239. tlog.is_wait = 0;
  1240. tlog.wait_on_log = NULL;
  1241. errno = ret;
  1242. if (ret == 0 || ret == ETIMEDOUT) {
  1243. log = tlog.notify_log;
  1244. tlog.notify_log = NULL;
  1245. }
  1246. return log;
  1247. }
  1248. static void _tlog_wakeup_waiters(struct tlog_log *log)
  1249. {
  1250. pthread_mutex_lock(&log->lock);
  1251. if (log->waiters > 0) {
  1252. /* if there are waiters, wakeup */
  1253. pthread_cond_broadcast(&log->client_cond);
  1254. }
  1255. pthread_mutex_unlock(&log->lock);
  1256. }
  1257. static int _tlog_write_output_func(struct tlog_log *log, char *buff, int bufflen)
  1258. {
  1259. if (log->logscreen && log != tlog.root) {
  1260. _tlog_write_screen(log, NULL, buff, bufflen);
  1261. }
  1262. if (log->output_func == NULL) {
  1263. return -1;
  1264. }
  1265. return log->output_func(log, buff, bufflen);
  1266. }
  1267. static void _tlog_write_one_segment_log(struct tlog_log *log, char *buff, int bufflen)
  1268. {
  1269. struct tlog_segment_head *segment_head = NULL;
  1270. int write_len = 0;
  1271. segment_head = (struct tlog_segment_head *)buff;
  1272. for (write_len = 0; write_len < bufflen;) {
  1273. if (segment_head->magic != TLOG_SEGMENT_MAGIC) {
  1274. return;
  1275. }
  1276. _tlog_write_output_func(log, segment_head->data, segment_head->len - 1);
  1277. write_len += segment_head->len + sizeof(*segment_head);
  1278. segment_head = (struct tlog_segment_head *)(buff + write_len);
  1279. }
  1280. }
  1281. static void _tlog_write_segments_log(struct tlog_log *log, int log_len, int log_extlen)
  1282. {
  1283. _tlog_write_one_segment_log(log, log->buff + log->start, log_len);
  1284. if (log_extlen > 0) {
  1285. /* write extend buffer log */
  1286. _tlog_write_one_segment_log(log, log->buff, log_extlen);
  1287. }
  1288. }
  1289. static void _tlog_write_buff_log(struct tlog_log *log, int log_len, int log_extlen)
  1290. {
  1291. _tlog_write_output_func(log, log->buff + log->start, log_len);
  1292. if (log_extlen > 0) {
  1293. /* write extend buffer log */
  1294. _tlog_write_output_func(log, log->buff, log_extlen);
  1295. }
  1296. }
  1297. static void _tlog_work_write(struct tlog_log *log, int log_len, int log_extlen, int log_dropped)
  1298. {
  1299. /* write log */
  1300. if (log->segment_log) {
  1301. _tlog_write_segments_log(log, log_len, log_extlen);
  1302. } else {
  1303. _tlog_write_buff_log(log, log_len, log_extlen);
  1304. }
  1305. if (log_dropped > 0) {
  1306. /* if there is dropped log, record dropped log number */
  1307. char dropmsg[TLOG_TMP_LEN];
  1308. char *msg = dropmsg;
  1309. struct tlog_segment_log_head *log_head = NULL;
  1310. if (log->segment_log) {
  1311. memset(dropmsg, 0, sizeof(struct tlog_segment_log_head));
  1312. log_head = (struct tlog_segment_log_head *)dropmsg;
  1313. msg += sizeof(struct tlog_segment_log_head);
  1314. log_head->info.level = TLOG_WARN;
  1315. }
  1316. int len = snprintf(msg, msg - dropmsg, "[Total Dropped %d Messages]\n", log_dropped);
  1317. if (log_head) {
  1318. log_head->len = len;
  1319. }
  1320. _tlog_write_output_func(log, dropmsg, strnlen(dropmsg, sizeof(dropmsg)));
  1321. }
  1322. }
  1323. static int _tlog_root_write_screen_log(struct tlog_log *log, struct tlog_loginfo *info, const char *buff, int bufflen)
  1324. {
  1325. if (log->logscreen == 0) {
  1326. return 0;
  1327. }
  1328. return _tlog_write_screen(log, info, buff, bufflen);
  1329. }
  1330. static int _tlog_root_write_log(struct tlog_log *log, const char *buff, int bufflen)
  1331. {
  1332. struct tlog_segment_log_head *head = NULL;
  1333. static struct tlog_segment_log_head empty_info;
  1334. if (tlog.output_func == NULL) {
  1335. if (log->segment_log) {
  1336. head = (struct tlog_segment_log_head *)buff;
  1337. _tlog_root_write_screen_log(log, &head->info, head->data, head->len);
  1338. return _tlog_write_ext(log, &head->info, head->data, head->len);
  1339. }
  1340. _tlog_root_write_screen_log(log, NULL, buff, bufflen);
  1341. return _tlog_write(log, buff, bufflen);
  1342. }
  1343. if (log->segment_log && tlog.root == log) {
  1344. head = (struct tlog_segment_log_head *)buff;
  1345. _tlog_root_write_screen_log(log, &head->info, head->data, head->len - 1);
  1346. return tlog.output_func(&head->info, head->data, head->len - 1, tlog_get_private(log));
  1347. }
  1348. _tlog_root_write_screen_log(log, NULL, buff, bufflen);
  1349. memset(&empty_info, 0, sizeof(empty_info));
  1350. return tlog.output_func(&empty_info.info, buff, bufflen, tlog_get_private(log));
  1351. }
  1352. static void tlog_wait_zip_fini(void)
  1353. {
  1354. tlog_log *next;
  1355. if (tlog.root == NULL) {
  1356. return;
  1357. }
  1358. int wait_zip = 1;
  1359. int time_out = 0;
  1360. while (wait_zip) {
  1361. wait_zip = 0;
  1362. time_out++;
  1363. next = tlog.log;
  1364. while (next) {
  1365. if (next->zip_pid > 0 && wait_zip == 0) {
  1366. wait_zip = 1;
  1367. usleep(1000);
  1368. }
  1369. if (kill(next->zip_pid, 0) != 0 || time_out >= 5000) {
  1370. next->zip_pid = -1;
  1371. }
  1372. next = next->next;
  1373. }
  1374. }
  1375. return;
  1376. }
  1377. static void *_tlog_work(void *arg)
  1378. {
  1379. int log_len = 0;
  1380. int log_extlen = 0;
  1381. int log_end = 0;
  1382. int log_extend = 0;
  1383. int log_dropped = 0;
  1384. struct tlog_log *log = NULL;
  1385. struct tlog_log *loop_log = NULL;
  1386. void *unused __attribute__((unused));
  1387. unused = arg;
  1388. // for child process
  1389. tlog_wait_zip_fini();
  1390. while (1) {
  1391. log_len = 0;
  1392. log_extlen = 0;
  1393. log_extend = 0;
  1394. if (tlog.run == 0) {
  1395. if (_tlog_any_has_data() == 0) {
  1396. break;
  1397. }
  1398. }
  1399. _tlog_wait_pids();
  1400. pthread_mutex_lock(&tlog.lock);
  1401. if (loop_log == NULL) {
  1402. loop_log = log;
  1403. }
  1404. log = _tlog_next_log(log);
  1405. if (log == NULL) {
  1406. pthread_mutex_unlock(&tlog.lock);
  1407. continue;
  1408. }
  1409. /* if buffer is empty, wait */
  1410. if (_tlog_any_has_data_locked() == 0 && tlog.run) {
  1411. log = _tlog_wait_log_locked(log);
  1412. if (log == NULL) {
  1413. pthread_mutex_unlock(&tlog.lock);
  1414. if (errno != ETIMEDOUT && tlog.run) {
  1415. sleep(1);
  1416. }
  1417. continue;
  1418. }
  1419. }
  1420. if (_tlog_has_data(log) == 0) {
  1421. if (log->is_exit) {
  1422. if (_tlog_close(log, 0) == 0) {
  1423. log = NULL;
  1424. loop_log = NULL;
  1425. };
  1426. }
  1427. pthread_mutex_unlock(&tlog.lock);
  1428. continue;
  1429. }
  1430. loop_log = NULL;
  1431. if (log->ext_end > 0) {
  1432. log_len = log->ext_end - log->start;
  1433. log_extend = log->ext_end;
  1434. }
  1435. if (log->end < log->start) {
  1436. log_extlen = log->end;
  1437. } else if (log->end > log->start) {
  1438. log_len = log->end - log->start;
  1439. }
  1440. log_end = log->end;
  1441. log_dropped = log->dropped;
  1442. log->dropped = 0;
  1443. pthread_mutex_unlock(&tlog.lock);
  1444. /* start write log work */
  1445. _tlog_work_write(log, log_len, log_extlen, log_dropped);
  1446. pthread_mutex_lock(&tlog.lock);
  1447. /* release finished buffer */
  1448. log->start = log_end;
  1449. if (log_extend > 0) {
  1450. log->ext_end = 0;
  1451. }
  1452. pthread_mutex_unlock(&tlog.lock);
  1453. _tlog_wakeup_waiters(log);
  1454. }
  1455. return NULL;
  1456. }
  1457. void tlog_set_early_printf(int enable, int no_prefix, int color)
  1458. {
  1459. tlog.early_print_disable = (enable == 0) ? 1 : 0;
  1460. tlog.early_print_no_prefix = (no_prefix == 0) ? 0 : 1;
  1461. tlog.early_print_color = (color == 0 || isatty(STDOUT_FILENO) == 0) ? 0 : 1;
  1462. }
  1463. void tlog_reg_early_printf_callback(tlog_early_print_func callback)
  1464. {
  1465. tlog.tlog_early_print = callback;
  1466. }
  1467. void tlog_reg_early_printf_output_callback(tlog_log_output_func callback, int log_screen, void *private_data)
  1468. {
  1469. tlog.early_print_output = callback;
  1470. tlog.early_print_userptr = private_data;
  1471. tlog.early_print_with_screen = (log_screen == 0) ? 0 : 1;
  1472. }
  1473. const char *tlog_get_level_string(tlog_level level)
  1474. {
  1475. if (level >= TLOG_END) {
  1476. return NULL;
  1477. }
  1478. return tlog_level_str[level];
  1479. }
  1480. void tlog_set_maxlog_count(int count)
  1481. {
  1482. tlog_logcount(tlog.root, count);
  1483. }
  1484. static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
  1485. {
  1486. if (log == NULL) {
  1487. return;
  1488. }
  1489. log->logscreen = (enable != 0) ? 1 : 0;
  1490. }
  1491. void tlog_setlogscreen(int enable)
  1492. {
  1493. _tlog_log_setlogscreen(tlog.root, enable);
  1494. }
  1495. int tlog_write_log(char *buff, int bufflen)
  1496. {
  1497. if (unlikely(tlog.root == NULL)) {
  1498. return -1;
  1499. }
  1500. return _tlog_write(tlog.root, buff, bufflen);
  1501. }
  1502. void tlog_logscreen(tlog_log *log, int enable)
  1503. {
  1504. if (log == NULL) {
  1505. return;
  1506. }
  1507. _tlog_log_setlogscreen(log, enable);
  1508. }
  1509. static int _tlog_reg_output_func(tlog_log *log, tlog_output_func output)
  1510. {
  1511. if (log == NULL) {
  1512. return -1;
  1513. }
  1514. if (output == NULL) {
  1515. log->output_func = _tlog_write;
  1516. return 0;
  1517. }
  1518. log->output_func = output;
  1519. return 0;
  1520. }
  1521. int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
  1522. {
  1523. if (log == tlog.root) {
  1524. return -1;
  1525. }
  1526. return _tlog_reg_output_func(log, output);
  1527. }
  1528. int tlog_reg_format_func(tlog_format_func callback)
  1529. {
  1530. tlog.root_format = callback;
  1531. return 0;
  1532. }
  1533. int tlog_reg_log_output_func(tlog_log_output_func output, void *private_data)
  1534. {
  1535. tlog.output_func = output;
  1536. tlog_set_private(tlog.root, private_data);
  1537. return 0;
  1538. }
  1539. int tlog_setlevel(tlog_level level)
  1540. {
  1541. if (level >= TLOG_END) {
  1542. return -1;
  1543. }
  1544. tlog_set_level = level;
  1545. return 0;
  1546. }
  1547. int tlog_log_enabled(tlog_level level)
  1548. {
  1549. if (level >= TLOG_END) {
  1550. return 0;
  1551. }
  1552. return (tlog_set_level >= level) ? 1 : 0;
  1553. }
  1554. tlog_level tlog_getlevel(void)
  1555. {
  1556. return tlog_set_level;
  1557. }
  1558. void tlog_set_logfile(const char *logfile)
  1559. {
  1560. tlog_rename_logfile(tlog.root, logfile);
  1561. }
  1562. static void _tlog_get_gzip_cmd_path(void)
  1563. {
  1564. char *copy_path = NULL;
  1565. char gzip_cmd_path[PATH_MAX];
  1566. const char *env_path = getenv("PATH");
  1567. char *save_ptr = NULL;
  1568. if (env_path == NULL) {
  1569. env_path = "/bin:/usr/bin:/usr/local/bin";
  1570. }
  1571. copy_path = strdup(env_path);
  1572. if (copy_path == NULL) {
  1573. return;
  1574. }
  1575. for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(NULL, ":", &save_ptr)) {
  1576. snprintf(gzip_cmd_path, sizeof(gzip_cmd_path), "%s/gzip", tok);
  1577. if (access(gzip_cmd_path, X_OK) != 0) {
  1578. continue;
  1579. }
  1580. snprintf(tlog.gzip_cmd, sizeof(tlog.gzip_cmd), "%s", gzip_cmd_path);
  1581. break;
  1582. }
  1583. free(copy_path);
  1584. }
  1585. tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
  1586. {
  1587. struct tlog_log *log = NULL;
  1588. if (tlog.run == 0) {
  1589. fprintf(stderr, "tlog: tlog is not initialized.\n");
  1590. return NULL;
  1591. }
  1592. log = (struct tlog_log *)malloc(sizeof(*log));
  1593. if (log == NULL) {
  1594. fprintf(stderr, "tlog: malloc log failed.\n");
  1595. return NULL;
  1596. }
  1597. memset(log, 0, sizeof(*log));
  1598. log->start = 0;
  1599. log->end = 0;
  1600. log->ext_end = 0;
  1601. log->dropped = 0;
  1602. log->buffsize = (buffsize > 0) ? buffsize : TLOG_BUFF_SIZE;
  1603. log->logsize = (maxlogsize >= 0) ? maxlogsize : TLOG_LOG_SIZE;
  1604. log->logcount = (maxlogcount <= 0) ? 0 : maxlogcount;
  1605. log->fd = -1;
  1606. log->filesize = 0;
  1607. log->zip_pid = -1;
  1608. log->is_exit = 0;
  1609. log->fail = 0;
  1610. log->print_errmsg = 1;
  1611. log->waiters = 0;
  1612. log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
  1613. log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;
  1614. log->logscreen = ((flag & TLOG_SCREEN) == 0) ? 0 : 1;
  1615. log->logscreen_color = ((flag & TLOG_SCREEN_COLOR) == 0 || isatty(STDOUT_FILENO) == 0) ? 0 : 1;
  1616. log->multi_log = ((flag & TLOG_MULTI_WRITE) == 0) ? 0 : 1;
  1617. log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1;
  1618. log->max_line_size = TLOG_MAX_LINE_LEN;
  1619. log->output_func = _tlog_write;
  1620. log->file_perm = S_IRUSR | S_IWUSR | S_IRGRP;
  1621. log->archive_perm = S_IRUSR | S_IRGRP;
  1622. if (log->nocompress == 0 && tlog.gzip_cmd[0] == '\0') {
  1623. log->nocompress = 1;
  1624. }
  1625. if (log->logscreen_color == 1) {
  1626. log->logscreen = 1;
  1627. log->segment_log = 1;
  1628. }
  1629. tlog_rename_logfile(log, logfile);
  1630. if (log->nocompress) {
  1631. strncpy(log->suffix, TLOG_SUFFIX_LOG, sizeof(log->suffix));
  1632. } else {
  1633. strncpy(log->suffix, TLOG_SUFFIX_GZ, sizeof(log->suffix));
  1634. }
  1635. log->buff = (char *)malloc(log->buffsize);
  1636. if (log->buff == NULL) {
  1637. fprintf(stderr, "tlog: malloc log buffer failed, %s\n", strerror(errno));
  1638. goto errout;
  1639. }
  1640. pthread_mutex_lock(&tlog.lock);
  1641. if (tlog.log == NULL) {
  1642. tlog.log = log;
  1643. } else {
  1644. log->next = tlog.log;
  1645. tlog.log = log;
  1646. }
  1647. pthread_mutex_unlock(&tlog.lock);
  1648. return log;
  1649. errout:
  1650. if (log) {
  1651. pthread_cond_destroy(&log->client_cond);
  1652. pthread_mutex_destroy(&log->lock);
  1653. free(log);
  1654. log = NULL;
  1655. }
  1656. return NULL;
  1657. }
  1658. void tlog_close(tlog_log *log)
  1659. {
  1660. if (log == NULL) {
  1661. return;
  1662. }
  1663. log->is_exit = 1;
  1664. }
  1665. void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
  1666. {
  1667. pthread_mutex_lock(&tlog.lock);
  1668. strncpy(log->pending_logfile, logfile, sizeof(log->pending_logfile) - 1);
  1669. pthread_mutex_unlock(&tlog.lock);
  1670. log->rename_pending = 1;
  1671. }
  1672. static void tlog_fork_prepare(void)
  1673. {
  1674. if (tlog.root == NULL) {
  1675. return;
  1676. }
  1677. pthread_mutex_lock(&tlog.lock);
  1678. tlog_log *next;
  1679. next = tlog.log;
  1680. while (next) {
  1681. next->multi_log = 1;
  1682. next = next->next;
  1683. }
  1684. }
  1685. static void tlog_fork_parent(void)
  1686. {
  1687. if (tlog.root == NULL) {
  1688. return;
  1689. }
  1690. pthread_mutex_unlock(&tlog.lock);
  1691. }
  1692. static void tlog_fork_child(void)
  1693. {
  1694. pthread_attr_t attr;
  1695. tlog_log *next;
  1696. if (tlog.root == NULL) {
  1697. return;
  1698. }
  1699. next = tlog.log;
  1700. while (next) {
  1701. next->start = 0;
  1702. next->end = 0;
  1703. next->ext_end = 0;
  1704. next->dropped = 0;
  1705. next->filesize = 0;
  1706. next = next->next;
  1707. }
  1708. pthread_attr_init(&attr);
  1709. int ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
  1710. if (ret != 0) {
  1711. fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
  1712. goto errout;
  1713. }
  1714. goto out;
  1715. errout:
  1716. next = tlog.log;
  1717. while (next) {
  1718. next->fail = 1;
  1719. next = next->next;
  1720. }
  1721. out:
  1722. pthread_mutex_unlock(&tlog.lock);
  1723. }
  1724. int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
  1725. {
  1726. pthread_attr_t attr;
  1727. int ret;
  1728. struct tlog_log *log = NULL;
  1729. if (tlog.root_format != NULL) {
  1730. fprintf(stderr, "tlog: already initialized.\n");
  1731. return -1;
  1732. }
  1733. if (buffsize > 0 && buffsize < TLOG_MAX_LINE_SIZE_SET * 2) {
  1734. fprintf(stderr, "tlog: buffer size is invalid.\n");
  1735. return -1;
  1736. }
  1737. memset(&tlog, 0, sizeof(tlog));
  1738. tlog.is_wait = 0;
  1739. _tlog_get_gzip_cmd_path();
  1740. pthread_attr_init(&attr);
  1741. pthread_cond_init(&tlog.cond, NULL);
  1742. pthread_mutex_init(&tlog.lock, NULL);
  1743. tlog.run = 1;
  1744. log = tlog_open(logfile, maxlogsize, maxlogcount, buffsize, flag);
  1745. if (log == NULL) {
  1746. fprintf(stderr, "tlog: init tlog root failed.\n");
  1747. goto errout;
  1748. }
  1749. _tlog_reg_output_func(log, _tlog_root_write_log);
  1750. if ((flag & TLOG_NOCOMPRESS) == 0 && tlog.gzip_cmd[0] == '\0') {
  1751. fprintf(stderr, "tlog: can not find gzip command, disable compress.\n");
  1752. }
  1753. tlog.output_no_prefix = ((flag & TLOG_FORMAT_NO_PREFIX) == 0) ? 0 : 1;
  1754. tlog.root = log;
  1755. tlog.root_format = _tlog_root_default_format;
  1756. ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
  1757. if (ret != 0) {
  1758. fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
  1759. goto errout;
  1760. }
  1761. if (flag & TLOG_SUPPORT_FORK) {
  1762. pthread_atfork(&tlog_fork_prepare, &tlog_fork_parent, &tlog_fork_child);
  1763. }
  1764. return 0;
  1765. errout:
  1766. if (tlog.tid) {
  1767. void *retval = NULL;
  1768. tlog.run = 0;
  1769. pthread_join(tlog.tid, &retval);
  1770. tlog.tid = 0;
  1771. }
  1772. pthread_cond_destroy(&tlog.cond);
  1773. pthread_mutex_destroy(&tlog.lock);
  1774. tlog.run = 0;
  1775. tlog.root = NULL;
  1776. tlog.root_format = NULL;
  1777. _tlog_close(log, 1);
  1778. return -1;
  1779. }
  1780. void tlog_exit(void)
  1781. {
  1782. if (tlog.root_format == NULL) {
  1783. return;
  1784. }
  1785. if (tlog.tid) {
  1786. void *ret = NULL;
  1787. tlog.run = 0;
  1788. pthread_mutex_lock(&tlog.lock);
  1789. pthread_cond_signal(&tlog.cond);
  1790. pthread_mutex_unlock(&tlog.lock);
  1791. pthread_join(tlog.tid, &ret);
  1792. tlog.tid = 0;
  1793. }
  1794. tlog.root = NULL;
  1795. while (tlog.log) {
  1796. _tlog_close(tlog.log, 1);
  1797. }
  1798. pthread_cond_destroy(&tlog.cond);
  1799. pthread_mutex_destroy(&tlog.lock);
  1800. tlog.root_format = NULL;
  1801. tlog.is_wait = 0;
  1802. }