log.c 1.5 KB

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