mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:57:43 +00:00
AK: Start fleshing out LogStream, a type-aware logging mechanism.
The first implementation class is DebugLogStream, which can be used like so: dbg() << "Hello friends, I am " << m_years << " years old!"; Note that it will automatically print a newline when the object created by dbg() goes out of scope. This API will grow and evolve, so let's see what we end up with :^)
This commit is contained in:
parent
27f699ef0c
commit
05cc59921a
1 changed files with 78 additions and 0 deletions
78
AK/LogStream.h
Normal file
78
AK/LogStream.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/kstdio.h>
|
||||
|
||||
class LogStream {
|
||||
public:
|
||||
LogStream() {}
|
||||
virtual ~LogStream() {}
|
||||
|
||||
virtual void write(const char*, int) const = 0;
|
||||
};
|
||||
|
||||
class DebugLogStream final : public LogStream {
|
||||
public:
|
||||
DebugLogStream() {}
|
||||
virtual ~DebugLogStream() override
|
||||
{
|
||||
char newline = '\n';
|
||||
write(&newline, 1);
|
||||
}
|
||||
|
||||
virtual void write(const char* characters, int length) const override
|
||||
{
|
||||
for (int i = 0; i < length; ++i)
|
||||
dbgprintf("%c", characters[i]);
|
||||
}
|
||||
};
|
||||
|
||||
inline DebugLogStream dbg()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const char* value)
|
||||
{
|
||||
stream.write(value, strlen(value));
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const String& value)
|
||||
{
|
||||
stream.write(value.characters(), value.length());
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const StringView& value)
|
||||
{
|
||||
stream.write(value.characters(), value.length());
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, char value)
|
||||
{
|
||||
stream.write(&value, 1);
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, bool value)
|
||||
{
|
||||
return stream << (value ? "true" : "false");
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, int value)
|
||||
{
|
||||
return stream << String::number(value);
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, unsigned value)
|
||||
{
|
||||
return stream << String::number(value);
|
||||
}
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, const void* value)
|
||||
{
|
||||
return stream << String::format("%p", value);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue