2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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;
|