tlog.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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;
  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. memset(&empty_info, 0, sizeof(empty_info));
  1391. return tlog.output_func(&empty_info.info, buff, bufflen, tlog_get_private(log));
  1392. }
  1393. static void tlog_wait_zip_fini(void)
  1394. {
  1395. tlog_log *next;
  1396. if (tlog.root == NULL) {
  1397. return;
  1398. }
  1399. int wait_zip = 1;
  1400. int time_out = 0;
  1401. while (wait_zip) {
  1402. wait_zip = 0;
  1403. time_out++;
  1404. next = tlog.log;
  1405. while (next) {
  1406. if (next->zip_pid > 0 && wait_zip == 0) {
  1407. wait_zip = 1;
  1408. usleep(1000);
  1409. }
  1410. if (kill(next->zip_pid, 0) != 0 || time_out >= 5000) {
  1411. next->zip_pid = -1;
  1412. }
  1413. next = next->next;
  1414. }
  1415. }
  1416. return;
  1417. }
  1418. static void *_tlog_work(void *arg)
  1419. {
  1420. int log_len = 0;
  1421. int log_extlen = 0;
  1422. int log_end = 0;
  1423. int log_extend = 0;
  1424. int log_dropped = 0;
  1425. struct tlog_log *log = NULL;
  1426. struct tlog_log *loop_log = NULL;
  1427. void *unused __attribute__((unused));
  1428. unused = arg;
  1429. // for child process
  1430. tlog_wait_zip_fini();
  1431. while (1) {
  1432. log_len = 0;
  1433. log_extlen = 0;
  1434. log_extend = 0;
  1435. if (__atomic_load_n(&tlog.run, __ATOMIC_RELAXED) == 0) {
  1436. if (_tlog_any_has_data() == 0) {
  1437. break;
  1438. }
  1439. }
  1440. _tlog_wait_pids();
  1441. pthread_mutex_lock(&tlog.lock);
  1442. if (loop_log == NULL) {
  1443. loop_log = log;
  1444. }
  1445. log = _tlog_next_log(log);
  1446. if (log == NULL) {
  1447. pthread_mutex_unlock(&tlog.lock);
  1448. continue;
  1449. }
  1450. /* if buffer is empty, wait */
  1451. if (_tlog_any_has_data_locked() == 0 && __atomic_load_n(&tlog.run, __ATOMIC_RELAXED)) {
  1452. log = _tlog_wait_log_locked(log);
  1453. if (log == NULL) {
  1454. pthread_mutex_unlock(&tlog.lock);
  1455. if (errno != ETIMEDOUT && __atomic_load_n(&tlog.run, __ATOMIC_RELAXED)) {
  1456. sleep(1);
  1457. }
  1458. continue;
  1459. }
  1460. }
  1461. if (_tlog_has_data(log) == 0) {
  1462. if (log->is_exit) {
  1463. if (_tlog_close(log, 0) == 0) {
  1464. log = NULL;
  1465. loop_log = NULL;
  1466. };
  1467. }
  1468. pthread_mutex_unlock(&tlog.lock);
  1469. continue;
  1470. }
  1471. loop_log = NULL;
  1472. if (log->ext_end > 0) {
  1473. log_len = log->ext_end - log->start;
  1474. log_extend = log->ext_end;
  1475. }
  1476. if (log->end < log->start) {
  1477. log_extlen = log->end;
  1478. } else if (log->end > log->start) {
  1479. log_len = log->end - log->start;
  1480. }
  1481. log_end = log->end;
  1482. log_dropped = log->dropped;
  1483. log->dropped = 0;
  1484. pthread_mutex_unlock(&tlog.lock);
  1485. /* start write log work */
  1486. _tlog_work_write(log, log_len, log_extlen, log_dropped);
  1487. pthread_mutex_lock(&tlog.lock);
  1488. /* release finished buffer */
  1489. log->start = log_end;
  1490. if (log_extend > 0) {
  1491. log->ext_end = 0;
  1492. }
  1493. pthread_mutex_unlock(&tlog.lock);
  1494. _tlog_wakeup_waiters(log);
  1495. }
  1496. return NULL;
  1497. }
  1498. void tlog_set_early_printf(int enable, int no_prefix, int color)
  1499. {
  1500. tlog.early_print_disable = (enable == 0) ? 1 : 0;
  1501. tlog.early_print_no_prefix = (no_prefix == 0) ? 0 : 1;
  1502. tlog.early_print_color = (color == 0 || isatty(STDOUT_FILENO) == 0) ? 0 : 1;
  1503. }
  1504. void tlog_reg_early_printf_callback(tlog_early_print_func callback)
  1505. {
  1506. tlog.tlog_early_print = callback;
  1507. }
  1508. void tlog_reg_early_printf_output_callback(tlog_log_output_func callback, int log_screen, void *private_data)
  1509. {
  1510. tlog.early_print_output = callback;
  1511. tlog.early_print_userptr = private_data;
  1512. tlog.early_print_with_screen = (log_screen == 0) ? 0 : 1;
  1513. }
  1514. const char *tlog_get_level_string(tlog_level level)
  1515. {
  1516. if (level >= TLOG_END) {
  1517. return NULL;
  1518. }
  1519. return tlog_level_str[level];
  1520. }
  1521. void tlog_set_maxlog_count(int count)
  1522. {
  1523. tlog_logcount(tlog.root, count);
  1524. }
  1525. static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
  1526. {
  1527. if (log == NULL) {
  1528. return;
  1529. }
  1530. log->logscreen = (enable != 0) ? 1 : 0;
  1531. }
  1532. void tlog_setlogscreen(int enable)
  1533. {
  1534. _tlog_log_setlogscreen(tlog.root, enable);
  1535. }
  1536. int tlog_write_log(const char *buff, int bufflen)
  1537. {
  1538. if (unlikely(tlog.root == NULL)) {
  1539. return -1;
  1540. }
  1541. return _tlog_write(tlog.root, buff, bufflen);
  1542. }
  1543. void tlog_logscreen(tlog_log *log, int enable)
  1544. {
  1545. if (log == NULL) {
  1546. return;
  1547. }
  1548. _tlog_log_setlogscreen(log, enable);
  1549. }
  1550. static int _tlog_reg_output_func(tlog_log *log, tlog_output_func output)
  1551. {
  1552. if (log == NULL) {
  1553. return -1;
  1554. }
  1555. if (output == NULL) {
  1556. log->output_func = _tlog_write;
  1557. return 0;
  1558. }
  1559. log->output_func = output;
  1560. return 0;
  1561. }
  1562. int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
  1563. {
  1564. if (log == tlog.root) {
  1565. return -1;
  1566. }
  1567. int ret = _tlog_reg_output_func(log, output);
  1568. if (ret == 0) {
  1569. log->set_custom_output_func = 1;
  1570. }
  1571. return ret;
  1572. }
  1573. int tlog_reg_format_func(tlog_format_func callback)
  1574. {
  1575. tlog.root_format = callback;
  1576. return 0;
  1577. }
  1578. int tlog_reg_log_output_func(tlog_log_output_func output, void *private_data)
  1579. {
  1580. tlog.output_func = output;
  1581. tlog_set_private(tlog.root, private_data);
  1582. tlog.log->set_custom_output_func = 1;
  1583. return 0;
  1584. }
  1585. int tlog_setlevel(tlog_level level)
  1586. {
  1587. if (level >= TLOG_END) {
  1588. return -1;
  1589. }
  1590. tlog_set_level = level;
  1591. return 0;
  1592. }
  1593. int tlog_log_enabled(tlog_level level)
  1594. {
  1595. if (level >= TLOG_END) {
  1596. return 0;
  1597. }
  1598. return (tlog_set_level >= level) ? 1 : 0;
  1599. }
  1600. tlog_level tlog_getlevel(void)
  1601. {
  1602. return tlog_set_level;
  1603. }
  1604. void tlog_set_logfile(const char *logfile)
  1605. {
  1606. tlog_rename_logfile(tlog.root, logfile);
  1607. }
  1608. static void _tlog_get_gzip_cmd_path(void)
  1609. {
  1610. char *copy_path = NULL;
  1611. char gzip_cmd_path[PATH_MAX];
  1612. const char *env_path = getenv("PATH");
  1613. char *save_ptr = NULL;
  1614. if (env_path == NULL) {
  1615. env_path = "/bin:/usr/bin:/usr/local/bin";
  1616. }
  1617. copy_path = strdup(env_path);
  1618. if (copy_path == NULL) {
  1619. return;
  1620. }
  1621. for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(NULL, ":", &save_ptr)) {
  1622. snprintf(gzip_cmd_path, sizeof(gzip_cmd_path), "%s/gzip", tok);
  1623. if (access(gzip_cmd_path, X_OK) != 0) {
  1624. continue;
  1625. }
  1626. snprintf(tlog.gzip_cmd, sizeof(tlog.gzip_cmd), "%s", gzip_cmd_path);
  1627. break;
  1628. }
  1629. free(copy_path);
  1630. }
  1631. tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
  1632. {
  1633. struct tlog_log *log = NULL;
  1634. if (__atomic_load_n(&tlog.run, __ATOMIC_RELAXED) == 0) {
  1635. fprintf(stderr, "tlog: tlog is not initialized.\n");
  1636. return NULL;
  1637. }
  1638. log = (struct tlog_log *)malloc(sizeof(*log));
  1639. if (log == NULL) {
  1640. fprintf(stderr, "tlog: malloc log failed.\n");
  1641. return NULL;
  1642. }
  1643. memset(log, 0, sizeof(*log));
  1644. log->start = 0;
  1645. log->end = 0;
  1646. log->ext_end = 0;
  1647. log->dropped = 0;
  1648. log->buffsize = (buffsize > 0) ? buffsize : TLOG_BUFF_SIZE;
  1649. log->logsize = (maxlogsize >= 0) ? maxlogsize : TLOG_LOG_SIZE;
  1650. log->logcount = (maxlogcount <= 0) ? 0 : maxlogcount;
  1651. log->fd = -1;
  1652. log->filesize = 0;
  1653. log->zip_pid = -1;
  1654. log->is_exit = 0;
  1655. log->fail = 0;
  1656. log->print_errmsg = 1;
  1657. log->waiters = 0;
  1658. log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
  1659. log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;
  1660. log->logscreen = ((flag & TLOG_SCREEN) == 0) ? 0 : 1;
  1661. log->logscreen_color = ((flag & TLOG_SCREEN_COLOR) == 0 || isatty(STDOUT_FILENO) == 0) ? 0 : 1;
  1662. log->multi_log = ((flag & TLOG_MULTI_WRITE) == 0) ? 0 : 1;
  1663. log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1;
  1664. log->max_line_size = TLOG_MAX_LINE_LEN;
  1665. log->output_func = _tlog_write;
  1666. log->file_perm = S_IRUSR | S_IWUSR | S_IRGRP;
  1667. log->archive_perm = S_IRUSR | S_IRGRP;
  1668. if (log->nocompress == 0 && tlog.gzip_cmd[0] == '\0') {
  1669. log->nocompress = 1;
  1670. }
  1671. if (log->logscreen_color == 1) {
  1672. log->logscreen = 1;
  1673. log->segment_log = 1;
  1674. }
  1675. tlog_rename_logfile(log, logfile);
  1676. if (log->nocompress) {
  1677. strncpy(log->suffix, TLOG_SUFFIX_LOG, sizeof(log->suffix));
  1678. } else {
  1679. strncpy(log->suffix, TLOG_SUFFIX_GZ, sizeof(log->suffix));
  1680. }
  1681. log->buff = (char *)malloc(log->buffsize);
  1682. if (log->buff == NULL) {
  1683. fprintf(stderr, "tlog: malloc log buffer failed, %s\n", strerror(errno));
  1684. goto errout;
  1685. }
  1686. pthread_mutex_lock(&tlog.lock);
  1687. if (tlog.log == NULL) {
  1688. tlog.log = log;
  1689. } else {
  1690. log->next = tlog.log;
  1691. tlog.log = log;
  1692. }
  1693. pthread_mutex_unlock(&tlog.lock);
  1694. return log;
  1695. errout:
  1696. if (log) {
  1697. pthread_cond_destroy(&log->client_cond);
  1698. pthread_mutex_destroy(&log->lock);
  1699. free(log);
  1700. log = NULL;
  1701. }
  1702. return NULL;
  1703. }
  1704. void tlog_close(tlog_log *log)
  1705. {
  1706. if (log == NULL) {
  1707. return;
  1708. }
  1709. log->is_exit = 1;
  1710. }
  1711. void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
  1712. {
  1713. pthread_mutex_lock(&tlog.lock);
  1714. strncpy(log->pending_logfile, logfile, sizeof(log->pending_logfile) - 1);
  1715. pthread_mutex_unlock(&tlog.lock);
  1716. log->rename_pending = 1;
  1717. }
  1718. static void tlog_fork_prepare(void)
  1719. {
  1720. if (tlog.root == NULL) {
  1721. return;
  1722. }
  1723. pthread_mutex_lock(&tlog.lock);
  1724. tlog_log *next;
  1725. next = tlog.log;
  1726. while (next) {
  1727. next->multi_log = 1;
  1728. next = next->next;
  1729. }
  1730. }
  1731. static void tlog_fork_parent(void)
  1732. {
  1733. if (tlog.root == NULL) {
  1734. return;
  1735. }
  1736. pthread_mutex_unlock(&tlog.lock);
  1737. }
  1738. static void tlog_fork_child(void)
  1739. {
  1740. pthread_attr_t attr;
  1741. tlog_log *next;
  1742. if (tlog.root == NULL) {
  1743. return;
  1744. }
  1745. next = tlog.log;
  1746. while (next) {
  1747. next->start = 0;
  1748. next->end = 0;
  1749. next->ext_end = 0;
  1750. next->dropped = 0;
  1751. next->filesize = 0;
  1752. next = next->next;
  1753. }
  1754. pthread_attr_init(&attr);
  1755. int ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
  1756. if (ret != 0) {
  1757. fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
  1758. goto errout;
  1759. }
  1760. goto out;
  1761. errout:
  1762. next = tlog.log;
  1763. while (next) {
  1764. next->fail = 1;
  1765. next = next->next;
  1766. }
  1767. out:
  1768. pthread_mutex_unlock(&tlog.lock);
  1769. }
  1770. int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
  1771. {
  1772. pthread_attr_t attr;
  1773. int ret;
  1774. struct tlog_log *log = NULL;
  1775. if (tlog.root_format != NULL) {
  1776. fprintf(stderr, "tlog: already initialized.\n");
  1777. return -1;
  1778. }
  1779. if (buffsize > 0 && buffsize < TLOG_MAX_LINE_SIZE_SET * 2) {
  1780. fprintf(stderr, "tlog: buffer size is invalid.\n");
  1781. return -1;
  1782. }
  1783. memset(&tlog, 0, sizeof(tlog));
  1784. tlog.is_wait = 0;
  1785. _tlog_get_gzip_cmd_path();
  1786. pthread_attr_init(&attr);
  1787. pthread_cond_init(&tlog.cond, NULL);
  1788. pthread_mutex_init(&tlog.lock, NULL);
  1789. __atomic_store_n(&tlog.run, 1, __ATOMIC_RELAXED);
  1790. log = tlog_open(logfile, maxlogsize, maxlogcount, buffsize, flag);
  1791. if (log == NULL) {
  1792. fprintf(stderr, "tlog: init tlog root failed.\n");
  1793. goto errout;
  1794. }
  1795. _tlog_reg_output_func(log, _tlog_root_write_log);
  1796. if ((flag & TLOG_NOCOMPRESS) == 0 && tlog.gzip_cmd[0] == '\0') {
  1797. fprintf(stderr, "tlog: can not find gzip command, disable compress.\n");
  1798. }
  1799. tlog.output_no_prefix = ((flag & TLOG_FORMAT_NO_PREFIX) == 0) ? 0 : 1;
  1800. tlog.root = log;
  1801. tlog.root_format = _tlog_root_default_format;
  1802. ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
  1803. if (ret != 0) {
  1804. fprintf(stderr, "tlog: create tlog work thread failed, %s\n", strerror(errno));
  1805. goto errout;
  1806. }
  1807. if (flag & TLOG_SUPPORT_FORK) {
  1808. pthread_atfork(&tlog_fork_prepare, &tlog_fork_parent, &tlog_fork_child);
  1809. }
  1810. return 0;
  1811. errout:
  1812. if (tlog.tid) {
  1813. void *retval = NULL;
  1814. __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
  1815. pthread_join(tlog.tid, &retval);
  1816. tlog.tid = 0;
  1817. }
  1818. pthread_cond_destroy(&tlog.cond);
  1819. pthread_mutex_destroy(&tlog.lock);
  1820. __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
  1821. tlog.root = NULL;
  1822. tlog.root_format = NULL;
  1823. _tlog_close(log, 1);
  1824. return -1;
  1825. }
  1826. void tlog_exit(void)
  1827. {
  1828. if (tlog.root_format == NULL) {
  1829. return;
  1830. }
  1831. if (tlog.tid) {
  1832. void *ret = NULL;
  1833. __atomic_store_n(&tlog.run, 0, __ATOMIC_RELAXED);
  1834. pthread_mutex_lock(&tlog.lock);
  1835. pthread_cond_signal(&tlog.cond);
  1836. pthread_mutex_unlock(&tlog.lock);
  1837. pthread_join(tlog.tid, &ret);
  1838. tlog.tid = 0;
  1839. }
  1840. tlog.root = NULL;
  1841. while (tlog.log) {
  1842. _tlog_close(tlog.log, 1);
  1843. }
  1844. pthread_cond_destroy(&tlog.cond);
  1845. pthread_mutex_destroy(&tlog.lock);
  1846. tlog.root_format = NULL;
  1847. tlog.is_wait = 0;
  1848. }