qxLib
file_logger_stream_ofstream.inl
Go to the documentation of this file.
1 /**
2 
3  @file file_logger_stream_ofstream.inl
4  @author Khrapov
5  @date 14.01.2026
6  @copyright (c) Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
14  const config& streamConfig,
15  unit<size_t, units::data> bufferSize)
16  : base_file_logger_stream(streamConfig)
17 {
18  const std::filesystem::path path = prepare_folder_and_get_log_file_path(streamConfig);
19 
20  m_File = std::ofstream(
21  path,
22  std::ios_base::binary
23  | (streamConfig.eLogFilePolicy == log_file_policy::clear_then_upend ? std::ios_base::trunc
24  : std::ios_base::app));
25 
26  if (!m_File)
27  {
28  details::get_cerr<char_type>::get() << QXT("Can't open log file ") << path;
29  return;
30  }
31 
32  if (bufferSize.value > 0)
33  {
34  const unit<size_t, units::data> bytes = convert(bufferSize).to(units::data::bytes);
35  m_Buffer.resize(bytes.value);
36  m_File.rdbuf()->pubsetbuf(m_Buffer.data(), m_Buffer.size());
37  }
38 }
39 
40 inline file_logger_stream_ofstream::~file_logger_stream_ofstream()
41 {
42  if (m_File)
43  m_File << std::flush;
44 }
45 
47  const category& category,
48  verbosity eVerbosity,
49  std::thread::id threadId,
50  std::chrono::system_clock::time_point messageTime,
51  string_view svFile,
52  string_view svFunction,
53  int nLine,
54  string_view svMessage)
55 {
56  if (m_File)
57  {
58  const cstring_view svUtf8 = to_utf8(svMessage);
59  m_File.write(svUtf8.data(), static_cast<std::streamsize>(svUtf8.size()));
60  }
61 }
62 
64 {
65  if (m_File)
66  m_File << std::flush;
67 }
68 
69 } // namespace qx
Base class for all file logger streams.
std::filesystem::path prepare_folder_and_get_log_file_path(const config &config) noexcept
Create a folder (if required) and get log file path.
static cstring_view to_utf8(string_view svMessage)
Encode a message as UTF-8 for a byte output.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
A conversion class.
Definition: base.h:77
file_logger_stream_ofstream(const config &streamConfig=config(), unit< size_t, units::data > bufferSize={ 8192 *sizeof(char_type), units::data::bytes })
file_logger_stream_ofstream object constructor
virtual void do_flush() override
Flush the stream.
virtual void do_log(const category &category, verbosity eVerbosity, std::thread::id threadId, std::chrono::system_clock::time_point messageTime, string_view svFile, string_view svFunction, int nLine, string_view svMessage) override
Proceed stream logging.
Definition: base.h:52