hydrobricks
Loading...
Searching...
No Matches
Log.h
1#ifndef HYDROBRICKS_LOG_H
2#define HYDROBRICKS_LOG_H
3
4#include <format>
5#include <memory>
6#include <string>
7
8enum class LogLevel {
9 Error,
10 Warning,
11 Message,
12 Debug
13};
14
20class LogFile {
21 public:
22 LogFile();
23 ~LogFile();
24 LogFile(const LogFile&) = delete;
25 LogFile& operator=(const LogFile&) = delete;
26
30 void Open(const std::string& path);
31
35 void Close();
36
37 [[nodiscard]] bool IsOpen();
38
39 void Write(const std::string& line);
40
41 private:
42 // The stream and its mutex live in the implementation: this header is part of the
43 // core's common includes, which must not pull in <fstream> and <mutex>.
44 struct Impl;
45 std::unique_ptr<Impl> _impl;
46};
47
53class LogScope {
54 public:
55 explicit LogScope(LogFile* file);
56 ~LogScope();
57 LogScope(const LogScope&) = delete;
58 LogScope& operator=(const LogScope&) = delete;
59
60 private:
61 LogFile* _previous;
62};
63
68void LogSetLevel(LogLevel level);
69
73void LogSetFile(const std::string& path);
74
78void LogFlush();
79
80// Core logging functions (non-template)
81void LogError(const std::string& msg);
82void LogWarning(const std::string& msg);
83void LogMessage(const std::string& msg);
84void LogDebug(const std::string& msg);
85
86// Template overloads for formatted messages (std::format style)
87template <typename... Args>
88void LogError(std::format_string<Args...> fmt, Args&&... args) {
89 LogError(std::format(fmt, std::forward<Args>(args)...));
90}
91
92template <typename... Args>
93void LogWarning(std::format_string<Args...> fmt, Args&&... args) {
94 LogWarning(std::format(fmt, std::forward<Args>(args)...));
95}
96
97template <typename... Args>
98void LogMessage(std::format_string<Args...> fmt, Args&&... args) {
99 LogMessage(std::format(fmt, std::forward<Args>(args)...));
100}
101
102template <typename... Args>
103void LogDebug(std::format_string<Args...> fmt, Args&&... args) {
104 LogDebug(std::format(fmt, std::forward<Args>(args)...));
105}
106
107#endif // HYDROBRICKS_LOG_H
Definition Log.h:20
void Open(const std::string &path)
Definition Log.cpp:56
void Close()
Definition Log.cpp:64
Definition Log.h:53
Definition Log.cpp:46