aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/file.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/logger/file.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/logger/file.cpp')
-rw-r--r--library/cpp/logger/file.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/library/cpp/logger/file.cpp b/library/cpp/logger/file.cpp
new file mode 100644
index 0000000000..15a4946eda
--- /dev/null
+++ b/library/cpp/logger/file.cpp
@@ -0,0 +1,59 @@
+#include "file.h"
+#include "record.h"
+
+#include <util/system/file.h>
+#include <util/system/rwlock.h>
+#include <util/generic/string.h>
+
+/*
+ * file log
+ */
+class TFileLogBackend::TImpl {
+public:
+ inline TImpl(const TString& path)
+ : File_(OpenFile(path))
+ {
+ }
+
+ inline void WriteData(const TLogRecord& rec) {
+ //many writes are thread-safe
+ TReadGuard guard(Lock_);
+
+ File_.Write(rec.Data, rec.Len);
+ }
+
+ inline void ReopenLog() {
+ //but log rotate not thread-safe
+ TWriteGuard guard(Lock_);
+
+ File_.LinkTo(OpenFile(File_.GetName()));
+ }
+
+private:
+ static inline TFile OpenFile(const TString& path) {
+ return TFile(path, OpenAlways | WrOnly | ForAppend | Seq | NoReuse);
+ }
+
+private:
+ TRWMutex Lock_;
+ TFile File_;
+};
+
+TFileLogBackend::TFileLogBackend(const TString& path)
+ : Impl_(new TImpl(path))
+{
+}
+
+TFileLogBackend::~TFileLogBackend() {
+}
+
+void TFileLogBackend::WriteData(const TLogRecord& rec) {
+ Impl_->WriteData(rec);
+}
+
+void TFileLogBackend::ReopenLog() {
+ TAtomicSharedPtr<TImpl> copy = Impl_;
+ if (copy) {
+ copy->ReopenLog();
+ }
+}