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