log.c 22 KB

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