30 template <
typename Derived,
typename MessageType>
33 template <
typename Message>
71 explicit log(
const std::filesystem::path& log_path)
73 using namespace std::string_literals;
76 std::filesystem::create_directories(log_path.parent_path());
80 std::filesystem::remove(_log_path);
82 catch (
const std::exception& ex)
87 do_append(L
"-------------------------------- start of log --------------------------------"s);
99 using namespace std::string_literals;
100 std::lock_guard<std::mutex> lock(_mtx);
101 do_append(L
"-------------------------------- end of log --------------------------------"s);
103 catch (
const std::system_error& ex)
105 std::wcerr <<
"cbeam::logging::~log: Could not lock the mutex. This indicates a serious (unexpected) bug that must be fixed during development phase: "
106 << ex.what() << std::endl;
118 void append(
const std::wstring& str)
noexcept
124 if (!_mtx.try_lock())
127 header = create_header(thread_id);
131 do_append(str, thread_id, header);
133 catch (
const std::exception& ex)
135 std::wcerr <<
"cbeam::logging::append: " << ex.what() << std::endl;
147 void append(
const std::string& str)
noexcept
149 append(std::wstring(str.begin(), str.end()));
173 time = create_header(thread_id);
175 std::wofstream(_log_path, std::ofstream::app) << time << str << std::endl;
177 catch (
const std::exception& ex)
179 std::wcerr << str <<
" " << ex.what() << std::endl;
191 std::wstringstream stream;
192 stream << current_time_to_wstring() << L
" ("
204 static std::wstring current_time_to_wstring()
209 std::unique_ptr<concurrency::message_manager<message>> _message_manager;
210 std::filesystem::path _log_path;
Definition message_manager.hpp:102
Definition threaded_object.hpp:97
void append(const std::wstring &str) noexcept
Appends a wide-string message to the log, along with thread and timestamp information.
Definition logging_impl.hpp:118
log(const std::filesystem::path &log_path)
Constructs a log object that manages logging to a file.
Definition logging_impl.hpp:71
void append(const std::string &str) noexcept
Appends a narrow-string message to the log (converted internally to wide-string).
Definition logging_impl.hpp:147
~log() noexcept
Destructor writes a final footer line before the log object is destroyed.
Definition logging_impl.hpp:95
Provides concurrency primitives and abstractions for multithreaded programming. It features the power...
Definition message_manager.hpp:47
HANDLE thread_id_type
Definition thread.hpp:50
thread_id_type get_current_thread_id()
Retrieves the current thread's native identifier.
Definition thread.hpp:63
std::wstring to_string(concurrency::thread_id_type id, std::size_t mask=(std::size_t) -1)
Returns a hexadecimal string representation of the given thread ID.
Definition thread.hpp:214
std::wstring get_thread_name(thread_id_type id)
Retrieves the name of the specified thread.
Definition thread.hpp:184
std::string to_string(const container::buffer &b)
Creates a std::string from the contents of a container::buffer.
Definition buffer.hpp:42
T from_string(const std::string &str)
Converts a given std::string to a specified type.
Definition string.hpp:171
std::wstring from_string< std::wstring >(const std::string &str)
Converts the given std::string to std::wstring using UTF-8 to UTF-16 encoding.
Definition string.hpp:196
Offers flexible logging mechanisms to record messages with timestamps and thread information....
Definition logging_impl.hpp:53