Skip to content

Commit 6fcfd90

Browse files
authored
Add support for log streams (#3)
1 parent 80ce938 commit 6fcfd90

9 files changed

Lines changed: 111 additions & 33 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.vscode/
2-
build/
2+
build/
3+
/.vs

include/AsyncLogger/Logger.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "LogIntermediate.hpp"
1717
#include "LogLevel.hpp"
1818
#include "LogMessage.hpp"
19+
#include "LogStream.hpp"
1920

2021
namespace al
2122
{
@@ -37,7 +38,7 @@ namespace al
3738
static void FlushQueue();
3839

3940
protected:
40-
static void PushMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message) noexcept;
41+
static void PushMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message, std::optional<std::shared_ptr<LogStream> const>&& stream = std::nullopt) noexcept;
4142

4243
private:
4344
void CallSinks(LogMessagePtr msgPtr);
@@ -70,7 +71,17 @@ namespace al
7071
template<typename ...Args>
7172
inline void LOGF(const eLogLevel level, LogIntermediate formatString, Args&&... formatArgs)
7273
{
73-
auto capture = LogCapture(level, std::move(formatString.Location()));
74+
auto capture = LogCapture(level, std::move(formatString.Location()), std::nullopt);
75+
capture << VFORMAT(formatString.FormatString(), MAKE_FORMAT_ARGS(formatArgs...));
76+
}
77+
78+
template<typename... Args>
79+
inline void LOGF(const std::shared_ptr<LogStream> stream, const eLogLevel level, LogIntermediate formatString, Args&&... formatArgs)
80+
{
81+
if (stream && !stream->Enabled())
82+
return;
83+
84+
auto capture = LogCapture(level, std::move(formatString.Location()), stream);
7485
capture << VFORMAT(formatString.FormatString(), MAKE_FORMAT_ARGS(formatArgs...));
7586
}
7687
}

src/LogCapture.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace al
55
{
6-
LogCapture::LogCapture(const eLogLevel level, std::source_location&& location) :
6+
LogCapture::LogCapture(const eLogLevel level, std::source_location&& location, std::optional<std::shared_ptr<LogStream> const> stream) :
77
m_Level(level),
88
m_Timestamp(std::chrono::system_clock::now()),
9-
m_Location(location)
9+
m_Location(location),
10+
m_LogStream(stream)
1011
{}
1112

1213
LogCapture::~LogCapture()
1314
{
1415
m_Stream << '\n';
15-
Logger::PushMessage(m_Level, std::move(m_Timestamp), std::move(m_Location), std::move(m_Stream.str()));
16+
Logger::PushMessage(m_Level, std::move(m_Timestamp), std::move(m_Location), std::move(m_Stream.str()), std::move(m_LogStream));
1617
}
1718
}

src/LogCapture.hpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@
33
#include <source_location>
44
#include <sstream>
55
#include "LogLevel.hpp"
6+
#include "LogStream.hpp"
67

78
namespace al
89
{
9-
class Logger;
10-
class LogCapture
11-
{
12-
public:
13-
LogCapture(const eLogLevel level, std::source_location&& location);
14-
~LogCapture();
10+
class Logger;
11+
class LogCapture
12+
{
13+
public:
14+
LogCapture(const eLogLevel level, std::source_location&& location, std::optional<std::shared_ptr<LogStream> const> stream);
15+
~LogCapture();
1516

16-
template<typename T>
17-
std::ostream& operator<< (const T& d);
17+
template<typename T>
18+
std::ostream& operator<< (const T& d);
1819

19-
private:
20-
const eLogLevel m_Level;
21-
std::chrono::system_clock::time_point m_Timestamp;
22-
std::source_location m_Location;
23-
std::ostringstream m_Stream;
24-
25-
};
20+
private:
21+
const eLogLevel m_Level;
22+
std::chrono::system_clock::time_point m_Timestamp;
23+
std::source_location m_Location;
24+
std::ostringstream m_Stream;
25+
std::optional<std::shared_ptr<LogStream> const> m_LogStream;
26+
};
2627

27-
template<typename T>
28-
std::ostream& LogCapture::operator<< (const T& d)
29-
{
30-
m_Stream << d;
31-
return m_Stream;
32-
}
28+
template<typename T>
29+
std::ostream& LogCapture::operator<< (const T& d)
30+
{
31+
m_Stream << d;
32+
return m_Stream;
33+
}
3334

3435
}

src/LogMessage.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace al
66
{
7-
LogMessage::LogMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message) :
7+
LogMessage::LogMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message, std::optional<std::shared_ptr<LogStream> const> stream) :
88
m_Level(level),
99
m_Timestamp(timestamp),
1010
m_Location(location),
11-
m_Message(message)
11+
m_Message(message),
12+
m_Stream(stream)
1213
{
1314

1415
}
@@ -32,4 +33,9 @@ namespace al
3233
{
3334
return m_Timestamp;
3435
}
36+
37+
const std::optional<std::shared_ptr<LogStream> const> LogMessage::Stream() const
38+
{
39+
return m_Stream;
40+
}
3541
}

src/LogMessage.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
#include <source_location>
33
#include <string>
44
#include "LogLevel.hpp"
5+
#include "LogStream.hpp"
56

67
namespace al
78
{
89
class LogMessage
910
{
1011
public:
11-
LogMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message);
12+
LogMessage(const eLogLevel level, std::chrono::system_clock::time_point&& timestamp, std::source_location&& location, std::string&& message, std::optional<std::shared_ptr<LogStream> const> stream);
1213
virtual ~LogMessage() = default;
1314

1415
const eLogLevel& Level() const;
1516
const std::source_location& Location() const;
1617
const std::string& Message() const;
1718
const std::chrono::system_clock::time_point& Timestamp() const;
19+
const std::optional<std::shared_ptr<LogStream> const> Stream() const;
1820

1921
private:
2022
const eLogLevel m_Level;
2123
const std::chrono::system_clock::time_point m_Timestamp;
2224
const std::source_location m_Location;
2325
const std::string m_Message;
26+
const std::optional<std::shared_ptr<LogStream> const> m_Stream;
2427

2528
};
2629
}

src/LogStream.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "LogStream.hpp"
2+
3+
namespace al
4+
{
5+
LogStream::LogStream(std::string name, void* userData) :
6+
m_Name(name),
7+
m_UserData(userData),
8+
m_Enabled(true)
9+
{
10+
}
11+
12+
const std::string& LogStream::Name() const
13+
{
14+
return m_Name;
15+
}
16+
17+
const void* LogStream::UserData() const
18+
{
19+
return m_UserData;
20+
}
21+
22+
bool LogStream::Enabled() const
23+
{
24+
return m_Enabled;
25+
}
26+
27+
void LogStream::SetEnabled(bool state)
28+
{
29+
m_Enabled = state;
30+
}
31+
}

src/LogStream.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include "LogIntermediate.hpp"
3+
4+
namespace al
5+
{
6+
class LogStream
7+
{
8+
public:
9+
LogStream(std::string name, void* userData = nullptr);
10+
11+
const std::string& Name() const;
12+
const void* UserData() const;
13+
bool Enabled() const;
14+
15+
void SetEnabled(bool state);
16+
17+
private:
18+
std::string m_Name;
19+
void* m_UserData;
20+
bool m_Enabled;
21+
22+
};
23+
}

src/Logger.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace al
66
{
77
LogCapture LOG(const eLogLevel level, std::source_location location)
88
{
9-
return LogCapture{ level, std::move(location) };
9+
return LogCapture{ level, std::move(location), std::nullopt };
1010
}
1111

1212
void Logger::AddSink(LogSink sink)
@@ -32,8 +32,9 @@ namespace al
3232
void Logger::PushMessage(const eLogLevel level,
3333
std::chrono::system_clock::time_point &&timestamp,
3434
std::source_location &&location,
35-
std::string &&message) noexcept {
36-
auto msgPtr = std::make_shared<LogMessage>(level, std::move(timestamp), std::move(location), std::move(message));
35+
std::string &&message, std::optional<std::shared_ptr<LogStream> const> &&stream) noexcept
36+
{
37+
auto msgPtr = std::make_shared<LogMessage>(level, std::move(timestamp), std::move(location), std::move(message), std::move(stream));
3738

3839
Logger::GetInstance().QueueMessage(std::move(msgPtr));
3940
}

0 commit comments

Comments
 (0)