system.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include "log.h"
  3. #include "backend.h"
  4. #include "priority.h"
  5. #define YSYSLOG(priority, ...) SysLogInstance().AddLog((priority), __VA_ARGS__)
  6. #define YSYSLOGINIT_FLAGS(ident, facility, flags) \
  7. struct TLogIniter { \
  8. TLogIniter() { \
  9. SysLogInstance().ResetBackend(THolder<TLogBackend>( \
  10. (ident) ? (TLogBackend*)(new TSysLogBackend((ident), (facility), (flags))) : (TLogBackend*)(new TNullLogBackend())));\
  11. } \
  12. } Y_CAT(loginit, __LINE__);
  13. #define YSYSLOGINIT(ident, facility) YSYSLOGINIT_FLAGS((ident), (facility), 0)
  14. class TSysLogBackend: public TLogBackend {
  15. public:
  16. enum EFacility {
  17. TSYSLOG_LOCAL0 = 0,
  18. TSYSLOG_LOCAL1 = 1,
  19. TSYSLOG_LOCAL2 = 2,
  20. TSYSLOG_LOCAL3 = 3,
  21. TSYSLOG_LOCAL4 = 4,
  22. TSYSLOG_LOCAL5 = 5,
  23. TSYSLOG_LOCAL6 = 6,
  24. TSYSLOG_LOCAL7 = 7
  25. };
  26. enum EFlags {
  27. LogPerror = 1,
  28. LogCons = 2
  29. };
  30. TSysLogBackend(const char* ident, EFacility facility, int flags = 0);
  31. ~TSysLogBackend() override;
  32. void WriteData(const TLogRecord& rec) override;
  33. void ReopenLog() override;
  34. virtual TString GetIdent() const {
  35. return Ident;
  36. }
  37. virtual EFacility GetFacility() const {
  38. return Facility;
  39. }
  40. virtual int GetFlags() const {
  41. return Flags;
  42. }
  43. protected:
  44. int ELogPriority2SyslogPriority(ELogPriority priority);
  45. TString Ident;
  46. EFacility Facility;
  47. int Flags;
  48. };
  49. /*
  50. * return system-wide logger instance
  51. * better do not use in real programs(instead of robot, of course)
  52. */
  53. TLog& SysLogInstance();