1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:47:46 +00:00

AK: Add out() and warn() streams that forward to stdout and stderr

Our C++ code generator tools have been relying on host-side dbg() being
forwarded to stdout until now. Now they use out() instead.

Hopefully this will make it easier and more enticing to use streams in
userspace programs as well. :^)
This commit is contained in:
Andreas Kling 2020-04-06 10:12:10 +02:00
parent 63b11e094d
commit 0d48fb9a87
6 changed files with 219 additions and 176 deletions

View file

@ -34,6 +34,10 @@
# include <Kernel/Thread.h>
#endif
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
#include <stdio.h>
#endif
namespace AK {
const LogStream& operator<<(const LogStream& stream, const String& value)
@ -163,4 +167,20 @@ DebugLogStream::~DebugLogStream()
write(&newline, 1);
}
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
StdLogStream::~StdLogStream()
{
char newline = '\n';
write(&newline, 1);
}
void StdLogStream::write(const char* characters, int length) const
{
if (::write(m_fd, characters, length) < 0) {
perror("StdLogStream::write");
ASSERT_NOT_REACHED();
}
}
#endif
}

View file

@ -68,6 +68,24 @@ public:
}
};
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
class StdLogStream final : public LogStream {
public:
StdLogStream(int fd)
: m_fd(fd)
{
}
virtual ~StdLogStream() override;
virtual void write(const char* characters, int length) const override;
private:
int m_fd { -1 };
};
inline StdLogStream out() { return StdLogStream(STDOUT_FILENO); }
inline StdLogStream warn() { return StdLogStream(STDERR_FILENO); }
#endif
#if !defined(BOOTSTRAPPER) && defined(KERNEL)
class KernelLogStream final : public LogStream {
public:
@ -128,3 +146,8 @@ DebugLogStream klog();
using AK::dbg;
using AK::klog;
using AK::LogStream;
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
using AK::out;
using AK::warn;
#endif