2019-07-04 04:43:30 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-09 16:45:11 +00:00
|
|
|
#include <AK/Types.h>
|
2019-07-04 04:43:30 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
|
2019-12-20 21:56:42 +00:00
|
|
|
#ifndef KERNEL
|
2019-08-15 18:55:45 +00:00
|
|
|
# include <AK/ScopedValueRollback.h>
|
|
|
|
# include <AK/StringView.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <unistd.h>
|
2019-07-25 05:00:33 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class String;
|
|
|
|
class StringView;
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
class LogStream {
|
|
|
|
public:
|
2019-07-25 05:00:33 +00:00
|
|
|
LogStream()
|
2019-12-20 21:56:42 +00:00
|
|
|
#ifndef KERNEL
|
2019-07-25 05:00:33 +00:00
|
|
|
: m_errno_restorer(errno)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2019-07-04 04:43:30 +00:00
|
|
|
virtual ~LogStream() {}
|
|
|
|
|
|
|
|
virtual void write(const char*, int) const = 0;
|
2019-07-15 18:22:30 +00:00
|
|
|
|
2019-07-25 05:00:33 +00:00
|
|
|
private:
|
2019-12-20 21:56:42 +00:00
|
|
|
#ifndef KERNEL
|
2019-07-25 13:15:14 +00:00
|
|
|
ScopedValueRollback<int> m_errno_restorer;
|
2019-07-25 05:00:33 +00:00
|
|
|
#endif
|
2019-07-04 04:43:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-21 19:43:37 +00:00
|
|
|
dbgputstr(characters, length);
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const char* value)
|
|
|
|
{
|
2019-07-04 05:05:58 +00:00
|
|
|
int length = 0;
|
|
|
|
const char* p = value;
|
|
|
|
while (*(p++))
|
|
|
|
++length;
|
|
|
|
stream.write(value, length);
|
2019-07-04 04:43:30 +00:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, const String&);
|
|
|
|
const LogStream& operator<<(const LogStream&, const StringView&);
|
2019-12-09 16:45:11 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, i32);
|
|
|
|
const LogStream& operator<<(const LogStream&, u32);
|
|
|
|
const LogStream& operator<<(const LogStream&, u64);
|
|
|
|
|
|
|
|
#ifdef __serenity__
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, size_t value)
|
|
|
|
{
|
|
|
|
if constexpr (sizeof(size_t) == 4)
|
|
|
|
return stream << (u32)value;
|
|
|
|
else
|
|
|
|
return stream << (u64)value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, const void*);
|
2019-07-04 04:43:30 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-08-15 18:55:45 +00:00
|
|
|
DebugLogStream dbg();
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
using AK::dbg;
|
2019-07-15 18:22:30 +00:00
|
|
|
using AK::LogStream;
|