bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
FileLog.h
Go to the documentation of this file.
1#pragma once
2
3#include "SyncLog.h" // bux::I_SnapT<>
4#include "XException.h" // RUNTIME_ERROR()
5#include <chrono> // std::chrono::*
6#include <filesystem> // std::filesystem::*
7#include <fstream> // std::ofstream
8#include <string> // std::string
9#include <vector> // std::vector<>
10
11namespace bux {
12
13//
14// Types
15//
16class C_PathFmtLogSnap: public I_SnapT<std::ostream*>
19{
20public:
21
22 // Nonvirtuals
23 explicit C_PathFmtLogSnap(const std::chrono::time_zone *tz);
24 explicit C_PathFmtLogSnap(bool use_local_time = true): C_PathFmtLogSnap(use_local_time? std::chrono::get_tzdb().current_zone(): nullptr) {}
25 C_PathFmtLogSnap &configPath(const std::string &pathFmt);
26 C_PathFmtLogSnap &configPath(uintmax_t fsize_in_bytes, const auto &fallbackPaths)
33 requires requires {
34 std::begin(fallbackPaths);
35 { *std::begin(fallbackPaths) }-> std::convertible_to<std::string>;
36 std::end(fallbackPaths);
37 }
38 {
39 m_PathFmts.clear();
40 for (auto &i: fallbackPaths)
41 {
42 const std::string _path = i;
43 if (_path.empty())
44 RUNTIME_ERROR("Null path format");
45
46 m_PathFmts.emplace_back(std::filesystem::absolute(_path).string());
47 }
48 m_FileSizeLimit = fsize_in_bytes;
49 m_CurPathFmt = 0;
50 return *this;
51 }
52 C_PathFmtLogSnap &enableAutoMkDir(bool yes = true);
53 C_PathFmtLogSnap &setBinaryMode(bool enabled);
54
55 // Implement I_SnapT<std::ostream*>
56 std::ostream *snap() override;
57
58private:
59
60 // Data
61 const std::chrono::time_zone *const m_tz;
62 std::ofstream m_Out; // prior to m_Lock
63 std::string m_CurrPath; // Path of the currently openned file
64 std::vector<std::string> m_PathFmts;
65 uintmax_t m_FileSizeLimit{}; // in bytes
66 size_t m_CurPathFmt{};
67 std::chrono::sys_seconds m_OldTime;
68 std::ios_base::openmode m_OpenMode{std::ios_base::out};
69 bool m_AutoMkDir{true};
70};
71
72} // namespace bux
#define RUNTIME_ERROR(fmtStr,...)
Wrap FILE(DATE)#__LINE__ FUNCTION: msg into std::runtime_error.
Definition XException.h:32
C_PathFmtLogSnap & enableAutoMkDir(bool yes=true)
Definition FileLog.cpp:33
std::ostream * snap() override
Snap the current T value.
Definition FileLog.cpp:57
C_PathFmtLogSnap(const std::chrono::time_zone *tz)
Definition FileLog.cpp:11
C_PathFmtLogSnap & configPath(uintmax_t fsize_in_bytes, const auto &fallbackPaths)
Definition FileLog.h:26
C_PathFmtLogSnap & setBinaryMode(bool enabled)
Definition FileLog.cpp:44
C_PathFmtLogSnap(bool use_local_time=true)
Definition FileLog.h:24
C_PathFmtLogSnap & configPath(const std::string &pathFmt)
Definition FileLog.cpp:16
THE common namespace of bux library.
Definition AtomiX.cpp:3
< Parasitic Type
Definition SyncLog.h:167