common.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "common.h"
  2. #include <util/generic/yexception.h>
  3. namespace NLoggingImpl {
  4. TString GetLocalTimeSSimple() {
  5. struct tm tm;
  6. return Strftime("%b%d_%H%M%S", Now().LocalTime(&tm));
  7. }
  8. TString PrepareToOpenLog(TString logType, const int logLevel, const bool rotation, const bool startAsDaemon) {
  9. Y_ENSURE(logLevel >= 0 && logLevel <= (int)LOG_MAX_PRIORITY, "Incorrect log level");
  10. if (rotation && TFsPath(logType).Exists()) {
  11. TString newPath = Sprintf("%s_%s_%" PRIu64, logType.data(), NLoggingImpl::GetLocalTimeSSimple().data(), static_cast<ui64>(Now().MicroSeconds()));
  12. TFsPath(logType).RenameTo(newPath);
  13. }
  14. if (startAsDaemon && (logType == "console"sv || logType == "cout"sv || logType == "cerr"sv)) {
  15. logType = "null";
  16. }
  17. return logType;
  18. }
  19. }
  20. bool TLogFilter::CheckLoggingContext(TLog& log, const TLogRecordContext& context) {
  21. return context.Priority <= log.FiltrationLevel();
  22. }
  23. TSimpleSharedPtr<TLogElement> TLogFilter::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) {
  24. if (earlier)
  25. return earlier;
  26. TSimpleSharedPtr<TLogElement> result(new TLogElement(&logger));
  27. *result << context.Priority;
  28. return result;
  29. }