tlog.c 47 KB

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