rty_formater.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "rty_formater.h"
  2. #include <util/datetime/base.h>
  3. #include <util/datetime/systime.h>
  4. #include <util/stream/str.h>
  5. #include <util/stream/printf.h>
  6. #include <util/system/mem_info.h>
  7. #include <util/system/yassert.h>
  8. #include <inttypes.h>
  9. #include <cstdio>
  10. namespace {
  11. constexpr size_t LocalTimeSBufferSize = sizeof("2017-07-24 12:20:34.313 +0300");
  12. size_t PrintLocalTimeS(const TInstant instant, char* const begin, const char* const end) {
  13. Y_ABORT_UNLESS(static_cast<size_t>(end - begin) >= LocalTimeSBufferSize);
  14. struct tm tm;
  15. instant.LocalTime(&tm);
  16. // both stftime and snprintf exclude the terminating null byte from the return value
  17. char* pos = begin;
  18. pos += strftime(pos, end - pos, "%Y-%m-%d %H:%M:%S.", &tm);
  19. pos += snprintf(pos, end - pos, "%03" PRIu32, instant.MilliSecondsOfSecond());
  20. pos += strftime(pos, end - pos, " %z", &tm);
  21. Y_ABORT_UNLESS(LocalTimeSBufferSize - 1 == pos - begin); // together with Y_ABORT_UNLESS above this also implies pos<=end
  22. return (pos - begin);
  23. }
  24. }
  25. namespace NLoggingImpl {
  26. IOutputStream& operator<<(IOutputStream& out, TLocalTimeS localTimeS) {
  27. char buffer[LocalTimeSBufferSize];
  28. size_t len = PrintLocalTimeS(localTimeS.GetInstant(), buffer, buffer + sizeof(buffer));
  29. out.Write(buffer, len);
  30. return out;
  31. }
  32. TLocalTimeS::operator TString() const {
  33. TString res;
  34. res.reserve(LocalTimeSBufferSize);
  35. res.ReserveAndResize(PrintLocalTimeS(Instant, res.begin(), res.begin() + res.capacity()));
  36. return res;
  37. }
  38. TString TLocalTimeS::operator+(const TStringBuf right) const {
  39. TString res(*this);
  40. res += right;
  41. return res;
  42. }
  43. TStringBuf StripFileName(TStringBuf string) {
  44. return string.RNextTok(LOCSLASH_C);
  45. }
  46. TString GetSystemResources() {
  47. NMemInfo::TMemInfo mi = NMemInfo::GetMemInfo();
  48. return PrintSystemResources(mi);
  49. }
  50. TString PrintSystemResources(const NMemInfo::TMemInfo& mi) {
  51. return Sprintf(" rss=%0.3fMb, vms=%0.3fMb", mi.RSS * 1.0 / (1024 * 1024), mi.VMS * 1.0 / (1024 * 1024));
  52. }
  53. }
  54. namespace {
  55. class TRtyLoggerFormatter : public ILoggerFormatter {
  56. public:
  57. void Format(const TLogRecordContext& context, TLogElement& elem) const override {
  58. elem << context.CustomMessage << ": " << NLoggingImpl::GetLocalTimeS() << " "
  59. << NLoggingImpl::StripFileName(context.SourceLocation.File) << ":" << context.SourceLocation.Line;
  60. if (context.Priority > TLOG_RESOURCES && !ExitStarted()) {
  61. elem << NLoggingImpl::GetSystemResources();
  62. }
  63. elem << " ";
  64. }
  65. };
  66. }
  67. ILoggerFormatter* CreateRtyLoggerFormatter() {
  68. return new TRtyLoggerFormatter();
  69. }
  70. bool TRTYMessageFormater::CheckLoggingContext(TLog& /*logger*/, const TLogRecordContext& /*context*/) {
  71. return true;
  72. }
  73. TSimpleSharedPtr<TLogElement> TRTYMessageFormater::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) {
  74. if (!earlier) {
  75. earlier.Reset(new TLogElement(&logger));
  76. }
  77. TLoggerFormatterOperator::Get()->Format(context, *earlier);
  78. return earlier;
  79. }