Commit b0378e5 1 parent 4be547b commit b0378e5 Copy full SHA for b0378e5
File tree 3 files changed +20
-0
lines changed
3 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
#include " Format.hpp"
4
4
#include " Log.hpp"
5
+ #include < fstream>
5
6
#include < iostream>
6
7
#include < sstream>
7
8
8
9
namespace SSBL {
9
10
class Logger {
10
11
public:
11
12
Logger &Log (Level level = Level::Info);
13
+ Logger &LogToFile (const std::string &filePath, Level level = Level::Info);
12
14
13
15
template <typename T>
14
16
Logger &operator <<(const T &stream) {
@@ -34,6 +36,7 @@ class Logger {
34
36
private:
35
37
std::ostream *m_msgOutStream = &std::cout;
36
38
std::ostream *m_errOutStream = &std::cerr;
39
+ std::optional<std::ofstream> m_fileOutStream;
37
40
38
41
std::ostringstream m_stream;
39
42
size_t insertCount = 0 ;
Original file line number Diff line number Diff line change 1
1
#include " Logger.hpp"
2
2
#include " Log.hpp"
3
+ #include < fstream>
3
4
4
5
#if _WIN32
5
6
10
11
namespace SSBL {
11
12
12
13
Logger &Logger::Log (const Level level) {
14
+ m_level = level;
15
+ return *this ;
16
+ }
17
+ Logger &Logger::LogToFile (const std::string &filePath, Level level) {
13
18
m_level = level;
19
+ m_fileOutStream = std::ofstream (filePath);
14
20
return *this ;
15
21
}
16
22
@@ -32,6 +38,13 @@ bool Logger::IsLevelIncluded(const Level level) const {
32
38
}
33
39
34
40
void Logger::Send (const std::string &string) {
41
+ if (m_fileOutStream.has_value ()) {
42
+ m_fileOutStream.value () << ToLog (string, m_level, m_config);
43
+ m_fileOutStream.value ().close ();
44
+ m_fileOutStream.reset ();
45
+ return ;
46
+ }
47
+
35
48
if (static_cast <bool >(m_config & Config::Color))
36
49
SetColor ();
37
50
Original file line number Diff line number Diff line change @@ -11,8 +11,12 @@ int main() {
11
11
logger.Log (Level::Warning) << " Hello, Warning!\n " ;
12
12
logger.Log (Level::Error) << " Hello, Error!\n " ;
13
13
// logger.Log(Level::Fatal) << "Hello, Fatal!\n";
14
+ // ^ Try and uncomment this
14
15
15
16
// Formatted output
16
17
logger.Log (Level::Info) << " My name is {}, and I am {} years old." << " John" << 35 << ' \n ' ;
17
18
logger.Log (Level::Info) << " {2}, {1}!" << " World" << " Hello" << ' \n ' ;
19
+
20
+ // File output
21
+ logger.LogToFile (" Example.log" ) << " Hello, File!\n " ;
18
22
}
You can’t perform that action at this time.
0 commit comments