system.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <util/stream/output.h>
  2. #include <util/stream/null.h>
  3. #include <util/system/yassert.h>
  4. #include <util/system/defaults.h>
  5. #include <util/generic/singleton.h>
  6. #include <util/generic/utility.h>
  7. #if defined(_unix_)
  8. #include <syslog.h>
  9. #endif
  10. #include "system.h"
  11. #include "record.h"
  12. #include "stream.h"
  13. TSysLogBackend::TSysLogBackend(const char* ident, EFacility facility, int flags)
  14. : Ident(ident)
  15. , Facility(facility)
  16. , Flags(flags)
  17. {
  18. #if defined(_unix_)
  19. Y_ASSERT(TSYSLOG_LOCAL0 <= facility && facility <= TSYSLOG_LOCAL7);
  20. static const int f2sf[] = {
  21. LOG_LOCAL0,
  22. LOG_LOCAL1,
  23. LOG_LOCAL2,
  24. LOG_LOCAL3,
  25. LOG_LOCAL4,
  26. LOG_LOCAL5,
  27. LOG_LOCAL6,
  28. LOG_LOCAL7};
  29. int sysflags = LOG_NDELAY | LOG_PID;
  30. if (flags & LogPerror) {
  31. sysflags |= LOG_PERROR;
  32. }
  33. if (flags & LogCons) {
  34. sysflags |= LOG_CONS;
  35. }
  36. openlog(Ident.data(), sysflags, f2sf[(size_t)facility]);
  37. #endif
  38. }
  39. TSysLogBackend::~TSysLogBackend() {
  40. #if defined(_unix_)
  41. closelog();
  42. #endif
  43. }
  44. void TSysLogBackend::WriteData(const TLogRecord& rec) {
  45. #if defined(_unix_)
  46. syslog(ELogPriority2SyslogPriority(rec.Priority), "%.*s", (int)rec.Len, rec.Data);
  47. #else
  48. Y_UNUSED(rec);
  49. #endif
  50. }
  51. void TSysLogBackend::ReopenLog() {
  52. }
  53. int TSysLogBackend::ELogPriority2SyslogPriority(ELogPriority priority) {
  54. #if defined(_unix_)
  55. return Min(int(priority), (int)LOG_PRIMASK);
  56. #else
  57. // trivial conversion
  58. return int(priority);
  59. #endif
  60. }
  61. namespace {
  62. class TSysLogInstance: public TLog {
  63. public:
  64. inline TSysLogInstance()
  65. : TLog(MakeHolder<TStreamLogBackend>(&Cnull))
  66. {
  67. }
  68. };
  69. }
  70. TLog& SysLogInstance() {
  71. return *Singleton<TSysLogInstance>();
  72. }