log.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <daemon/main.h>
  3. #include "../libnetdata.h"
  4. int web_server_is_multithreaded = 1;
  5. const char *program_name = "";
  6. uint64_t debug_flags = 0;
  7. int access_log_syslog = 1;
  8. int error_log_syslog = 1;
  9. int output_log_syslog = 1; // debug log
  10. int stdaccess_fd = -1;
  11. FILE *stdaccess = NULL;
  12. const char *stdaccess_filename = NULL;
  13. const char *stderr_filename = NULL;
  14. const char *stdout_filename = NULL;
  15. const char *facility_log = NULL;
  16. #ifdef ENABLE_ACLK
  17. const char *aclklog_filename = NULL;
  18. int aclklog_fd = -1;
  19. FILE *aclklog = NULL;
  20. int aclklog_syslog = 1;
  21. int aclklog_enabled = 0;
  22. #endif
  23. // ----------------------------------------------------------------------------
  24. // Log facility(https://tools.ietf.org/html/rfc5424)
  25. //
  26. // The facilities accepted in the Netdata are in according with the following
  27. // header files for their respective operating system:
  28. // sys/syslog.h (Linux )
  29. // sys/sys/syslog.h (FreeBSD)
  30. // bsd/sys/syslog.h (darwin-xnu)
  31. #define LOG_AUTH_KEY "auth"
  32. #define LOG_AUTHPRIV_KEY "authpriv"
  33. #ifdef __FreeBSD__
  34. # define LOG_CONSOLE_KEY "console"
  35. #endif
  36. #define LOG_CRON_KEY "cron"
  37. #define LOG_DAEMON_KEY "daemon"
  38. #define LOG_FTP_KEY "ftp"
  39. #ifdef __APPLE__
  40. # define LOG_INSTALL_KEY "install"
  41. #endif
  42. #define LOG_KERN_KEY "kern"
  43. #define LOG_LPR_KEY "lpr"
  44. #define LOG_MAIL_KEY "mail"
  45. //#define LOG_INTERNAL_MARK_KEY "mark"
  46. #ifdef __APPLE__
  47. # define LOG_NETINFO_KEY "netinfo"
  48. # define LOG_RAS_KEY "ras"
  49. # define LOG_REMOTEAUTH_KEY "remoteauth"
  50. #endif
  51. #define LOG_NEWS_KEY "news"
  52. #ifdef __FreeBSD__
  53. # define LOG_NTP_KEY "ntp"
  54. #endif
  55. #define LOG_SECURITY_KEY "security"
  56. #define LOG_SYSLOG_KEY "syslog"
  57. #define LOG_USER_KEY "user"
  58. #define LOG_UUCP_KEY "uucp"
  59. #ifdef __APPLE__
  60. # define LOG_LAUNCHD_KEY "launchd"
  61. #endif
  62. #define LOG_LOCAL0_KEY "local0"
  63. #define LOG_LOCAL1_KEY "local1"
  64. #define LOG_LOCAL2_KEY "local2"
  65. #define LOG_LOCAL3_KEY "local3"
  66. #define LOG_LOCAL4_KEY "local4"
  67. #define LOG_LOCAL5_KEY "local5"
  68. #define LOG_LOCAL6_KEY "local6"
  69. #define LOG_LOCAL7_KEY "local7"
  70. static int log_facility_id(const char *facility_name)
  71. {
  72. static int
  73. hash_auth = 0,
  74. hash_authpriv = 0,
  75. #ifdef __FreeBSD__
  76. hash_console = 0,
  77. #endif
  78. hash_cron = 0,
  79. hash_daemon = 0,
  80. hash_ftp = 0,
  81. #ifdef __APPLE__
  82. hash_install = 0,
  83. #endif
  84. hash_kern = 0,
  85. hash_lpr = 0,
  86. hash_mail = 0,
  87. // hash_mark = 0,
  88. #ifdef __APPLE__
  89. hash_netinfo = 0,
  90. hash_ras = 0,
  91. hash_remoteauth = 0,
  92. #endif
  93. hash_news = 0,
  94. #ifdef __FreeBSD__
  95. hash_ntp = 0,
  96. #endif
  97. hash_security = 0,
  98. hash_syslog = 0,
  99. hash_user = 0,
  100. hash_uucp = 0,
  101. #ifdef __APPLE__
  102. hash_launchd = 0,
  103. #endif
  104. hash_local0 = 0,
  105. hash_local1 = 0,
  106. hash_local2 = 0,
  107. hash_local3 = 0,
  108. hash_local4 = 0,
  109. hash_local5 = 0,
  110. hash_local6 = 0,
  111. hash_local7 = 0;
  112. if(unlikely(!hash_auth))
  113. {
  114. hash_auth = simple_hash(LOG_AUTH_KEY);
  115. hash_authpriv = simple_hash(LOG_AUTHPRIV_KEY);
  116. #ifdef __FreeBSD__
  117. hash_console = simple_hash(LOG_CONSOLE_KEY);
  118. #endif
  119. hash_cron = simple_hash(LOG_CRON_KEY);
  120. hash_daemon = simple_hash(LOG_DAEMON_KEY);
  121. hash_ftp = simple_hash(LOG_FTP_KEY);
  122. #ifdef __APPLE__
  123. hash_install = simple_hash(LOG_INSTALL_KEY);
  124. #endif
  125. hash_kern = simple_hash(LOG_KERN_KEY);
  126. hash_lpr = simple_hash(LOG_LPR_KEY);
  127. hash_mail = simple_hash(LOG_MAIL_KEY);
  128. // hash_mark = simple_uhash();
  129. #ifdef __APPLE__
  130. hash_netinfo = simple_hash(LOG_NETINFO_KEY);
  131. hash_ras = simple_hash(LOG_RAS_KEY);
  132. hash_remoteauth = simple_hash(LOG_REMOTEAUTH_KEY);
  133. #endif
  134. hash_news = simple_hash(LOG_NEWS_KEY);
  135. #ifdef __FreeBSD__
  136. hash_ntp = simple_hash(LOG_NTP_KEY);
  137. #endif
  138. hash_security = simple_hash(LOG_SECURITY_KEY);
  139. hash_syslog = simple_hash(LOG_SYSLOG_KEY);
  140. hash_user = simple_hash(LOG_USER_KEY);
  141. hash_uucp = simple_hash(LOG_UUCP_KEY);
  142. #ifdef __APPLE__
  143. hash_launchd = simple_hash(LOG_LAUNCHD_KEY);
  144. #endif
  145. hash_local0 = simple_hash(LOG_LOCAL0_KEY);
  146. hash_local1 = simple_hash(LOG_LOCAL1_KEY);
  147. hash_local2 = simple_hash(LOG_LOCAL2_KEY);
  148. hash_local3 = simple_hash(LOG_LOCAL3_KEY);
  149. hash_local4 = simple_hash(LOG_LOCAL4_KEY);
  150. hash_local5 = simple_hash(LOG_LOCAL5_KEY);
  151. hash_local6 = simple_hash(LOG_LOCAL6_KEY);
  152. hash_local7 = simple_hash(LOG_LOCAL7_KEY);
  153. }
  154. int hash = simple_hash(facility_name);
  155. if ( hash == hash_auth )
  156. {
  157. return LOG_AUTH;
  158. }
  159. else if ( hash == hash_authpriv )
  160. {
  161. return LOG_AUTHPRIV;
  162. }
  163. #ifdef __FreeBSD__
  164. else if ( hash == hash_console )
  165. {
  166. return LOG_CONSOLE;
  167. }
  168. #endif
  169. else if ( hash == hash_cron )
  170. {
  171. return LOG_CRON;
  172. }
  173. else if ( hash == hash_daemon )
  174. {
  175. return LOG_DAEMON;
  176. }
  177. else if ( hash == hash_ftp )
  178. {
  179. return LOG_FTP;
  180. }
  181. #ifdef __APPLE__
  182. else if ( hash == hash_install )
  183. {
  184. return LOG_INSTALL;
  185. }
  186. #endif
  187. else if ( hash == hash_kern )
  188. {
  189. return LOG_KERN;
  190. }
  191. else if ( hash == hash_lpr )
  192. {
  193. return LOG_LPR;
  194. }
  195. else if ( hash == hash_mail )
  196. {
  197. return LOG_MAIL;
  198. }
  199. /*
  200. else if ( hash == hash_mark )
  201. {
  202. //this is internal for all OS
  203. return INTERNAL_MARK;
  204. }
  205. */
  206. #ifdef __APPLE__
  207. else if ( hash == hash_netinfo )
  208. {
  209. return LOG_NETINFO;
  210. }
  211. else if ( hash == hash_ras )
  212. {
  213. return LOG_RAS;
  214. }
  215. else if ( hash == hash_remoteauth )
  216. {
  217. return LOG_REMOTEAUTH;
  218. }
  219. #endif
  220. else if ( hash == hash_news )
  221. {
  222. return LOG_NEWS;
  223. }
  224. #ifdef __FreeBSD__
  225. else if ( hash == hash_ntp )
  226. {
  227. return LOG_NTP;
  228. }
  229. #endif
  230. else if ( hash == hash_security )
  231. {
  232. //FreeBSD is the unique that does not consider
  233. //this facility deprecated. We are keeping
  234. //it for other OS while they are kept in their headers.
  235. #ifdef __FreeBSD__
  236. return LOG_SECURITY;
  237. #else
  238. return LOG_AUTH;
  239. #endif
  240. }
  241. else if ( hash == hash_syslog )
  242. {
  243. return LOG_SYSLOG;
  244. }
  245. else if ( hash == hash_user )
  246. {
  247. return LOG_USER;
  248. }
  249. else if ( hash == hash_uucp )
  250. {
  251. return LOG_UUCP;
  252. }
  253. else if ( hash == hash_local0 )
  254. {
  255. return LOG_LOCAL0;
  256. }
  257. else if ( hash == hash_local1 )
  258. {
  259. return LOG_LOCAL1;
  260. }
  261. else if ( hash == hash_local2 )
  262. {
  263. return LOG_LOCAL2;
  264. }
  265. else if ( hash == hash_local3 )
  266. {
  267. return LOG_LOCAL3;
  268. }
  269. else if ( hash == hash_local4 )
  270. {
  271. return LOG_LOCAL4;
  272. }
  273. else if ( hash == hash_local5 )
  274. {
  275. return LOG_LOCAL5;
  276. }
  277. else if ( hash == hash_local6 )
  278. {
  279. return LOG_LOCAL6;
  280. }
  281. else if ( hash == hash_local7 )
  282. {
  283. return LOG_LOCAL7;
  284. }
  285. #ifdef __APPLE__
  286. else if ( hash == hash_launchd )
  287. {
  288. return LOG_LAUNCHD;
  289. }
  290. #endif
  291. return LOG_DAEMON;
  292. }
  293. //we do not need to use this now, but I already created this function to be
  294. //used case necessary.
  295. /*
  296. char *log_facility_name(int code)
  297. {
  298. char *defvalue = { "daemon" };
  299. switch(code)
  300. {
  301. case LOG_AUTH:
  302. {
  303. return "auth";
  304. }
  305. case LOG_AUTHPRIV:
  306. {
  307. return "authpriv";
  308. }
  309. #ifdef __FreeBSD__
  310. case LOG_CONSOLE:
  311. {
  312. return "console";
  313. }
  314. #endif
  315. case LOG_CRON:
  316. {
  317. return "cron";
  318. }
  319. case LOG_DAEMON:
  320. {
  321. return defvalue;
  322. }
  323. case LOG_FTP:
  324. {
  325. return "ftp";
  326. }
  327. #ifdef __APPLE__
  328. case LOG_INSTALL:
  329. {
  330. return "install";
  331. }
  332. #endif
  333. case LOG_KERN:
  334. {
  335. return "kern";
  336. }
  337. case LOG_LPR:
  338. {
  339. return "lpr";
  340. }
  341. case LOG_MAIL:
  342. {
  343. return "mail";
  344. }
  345. #ifdef __APPLE__
  346. case LOG_NETINFO:
  347. {
  348. return "netinfo" ;
  349. }
  350. case LOG_RAS:
  351. {
  352. return "ras";
  353. }
  354. case LOG_REMOTEAUTH:
  355. {
  356. return "remoteauth";
  357. }
  358. #endif
  359. case LOG_NEWS:
  360. {
  361. return "news";
  362. }
  363. #ifdef __FreeBSD__
  364. case LOG_NTP:
  365. {
  366. return "ntp" ;
  367. }
  368. case LOG_SECURITY:
  369. {
  370. return "security";
  371. }
  372. #endif
  373. case LOG_SYSLOG:
  374. {
  375. return "syslog";
  376. }
  377. case LOG_USER:
  378. {
  379. return "user";
  380. }
  381. case LOG_UUCP:
  382. {
  383. return "uucp";
  384. }
  385. case LOG_LOCAL0:
  386. {
  387. return "local0";
  388. }
  389. case LOG_LOCAL1:
  390. {
  391. return "local1";
  392. }
  393. case LOG_LOCAL2:
  394. {
  395. return "local2";
  396. }
  397. case LOG_LOCAL3:
  398. {
  399. return "local3";
  400. }
  401. case LOG_LOCAL4:
  402. {
  403. return "local4" ;
  404. }
  405. case LOG_LOCAL5:
  406. {
  407. return "local5";
  408. }
  409. case LOG_LOCAL6:
  410. {
  411. return "local6";
  412. }
  413. case LOG_LOCAL7:
  414. {
  415. return "local7" ;
  416. }
  417. #ifdef __APPLE__
  418. case LOG_LAUNCHD:
  419. {
  420. return "launchd";
  421. }
  422. #endif
  423. }
  424. return defvalue;
  425. }
  426. */
  427. // ----------------------------------------------------------------------------
  428. void syslog_init() {
  429. static int i = 0;
  430. if(!i) {
  431. openlog(program_name, LOG_PID,log_facility_id(facility_log));
  432. i = 1;
  433. }
  434. }
  435. #define LOG_DATE_LENGTH 26
  436. static inline void log_date(char *buffer, size_t len) {
  437. if(unlikely(!buffer || !len))
  438. return;
  439. time_t t;
  440. struct tm *tmp, tmbuf;
  441. t = now_realtime_sec();
  442. tmp = localtime_r(&t, &tmbuf);
  443. if (tmp == NULL) {
  444. buffer[0] = '\0';
  445. return;
  446. }
  447. if (unlikely(strftime(buffer, len, "%Y-%m-%d %H:%M:%S", tmp) == 0))
  448. buffer[0] = '\0';
  449. buffer[len - 1] = '\0';
  450. }
  451. static netdata_mutex_t log_mutex = NETDATA_MUTEX_INITIALIZER;
  452. static inline void log_lock() {
  453. netdata_mutex_lock(&log_mutex);
  454. }
  455. static inline void log_unlock() {
  456. netdata_mutex_unlock(&log_mutex);
  457. }
  458. static FILE *open_log_file(int fd, FILE *fp, const char *filename, int *enabled_syslog, int is_stdaccess, int *fd_ptr) {
  459. int f, devnull = 0;
  460. if(!filename || !*filename || !strcmp(filename, "none") || !strcmp(filename, "/dev/null")) {
  461. filename = "/dev/null";
  462. devnull = 1;
  463. }
  464. if(!strcmp(filename, "syslog")) {
  465. filename = "/dev/null";
  466. devnull = 1;
  467. syslog_init();
  468. if(enabled_syslog) *enabled_syslog = 1;
  469. }
  470. else if(enabled_syslog) *enabled_syslog = 0;
  471. // don't do anything if the user is willing
  472. // to have the standard one
  473. if(!strcmp(filename, "system")) {
  474. if(fd != -1 && !is_stdaccess) {
  475. if(fd_ptr) *fd_ptr = fd;
  476. return fp;
  477. }
  478. filename = "stderr";
  479. }
  480. if(!strcmp(filename, "stdout"))
  481. f = STDOUT_FILENO;
  482. else if(!strcmp(filename, "stderr"))
  483. f = STDERR_FILENO;
  484. else {
  485. f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
  486. if(f == -1) {
  487. error("Cannot open file '%s'. Leaving %d to its default.", filename, fd);
  488. if(fd_ptr) *fd_ptr = fd;
  489. return fp;
  490. }
  491. }
  492. // if there is a level-2 file pointer
  493. // flush it before switching the level-1 fds
  494. if(fp)
  495. fflush(fp);
  496. if(devnull && is_stdaccess) {
  497. fd = -1;
  498. fp = NULL;
  499. }
  500. if(fd != f && fd != -1) {
  501. // it automatically closes
  502. int t = dup2(f, fd);
  503. if (t == -1) {
  504. error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
  505. close(f);
  506. if(fd_ptr) *fd_ptr = fd;
  507. return fp;
  508. }
  509. // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
  510. close(f);
  511. }
  512. else fd = f;
  513. if(!fp) {
  514. fp = fdopen(fd, "a");
  515. if (!fp)
  516. error("Cannot fdopen() fd %d ('%s')", fd, filename);
  517. else {
  518. if (setvbuf(fp, NULL, _IOLBF, 0) != 0)
  519. error("Cannot set line buffering on fd %d ('%s')", fd, filename);
  520. }
  521. }
  522. if(fd_ptr) *fd_ptr = fd;
  523. return fp;
  524. }
  525. void reopen_all_log_files() {
  526. if(stdout_filename)
  527. open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
  528. if(stderr_filename)
  529. open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL);
  530. #ifdef ENABLE_ACLK
  531. if (aclklog_enabled)
  532. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  533. #endif
  534. if(stdaccess_filename)
  535. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  536. }
  537. void open_all_log_files() {
  538. // disable stdin
  539. open_log_file(STDIN_FILENO, stdin, "/dev/null", NULL, 0, NULL);
  540. open_log_file(STDOUT_FILENO, stdout, stdout_filename, &output_log_syslog, 0, NULL);
  541. open_log_file(STDERR_FILENO, stderr, stderr_filename, &error_log_syslog, 0, NULL);
  542. #ifdef ENABLE_ACLK
  543. if(aclklog_enabled)
  544. aclklog = open_log_file(aclklog_fd, aclklog, aclklog_filename, NULL, 0, &aclklog_fd);
  545. #endif
  546. stdaccess = open_log_file(stdaccess_fd, stdaccess, stdaccess_filename, &access_log_syslog, 1, &stdaccess_fd);
  547. }
  548. // ----------------------------------------------------------------------------
  549. // error log throttling
  550. time_t error_log_throttle_period = 1200;
  551. unsigned long error_log_errors_per_period = 200;
  552. unsigned long error_log_errors_per_period_backup = 0;
  553. int error_log_limit(int reset) {
  554. static time_t start = 0;
  555. static unsigned long counter = 0, prevented = 0;
  556. // fprintf(stderr, "FLOOD: counter=%lu, allowed=%lu, backup=%lu, period=%llu\n", counter, error_log_errors_per_period, error_log_errors_per_period_backup, (unsigned long long)error_log_throttle_period);
  557. // do not throttle if the period is 0
  558. if(error_log_throttle_period == 0)
  559. return 0;
  560. // prevent all logs if the errors per period is 0
  561. if(error_log_errors_per_period == 0)
  562. #ifdef NETDATA_INTERNAL_CHECKS
  563. return 0;
  564. #else
  565. return 1;
  566. #endif
  567. time_t now = now_monotonic_sec();
  568. if(!start) start = now;
  569. if(reset) {
  570. if(prevented) {
  571. char date[LOG_DATE_LENGTH];
  572. log_date(date, LOG_DATE_LENGTH);
  573. fprintf(
  574. stderr,
  575. "%s: %s LOG FLOOD PROTECTION reset for process '%s' "
  576. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  577. date,
  578. program_name,
  579. program_name,
  580. prevented,
  581. (int64_t)(now - start));
  582. }
  583. start = now;
  584. counter = 0;
  585. prevented = 0;
  586. }
  587. // detect if we log too much
  588. counter++;
  589. if(now - start > error_log_throttle_period) {
  590. if(prevented) {
  591. char date[LOG_DATE_LENGTH];
  592. log_date(date, LOG_DATE_LENGTH);
  593. fprintf(
  594. stderr,
  595. "%s: %s LOG FLOOD PROTECTION resuming logging from process '%s' "
  596. "(prevented %lu logs in the last %"PRId64" seconds).\n",
  597. date,
  598. program_name,
  599. program_name,
  600. prevented,
  601. (int64_t)error_log_throttle_period);
  602. }
  603. // restart the period accounting
  604. start = now;
  605. counter = 1;
  606. prevented = 0;
  607. // log this error
  608. return 0;
  609. }
  610. if(counter > error_log_errors_per_period) {
  611. if(!prevented) {
  612. char date[LOG_DATE_LENGTH];
  613. log_date(date, LOG_DATE_LENGTH);
  614. fprintf(
  615. stderr,
  616. "%s: %s LOG FLOOD PROTECTION too many logs (%lu logs in %"PRId64" seconds, threshold is set to %lu logs "
  617. "in %"PRId64" seconds). Preventing more logs from process '%s' for %"PRId64" seconds.\n",
  618. date,
  619. program_name,
  620. counter,
  621. (int64_t)(now - start),
  622. error_log_errors_per_period,
  623. (int64_t)error_log_throttle_period,
  624. program_name,
  625. (int64_t)(start + error_log_throttle_period - now));
  626. }
  627. prevented++;
  628. // prevent logging this error
  629. #ifdef NETDATA_INTERNAL_CHECKS
  630. return 0;
  631. #else
  632. return 1;
  633. #endif
  634. }
  635. return 0;
  636. }
  637. void error_log_limit_reset(void) {
  638. log_lock();
  639. error_log_errors_per_period = error_log_errors_per_period_backup;
  640. error_log_limit(1);
  641. log_unlock();
  642. }
  643. void error_log_limit_unlimited(void) {
  644. log_lock();
  645. error_log_errors_per_period = error_log_errors_per_period_backup;
  646. error_log_limit(1);
  647. error_log_errors_per_period = ((error_log_errors_per_period_backup * 10) < 10000) ? 10000 : (error_log_errors_per_period_backup * 10);
  648. log_unlock();
  649. }
  650. // ----------------------------------------------------------------------------
  651. // debug log
  652. void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  653. va_list args;
  654. char date[LOG_DATE_LENGTH];
  655. log_date(date, LOG_DATE_LENGTH);
  656. va_start( args, fmt );
  657. printf("%s: %s DEBUG : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
  658. vprintf(fmt, args);
  659. va_end( args );
  660. putchar('\n');
  661. if(output_log_syslog) {
  662. va_start( args, fmt );
  663. vsyslog(LOG_ERR, fmt, args );
  664. va_end( args );
  665. }
  666. fflush(stdout);
  667. }
  668. // ----------------------------------------------------------------------------
  669. // info log
  670. void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
  671. {
  672. va_list args;
  673. log_lock();
  674. // prevent logging too much
  675. if (error_log_limit(0)) {
  676. log_unlock();
  677. return;
  678. }
  679. if(error_log_syslog) {
  680. va_start( args, fmt );
  681. vsyslog(LOG_INFO, fmt, args );
  682. va_end( args );
  683. }
  684. char date[LOG_DATE_LENGTH];
  685. log_date(date, LOG_DATE_LENGTH);
  686. va_start( args, fmt );
  687. #ifdef NETDATA_INTERNAL_CHECKS
  688. fprintf(stderr, "%s: %s INFO : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, netdata_thread_tag(), line, file, function);
  689. #else
  690. fprintf(stderr, "%s: %s INFO : %s : ", date, program_name, netdata_thread_tag());
  691. #endif
  692. vfprintf( stderr, fmt, args );
  693. va_end( args );
  694. fputc('\n', stderr);
  695. log_unlock();
  696. }
  697. // ----------------------------------------------------------------------------
  698. // error log
  699. #if defined(STRERROR_R_CHAR_P)
  700. // GLIBC version of strerror_r
  701. static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
  702. #elif defined(HAVE_STRERROR_R)
  703. // POSIX version of strerror_r
  704. static const char *strerror_result(int a, const char *b) { (void)a; return b; }
  705. #elif defined(HAVE_C__GENERIC)
  706. // what a trick!
  707. // http://stackoverflow.com/questions/479207/function-overloading-in-c
  708. static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
  709. static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
  710. #define strerror_result(a, b) _Generic((a), \
  711. int: strerror_result_int, \
  712. char *: strerror_result_string \
  713. )(a, b)
  714. #else
  715. #error "cannot detect the format of function strerror_r()"
  716. #endif
  717. void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  718. // save a copy of errno - just in case this function generates a new error
  719. int __errno = errno;
  720. va_list args;
  721. log_lock();
  722. // prevent logging too much
  723. if (error_log_limit(0)) {
  724. log_unlock();
  725. return;
  726. }
  727. if(error_log_syslog) {
  728. va_start( args, fmt );
  729. vsyslog(LOG_ERR, fmt, args );
  730. va_end( args );
  731. }
  732. char date[LOG_DATE_LENGTH];
  733. log_date(date, LOG_DATE_LENGTH);
  734. va_start( args, fmt );
  735. #ifdef NETDATA_INTERNAL_CHECKS
  736. fprintf(stderr, "%s: %s %-5.5s : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, prefix, netdata_thread_tag(), line, file, function);
  737. #else
  738. fprintf(stderr, "%s: %s %-5.5s : %s : ", date, program_name, prefix, netdata_thread_tag());
  739. #endif
  740. vfprintf( stderr, fmt, args );
  741. va_end( args );
  742. if(__errno) {
  743. char buf[1024];
  744. fprintf(stderr, " (errno %d, %s)\n", __errno, strerror_result(strerror_r(__errno, buf, 1023), buf));
  745. errno = 0;
  746. }
  747. else
  748. fputc('\n', stderr);
  749. log_unlock();
  750. }
  751. void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... ) {
  752. // save a copy of errno - just in case this function generates a new error
  753. int __errno = errno;
  754. va_list args;
  755. const char *thread_tag;
  756. char os_threadname[NETDATA_THREAD_NAME_MAX + 1];
  757. if(error_log_syslog) {
  758. va_start( args, fmt );
  759. vsyslog(LOG_CRIT, fmt, args );
  760. va_end( args );
  761. }
  762. thread_tag = netdata_thread_tag();
  763. if (!netdata_thread_tag_exists()) {
  764. os_thread_get_current_name_np(os_threadname);
  765. if ('\0' != os_threadname[0]) { /* If it is not an empty string replace "MAIN" thread_tag */
  766. thread_tag = os_threadname;
  767. }
  768. }
  769. char date[LOG_DATE_LENGTH];
  770. log_date(date, LOG_DATE_LENGTH);
  771. log_lock();
  772. va_start( args, fmt );
  773. #ifdef NETDATA_INTERNAL_CHECKS
  774. fprintf(stderr, "%s: %s FATAL : %s : (%04lu@%-20.20s:%-15.15s): ", date, program_name, thread_tag, line, file, function);
  775. #else
  776. fprintf(stderr, "%s: %s FATAL : %s : ", date, program_name, thread_tag);
  777. #endif
  778. vfprintf( stderr, fmt, args );
  779. va_end( args );
  780. perror(" # ");
  781. fputc('\n', stderr);
  782. log_unlock();
  783. char action_data[70+1];
  784. snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, __errno);
  785. char action_result[60+1];
  786. snprintfz(action_result, 60, "%s:%s", program_name, strncmp(thread_tag, "STREAM_RECEIVER", strlen("STREAM_RECEIVER")) ? thread_tag : "[x]");
  787. send_statistics("FATAL", action_result, action_data);
  788. netdata_cleanup_and_exit(1);
  789. }
  790. // ----------------------------------------------------------------------------
  791. // access log
  792. void log_access( const char *fmt, ... ) {
  793. va_list args;
  794. if(access_log_syslog) {
  795. va_start( args, fmt );
  796. vsyslog(LOG_INFO, fmt, args );
  797. va_end( args );
  798. }
  799. if(stdaccess) {
  800. static netdata_mutex_t access_mutex = NETDATA_MUTEX_INITIALIZER;
  801. if(web_server_is_multithreaded)
  802. netdata_mutex_lock(&access_mutex);
  803. char date[LOG_DATE_LENGTH];
  804. log_date(date, LOG_DATE_LENGTH);
  805. fprintf(stdaccess, "%s: ", date);
  806. va_start( args, fmt );
  807. vfprintf( stdaccess, fmt, args );
  808. va_end( args );
  809. fputc('\n', stdaccess);
  810. if(web_server_is_multithreaded)
  811. netdata_mutex_unlock(&access_mutex);
  812. }
  813. }
  814. #ifdef ENABLE_ACLK
  815. void log_aclk_message_bin( const char *data, const size_t data_len, int tx, const char *mqtt_topic, const char *message_name) {
  816. if (aclklog) {
  817. static netdata_mutex_t aclklog_mutex = NETDATA_MUTEX_INITIALIZER;
  818. netdata_mutex_lock(&aclklog_mutex);
  819. char date[LOG_DATE_LENGTH];
  820. log_date(date, LOG_DATE_LENGTH);
  821. fprintf(aclklog, "%s: %s Msg:\"%s\", MQTT-topic:\"%s\": ", date, tx ? "OUTGOING" : "INCOMING", message_name, mqtt_topic);
  822. fwrite(data, data_len, 1, aclklog);
  823. fputc('\n', aclklog);
  824. netdata_mutex_unlock(&aclklog_mutex);
  825. }
  826. }
  827. #endif