bux API Reference 1.11.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
24#if LOCALZONE_IS_TIMEZONE
25 explicit C_PathFmtLogSnap(bool use_local_time): C_PathFmtLogSnap(use_local_time? local_zone(): T_LocalZone()) {}
26#endif
27 C_PathFmtLogSnap &configPath(const std::string &pathFmt);
28 C_PathFmtLogSnap &configPath(uintmax_t fsize_in_bytes, const auto &fallbackPaths)
35 requires requires {
36 std::begin(fallbackPaths);
37 { *std::begin(fallbackPaths) }-> std::convertible_to<std::string>;
38 std::end(fallbackPaths);
39 }
40 {
41 m_PathFmts.clear();
42 for (auto &i: fallbackPaths)
43 {
44 const std::string _path = i;
45 if (_path.empty())
46 RUNTIME_ERROR("Null path format");
47
48 m_PathFmts.emplace_back(std::filesystem::absolute(_path).string());
49 }
50 m_FileSizeLimit = fsize_in_bytes;
51 m_CurPathFmt = 0;
52 return *this;
53 }
54 C_PathFmtLogSnap &enableAutoMkDir(bool yes = true);
55 C_PathFmtLogSnap &setBinaryMode(bool enabled);
56
57 // Implement I_SnapT<std::ostream*>
58 std::ostream *snap() override;
59
60private:
61
62 // Data
63 const T_LocalZone m_tz;
64 std::ofstream m_Out; // prior to m_Lock
65 std::string m_CurrPath; // Path of the currently openned file
66 std::vector<std::string> m_PathFmts;
67 uintmax_t m_FileSizeLimit{}; // in bytes
68 size_t m_CurPathFmt{};
69 std::chrono::sys_seconds m_OldTime;
70 std::ios_base::openmode m_OpenMode{std::ios_base::out};
71 bool m_AutoMkDir{true};
72};
73
74} // 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
C_PathFmtLogSnap(bool use_local_time)
Definition FileLog.h:25
std::ostream * snap() override
Snap the current T value.
Definition FileLog.cpp:57
C_PathFmtLogSnap & configPath(uintmax_t fsize_in_bytes, const auto &fallbackPaths)
Definition FileLog.h:28
C_PathFmtLogSnap(T_LocalZone tz=local_zone())
Definition FileLog.cpp:11
C_PathFmtLogSnap & setBinaryMode(bool enabled)
Definition FileLog.cpp:44
C_PathFmtLogSnap & configPath(const std::string &pathFmt)
Definition FileLog.cpp:16
THE common namespace of bux library.
Definition AtomiX.cpp:3
const std::chrono::time_zone * T_LocalZone
Definition XPlatform.h:18
T_LocalZone local_zone()
Definition XPlatform.h:19
< Parasitic Type
Definition SyncLog.h:169