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
|
|
|
|
|
2020-03-22 18:07:02 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-12-09 16:45:11 +00:00
|
|
|
#include <AK/Types.h>
|
2019-07-04 04:43:30 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
|
2020-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
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 {
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
class LogStream {
|
|
|
|
public:
|
2019-07-25 05:00:33 +00:00
|
|
|
LogStream()
|
2020-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
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:
|
2020-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
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() {}
|
2020-02-14 20:41:10 +00:00
|
|
|
virtual ~DebugLogStream() override;
|
2019-07-04 04:43:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-06 08:12:10 +00:00
|
|
|
#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
|
|
|
|
|
2020-02-28 15:10:30 +00:00
|
|
|
#if !defined(BOOTSTRAPPER) && defined(KERNEL)
|
|
|
|
class KernelLogStream final : public LogStream {
|
|
|
|
public:
|
|
|
|
KernelLogStream() {}
|
|
|
|
virtual ~KernelLogStream() override;
|
|
|
|
|
|
|
|
virtual void write(const char* characters, int length) const override
|
|
|
|
{
|
|
|
|
kernelputstr(characters, length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const char* value)
|
|
|
|
{
|
2020-03-04 18:25:57 +00:00
|
|
|
if (!value)
|
|
|
|
return stream << "(null)";
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-22 18:07:02 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, const FlyString&);
|
2019-07-04 05:05:58 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, const String&);
|
|
|
|
const LogStream& operator<<(const LogStream&, const StringView&);
|
2020-02-05 18:09:29 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, int);
|
|
|
|
const LogStream& operator<<(const LogStream&, unsigned);
|
|
|
|
const LogStream& operator<<(const LogStream&, long long);
|
2020-01-30 23:03:58 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, unsigned long);
|
2020-02-05 18:09:29 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, unsigned long long);
|
2019-12-09 16:45:11 +00:00
|
|
|
|
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();
|
|
|
|
|
2020-02-28 15:10:30 +00:00
|
|
|
#if defined(KERNEL)
|
|
|
|
KernelLogStream klog();
|
|
|
|
#elif !defined(BOOTSTRAPPER)
|
|
|
|
DebugLogStream klog();
|
|
|
|
#endif
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
using AK::dbg;
|
2020-02-28 15:10:30 +00:00
|
|
|
using AK::klog;
|
2019-07-15 18:22:30 +00:00
|
|
|
using AK::LogStream;
|
2020-04-06 08:12:10 +00:00
|
|
|
|
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
|
|
|
using AK::out;
|
|
|
|
using AK::warn;
|
|
|
|
#endif
|