mono-log-posix.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * \file
  3. * POSIX interface to the logger
  4. *
  5. * This module contains the POSIX syslog logger routines
  6. *
  7. * Author:
  8. * Neale Ferguson <neale@sinenomine.net>
  9. *
  10. */
  11. #include <config.h>
  12. #ifdef HAVE_UNISTD_H
  13. #include <unistd.h>
  14. #endif
  15. #if defined(_POSIX_VERSION)
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <glib.h>
  21. #include <syslog.h>
  22. #include <stdarg.h>
  23. #include <errno.h>
  24. #include <time.h>
  25. #include <sys/time.h>
  26. #include "mono-logger-internals.h"
  27. static void *logUserData = NULL;
  28. /**
  29. * mapSyslogLevel:
  30. *
  31. * @level - GLogLevelFlags value
  32. * @returns The equivalent syslog priority value
  33. */
  34. static __inline__ int
  35. mapSyslogLevel(GLogLevelFlags level)
  36. {
  37. if (level & G_LOG_LEVEL_ERROR)
  38. return (LOG_ERR);
  39. if (level & G_LOG_LEVEL_CRITICAL)
  40. return (LOG_CRIT);
  41. if (level & G_LOG_LEVEL_WARNING)
  42. return (LOG_WARNING);
  43. if (level & G_LOG_LEVEL_MESSAGE)
  44. return (LOG_NOTICE);
  45. if (level & G_LOG_LEVEL_INFO)
  46. return (LOG_INFO);
  47. if (level & G_LOG_LEVEL_DEBUG)
  48. return (LOG_DEBUG);
  49. return (LOG_INFO);
  50. }
  51. /**
  52. * mono_log_open_syslog:
  53. * \param ident Identifier: ignored
  54. * \param userData Not used
  55. * Open the syslog interface specifying that we want our PID recorded
  56. * and that we're using the \c LOG_USER facility.
  57. */
  58. void
  59. mono_log_open_syslog(const char *ident, void *userData)
  60. {
  61. #ifdef HAVE_OPENLOG
  62. openlog("mono", LOG_PID, LOG_USER);
  63. #endif
  64. logUserData = userData;
  65. }
  66. /**
  67. * mono_log_write_syslog:
  68. * \param domain Identifier string
  69. * \param level Logging level flags
  70. * \param format \c printf format string
  71. * \param vargs Variable argument list
  72. * Write data to the log file.
  73. */
  74. void
  75. mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *message)
  76. {
  77. syslog (mapSyslogLevel(level), "%s", message);
  78. if (level & G_LOG_LEVEL_ERROR)
  79. g_assert_abort ();
  80. }
  81. /**
  82. * mono_log_close_syslog:
  83. * Close the log file
  84. */
  85. void
  86. mono_log_close_syslog()
  87. {
  88. #ifdef HAVE_CLOSELOG
  89. closelog();
  90. #endif
  91. }
  92. #endif