log.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /** @file log.c
  2. * @brief Helpers for logging
  3. */
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif /* HAVE_CONFIG_H */
  7. #include <syslog.h>
  8. #include "faux/str.h"
  9. #include "faux/log.h"
  10. /** @brief Parses syslog facility string and returns the facility id.
  11. *
  12. * Gets syslog facility string, parse it and finds out the facility
  13. * id in digital form. Usefull config or command line options parsing.
  14. *
  15. * @param [in] str Facility string.
  16. * @param [out] facility Facility in digital form.
  17. * @returns 0 - success, < 0 - parsing error
  18. */
  19. int faux_log_facility(const char *str, int *facility) {
  20. if (faux_str_casecmp(str, "local0") == 0)
  21. *facility = LOG_LOCAL0;
  22. else if (faux_str_casecmp(str, "local1") == 0)
  23. *facility = LOG_LOCAL1;
  24. else if (faux_str_casecmp(str, "local2") == 0)
  25. *facility = LOG_LOCAL2;
  26. else if (faux_str_casecmp(str, "local3") == 0)
  27. *facility = LOG_LOCAL3;
  28. else if (faux_str_casecmp(str, "local4") == 0)
  29. *facility = LOG_LOCAL4;
  30. else if (faux_str_casecmp(str, "local5") == 0)
  31. *facility = LOG_LOCAL5;
  32. else if (faux_str_casecmp(str, "local6") == 0)
  33. *facility = LOG_LOCAL6;
  34. else if (faux_str_casecmp(str, "local7") == 0)
  35. *facility = LOG_LOCAL7;
  36. else if (faux_str_casecmp(str, "auth") == 0)
  37. *facility = LOG_AUTH;
  38. #ifdef LOG_AUTHPRIV
  39. else if (faux_str_casecmp(str, "authpriv") == 0)
  40. *facility = LOG_AUTHPRIV;
  41. #endif
  42. else if (faux_str_casecmp(str, "cron") == 0)
  43. *facility = LOG_CRON;
  44. else if (faux_str_casecmp(str, "daemon") == 0)
  45. *facility = LOG_DAEMON;
  46. #ifdef LOG_FTP
  47. else if (faux_str_casecmp(str, "ftp") == 0)
  48. *facility = LOG_FTP;
  49. #endif
  50. else if (faux_str_casecmp(str, "kern") == 0)
  51. *facility = LOG_KERN;
  52. else if (faux_str_casecmp(str, "lpr") == 0)
  53. *facility = LOG_LPR;
  54. else if (faux_str_casecmp(str, "mail") == 0)
  55. *facility = LOG_MAIL;
  56. else if (faux_str_casecmp(str, "news") == 0)
  57. *facility = LOG_NEWS;
  58. else if (faux_str_casecmp(str, "syslog") == 0)
  59. *facility = LOG_SYSLOG;
  60. else if (faux_str_casecmp(str, "user") == 0)
  61. *facility = LOG_USER;
  62. else if (faux_str_casecmp(str, "uucp") == 0)
  63. *facility = LOG_UUCP;
  64. else
  65. return -1;
  66. return 0;
  67. }