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-09-23 12:45:41 +00:00
|
|
|
#include <AK/Format.h>
|
2020-03-22 18:07:02 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-12-09 16:45:11 +00:00
|
|
|
#include <AK/Types.h>
|
2020-07-02 14:34:08 +00:00
|
|
|
#include <AK/kmalloc.h>
|
2019-07-04 04:43:30 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(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 {
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
class LogStream {
|
|
|
|
public:
|
2019-07-25 05:00:33 +00:00
|
|
|
LogStream()
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(KERNEL)
|
2019-07-25 05:00:33 +00:00
|
|
|
: m_errno_restorer(errno)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2020-07-02 14:34:08 +00:00
|
|
|
virtual ~LogStream() { }
|
2019-07-04 04:43:30 +00:00
|
|
|
|
|
|
|
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-05-16 10:00:04 +00:00
|
|
|
#if !defined(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
|
|
|
};
|
|
|
|
|
2020-07-02 14:34:08 +00:00
|
|
|
class BufferedLogStream : public LogStream {
|
|
|
|
mutable size_t m_size { 0 };
|
|
|
|
mutable size_t m_capacity { 128 };
|
|
|
|
union {
|
|
|
|
mutable u8* m_buffer { nullptr };
|
|
|
|
mutable u8 m_local_buffer[128];
|
|
|
|
} u;
|
|
|
|
|
|
|
|
void grow(size_t bytes_needed) const
|
|
|
|
{
|
|
|
|
size_t new_capacity = (m_size + bytes_needed + 0x7F) & ~0x7F;
|
|
|
|
u8* new_data = static_cast<u8*>(kmalloc(new_capacity));
|
|
|
|
if (m_capacity <= sizeof(u.m_local_buffer)) {
|
|
|
|
__builtin_memcpy(new_data, u.m_local_buffer, m_size);
|
|
|
|
} else if (u.m_buffer) {
|
|
|
|
__builtin_memcpy(new_data, u.m_buffer, m_size);
|
|
|
|
kfree(u.m_buffer);
|
|
|
|
}
|
|
|
|
u.m_buffer = new_data;
|
|
|
|
m_capacity = new_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
u8* data() const
|
|
|
|
{
|
|
|
|
if (m_capacity <= sizeof(u.m_local_buffer))
|
|
|
|
return u.m_local_buffer;
|
|
|
|
return u.m_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return m_size; }
|
|
|
|
|
|
|
|
bool empty() const { return m_size == 0; }
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
public:
|
2020-07-02 14:34:08 +00:00
|
|
|
BufferedLogStream() { }
|
2019-07-04 04:43:30 +00:00
|
|
|
|
2020-07-02 14:34:08 +00:00
|
|
|
virtual ~BufferedLogStream() override
|
2019-07-04 04:43:30 +00:00
|
|
|
{
|
2020-07-02 14:34:08 +00:00
|
|
|
if (m_capacity > sizeof(u.m_local_buffer))
|
|
|
|
kfree(u.m_buffer);
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
2020-07-02 14:34:08 +00:00
|
|
|
|
|
|
|
virtual void write(const char* str, int len) const override
|
|
|
|
{
|
|
|
|
size_t new_size = m_size + len;
|
|
|
|
if (new_size > m_capacity)
|
|
|
|
grow(len);
|
|
|
|
__builtin_memcpy(data() + m_size, str, len);
|
|
|
|
m_size = new_size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DebugLogStream final : public BufferedLogStream {
|
|
|
|
public:
|
|
|
|
DebugLogStream() { }
|
|
|
|
virtual ~DebugLogStream() override;
|
2020-08-28 07:56:51 +00:00
|
|
|
|
|
|
|
// DebugLogStream only checks `enabled` and possibly generates output while the destructor runs.
|
|
|
|
static void set_enabled(bool);
|
|
|
|
static bool is_enabled();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool s_enabled;
|
2019-07-04 04:43:30 +00:00
|
|
|
};
|
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(KERNEL)
|
2020-04-06 08:12:10 +00:00
|
|
|
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-05-16 10:00:04 +00:00
|
|
|
#ifdef KERNEL
|
2020-07-02 14:34:08 +00:00
|
|
|
class KernelLogStream final : public BufferedLogStream {
|
2020-02-28 15:10:30 +00:00
|
|
|
public:
|
2020-07-02 14:34:08 +00:00
|
|
|
KernelLogStream() { }
|
2020-02-28 15:10:30 +00:00
|
|
|
virtual ~KernelLogStream() override;
|
|
|
|
};
|
|
|
|
#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);
|
2020-05-08 07:35:23 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, long);
|
2020-02-05 18:09:29 +00:00
|
|
|
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
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(KERNEL)
|
2020-04-12 17:17:10 +00:00
|
|
|
const LogStream& operator<<(const LogStream&, double);
|
|
|
|
const LogStream& operator<<(const LogStream&, float);
|
|
|
|
#endif
|
|
|
|
|
2020-09-03 15:57:08 +00:00
|
|
|
template<typename T>
|
|
|
|
const LogStream& operator<<(const LogStream& stream, Span<T> span)
|
|
|
|
{
|
|
|
|
return stream << "{ " << span.data() << ", " << span.size() << " }";
|
|
|
|
}
|
|
|
|
|
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-05-16 10:00:04 +00:00
|
|
|
#ifdef KERNEL
|
2020-02-28 15:10:30 +00:00
|
|
|
KernelLogStream klog();
|
2020-05-16 10:00:04 +00:00
|
|
|
#else
|
2020-02-28 15:10:30 +00:00
|
|
|
DebugLogStream klog();
|
|
|
|
#endif
|
|
|
|
|
2020-09-10 11:50:04 +00:00
|
|
|
void dump_bytes(ReadonlyBytes);
|
|
|
|
|
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
|
|
|
|
2020-05-16 10:00:04 +00:00
|
|
|
#if !defined(KERNEL)
|
2020-04-06 08:12:10 +00:00
|
|
|
using AK::out;
|
|
|
|
using AK::warn;
|
|
|
|
#endif
|