tlog.c 53 KB

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