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

Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)

Good-bye LogStream. Long live AK::Format!
This commit is contained in:
Andreas Kling 2021-03-12 17:29:37 +01:00
parent 423ed53396
commit ef1e5db1d0
209 changed files with 164 additions and 837 deletions

View file

@ -325,12 +325,6 @@ inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::copy(const void* data, size
return ::adopt(*new ByteBufferImpl(data, size));
}
inline const LogStream& operator<<(const LogStream& stream, const ByteBuffer& value)
{
stream.write((const char*)value.data(), value.size());
return stream;
}
}
using AK::ByteBuffer;

View file

@ -28,8 +28,13 @@
#include <AK/GenericLexer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/kstdio.h>
#include <ctype.h>
#if defined(__serenity__) && !defined(KERNEL)
# include <serenity.h>
#endif
#ifdef KERNEL
# include <Kernel/Process.h>
# include <Kernel/Thread.h>
@ -416,12 +421,6 @@ void vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams p
vformat_impl(params, fmtbuilder, parser);
}
void vformat(const LogStream& stream, StringView fmtstr, TypeErasedFormatParams params)
{
StringBuilder builder;
vformat(builder, fmtstr, params);
stream << builder.to_string();
}
void StandardFormatter::parse(TypeErasedFormatParams& params, FormatParser& parser)
{

View file

@ -364,7 +364,6 @@ struct Formatter<std::nullptr_t> : Formatter<FlatPtr> {
};
void vformat(StringBuilder&, StringView fmtstr, TypeErasedFormatParams);
void vformat(const LogStream& stream, StringView fmtstr, TypeErasedFormatParams);
#ifndef KERNEL
void vout(FILE*, StringView fmtstr, TypeErasedFormatParams, bool newline = false);

View file

@ -32,12 +32,10 @@ namespace AK {
class Bitmap;
class ByteBuffer;
class DebugLogStream;
class IPv4Address;
class JsonArray;
class JsonObject;
class JsonValue;
class LogStream;
class StackInfo;
class String;
class StringBuilder;
@ -143,7 +141,6 @@ using AK::ByteBuffer;
using AK::Bytes;
using AK::CircularDuplexStream;
using AK::CircularQueue;
using AK::DebugLogStream;
using AK::DoublyLinkedList;
using AK::DuplexMemoryStream;
using AK::FlyString;
@ -158,7 +155,6 @@ using AK::IPv4Address;
using AK::JsonArray;
using AK::JsonObject;
using AK::JsonValue;
using AK::LogStream;
using AK::NonnullOwnPtr;
using AK::NonnullOwnPtrVector;
using AK::NonnullRefPtr;

View file

@ -27,7 +27,6 @@
#pragma once
#include <AK/HashFunctions.h>
#include <AK/LogStream.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
#include <AK/kmalloc.h>

View file

@ -26,6 +26,7 @@
#pragma once
#include <AK/Forward.h>
#include <AK/HashTable.h>
namespace AK {

View file

@ -27,7 +27,6 @@
#pragma once
#include <AK/Endian.h>
#include <AK/LogStream.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
@ -143,11 +142,6 @@ struct Traits<IPv4Address> : public GenericTraits<IPv4Address> {
static constexpr unsigned hash(const IPv4Address& address) { return int_hash(address.to_u32()); }
};
inline const LogStream& operator<<(const LogStream& stream, const IPv4Address& value)
{
return stream << value.to_string();
}
template<>
struct Formatter<IPv4Address> : Formatter<String> {
void format(FormatBuilder& builder, IPv4Address value)

View file

@ -1,240 +0,0 @@
/*
* 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.
*/
#include <AK/FlyString.h>
#include <AK/LogStream.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#ifdef KERNEL
# include <Kernel/Process.h>
# include <Kernel/Thread.h>
#endif
namespace AK {
const LogStream& operator<<(const LogStream& stream, const String& value)
{
stream.write(value.characters(), value.length());
return stream;
}
const LogStream& operator<<(const LogStream& stream, const FlyString& value)
{
return stream << value.view();
}
const LogStream& operator<<(const LogStream& stream, const StringView& value)
{
stream.write(value.characters_without_null_termination(), value.length());
return stream;
}
const LogStream& operator<<(const LogStream& stream, int value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%d", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, long value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%ld", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, long long value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%lld", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, unsigned value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%u", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, unsigned long long value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%llu", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, unsigned long value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%lu", value);
return stream << buffer;
}
const LogStream& operator<<(const LogStream& stream, const void* value)
{
char buffer[32];
snprintf(buffer, sizeof(buffer), "%p", value);
return stream << buffer;
}
#if defined(__serenity__) && !defined(KERNEL)
static TriState got_process_name = TriState::Unknown;
static char process_name_buffer[256];
#endif
DebugLogStream dbg()
{
DebugLogStream stream;
// FIXME: This logic is redundant with the stuff in Format.cpp.
#if defined(__serenity__) && !defined(KERNEL)
if (got_process_name == TriState::Unknown) {
if (get_process_name(process_name_buffer, sizeof(process_name_buffer)) == 0)
got_process_name = TriState::True;
else
got_process_name = TriState::False;
}
if (got_process_name == TriState::True)
stream << "\033[33;1m" << process_name_buffer << '(' << getpid() << ")\033[0m: ";
#endif
#if defined(__serenity__) && defined(KERNEL)
if (Kernel::Processor::is_initialized() && Kernel::Thread::current())
stream << "\033[34;1m[#" << Kernel::Processor::id() << " " << *Kernel::Thread::current() << "]\033[0m: ";
else
stream << "\033[36;1m[Kernel]\033[0m: ";
#endif
return stream;
}
#ifdef KERNEL
KernelLogStream klog()
{
KernelLogStream stream;
if (Kernel::Processor::is_initialized() && Kernel::Thread::current())
stream << "\033[34;1m[#" << Kernel::Processor::id() << " " << *Kernel::Thread::current() << "]\033[0m: ";
else
stream << "\033[36;1m[Kernel]\033[0m: ";
return stream;
}
#else
DebugLogStream klog()
{
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return dbg();
# pragma GCC diagnostic pop
}
#endif
#ifdef KERNEL
KernelLogStream::~KernelLogStream()
{
if (!empty()) {
char newline = '\n';
write(&newline, 1);
kernelputstr(reinterpret_cast<char*>(data()), size());
}
}
#endif
DebugLogStream::~DebugLogStream()
{
if (!empty() && s_enabled) {
char newline = '\n';
write(&newline, 1);
dbgputstr(reinterpret_cast<char*>(data()), size());
}
}
void DebugLogStream::set_enabled(bool enabled)
{
s_enabled = enabled;
}
bool DebugLogStream::is_enabled()
{
return s_enabled;
}
bool DebugLogStream::s_enabled = true;
#ifndef KERNEL
const LogStream& operator<<(const LogStream& stream, double value)
{
return stream << String::format("%.4f", value);
}
const LogStream& operator<<(const LogStream& stream, float value)
{
return stream << String::format("%.4f", value);
}
#endif
void dump_bytes(ReadonlyBytes bytes)
{
StringBuilder builder;
u8 buffered_byte = 0;
size_t nrepeat = 0;
const char* prefix = "";
auto flush = [&]() {
if (nrepeat > 0) {
if (nrepeat == 1)
builder.appendff("{}{:#02x}", prefix, static_cast<int>(buffered_byte));
else
builder.appendff("{}{} * {:#02x}", prefix, nrepeat, static_cast<int>(buffered_byte));
nrepeat = 0;
prefix = ", ";
}
};
builder.append("{ ");
for (auto byte : bytes) {
if (nrepeat > 0) {
if (byte != buffered_byte)
flush();
buffered_byte = byte;
nrepeat++;
} else {
buffered_byte = byte;
nrepeat = 1;
}
}
flush();
builder.append(" }");
dbgln("{}", builder.string_view());
}
}

View file

@ -1,196 +0,0 @@
/*
* 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.
*/
#pragma once
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/Types.h>
#include <AK/kmalloc.h>
#include <AK/kstdio.h>
#if !defined(KERNEL)
# include <AK/ScopedValueRollback.h>
# include <AK/StringView.h>
# include <errno.h>
# include <unistd.h>
#endif
namespace AK {
class LogStream {
public:
LogStream()
#if !defined(KERNEL)
: m_errno_restorer(errno)
#endif
{
}
virtual ~LogStream() = default;
virtual void write(const char*, int) const = 0;
private:
#if !defined(KERNEL)
ScopedValueRollback<int> m_errno_restorer;
#endif
};
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; }
public:
BufferedLogStream() = default;
virtual ~BufferedLogStream() override
{
if (m_capacity > sizeof(u.m_local_buffer))
kfree(u.m_buffer);
}
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() = default;
virtual ~DebugLogStream() override;
// 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;
};
#ifdef KERNEL
class KernelLogStream final : public BufferedLogStream {
public:
KernelLogStream() = default;
virtual ~KernelLogStream() override;
};
#endif
inline const LogStream& operator<<(const LogStream& stream, const char* value)
{
if (!value)
return stream << "(null)";
int length = 0;
const char* p = value;
while (*(p++))
++length;
stream.write(value, length);
return stream;
}
const LogStream& operator<<(const LogStream&, const FlyString&);
const LogStream& operator<<(const LogStream&, const String&);
const LogStream& operator<<(const LogStream&, const StringView&);
const LogStream& operator<<(const LogStream&, int);
const LogStream& operator<<(const LogStream&, long);
const LogStream& operator<<(const LogStream&, unsigned);
const LogStream& operator<<(const LogStream&, long long);
const LogStream& operator<<(const LogStream&, unsigned long);
const LogStream& operator<<(const LogStream&, unsigned long long);
#if !defined(KERNEL)
const LogStream& operator<<(const LogStream&, double);
const LogStream& operator<<(const LogStream&, float);
#endif
template<typename T>
const LogStream& operator<<(const LogStream& stream, Span<T> span)
{
return stream << "{ " << span.data() << ", " << span.size() << " }";
}
const LogStream& operator<<(const LogStream&, const void*);
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");
}
[[deprecated("Plase use dbgln in AK/Format.h instead.")]] DebugLogStream dbg();
#ifdef KERNEL
KernelLogStream klog();
#else
DebugLogStream klog();
#endif
void dump_bytes(ReadonlyBytes);
}
using AK::dbg;
using AK::klog;
using AK::LogStream;

View file

@ -27,6 +27,7 @@
#include <AK/MappedFile.h>
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

View file

@ -27,7 +27,7 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/LogStream.h>
#include <AK/Format.h>
#include <AK/RefCounted.h>
#include <AK/StdLibExtras.h>
#include <AK/Traits.h>
@ -183,12 +183,6 @@ struct Traits<NonnullOwnPtr<T>> : public GenericTraits<NonnullOwnPtr<T>> {
static bool equals(const NonnullOwnPtr<T>& a, const NonnullOwnPtr<T>& b) { return a.ptr() == b.ptr(); }
};
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const NonnullOwnPtr<T>& value)
{
return stream << value.ptr();
}
template<typename T, typename U>
inline void swap(NonnullOwnPtr<T>& a, NonnullOwnPtr<U>& b)
{

View file

@ -28,7 +28,7 @@
#include <AK/Assertions.h>
#include <AK/Atomic.h>
#include <AK/LogStream.h>
#include <AK/Format.h>
#include <AK/Types.h>
#ifdef KERNEL
# include <Kernel/Arch/i386/CPU.h>
@ -339,12 +339,6 @@ inline NonnullRefPtr<T> adopt(T& object)
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
}
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr<T>& value)
{
return stream << value.ptr();
}
template<typename T>
struct Formatter<NonnullRefPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const NonnullRefPtr<T>& value)

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/Format.h>
#include <errno.h>
#include <string.h>
namespace AK {

View file

@ -218,12 +218,6 @@ struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
static bool equals(const OwnPtr<T>& a, const OwnPtr<T>& b) { return a.ptr() == b.ptr(); }
};
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const OwnPtr<T>& value)
{
return stream << value.ptr();
}
}
using AK::OwnPtr;

View file

@ -61,6 +61,7 @@
#define FLATTEN [[gnu::flatten]]
#ifndef __serenity__
# include <unistd.h>
# define PAGE_SIZE sysconf(_SC_PAGESIZE)
#endif

View file

@ -27,7 +27,7 @@
#pragma once
#include <AK/Atomic.h>
#include <AK/LogStream.h>
#include <AK/Format.h>
#include <AK/NonnullRefPtr.h>
#include <AK/StdLibExtras.h>
#include <AK/Traits.h>
@ -461,12 +461,6 @@ private:
mutable Atomic<FlatPtr> m_bits { PtrTraits::default_null_value };
};
template<typename T, typename PtrTraits = RefPtrTraits<T>>
inline const LogStream& operator<<(const LogStream& stream, const RefPtr<T, PtrTraits>& value)
{
return stream << value.ptr();
}
template<typename T>
struct Formatter<RefPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const RefPtr<T>& value)

View file

@ -26,7 +26,6 @@
#include <AK/Assertions.h>
#include <AK/Checked.h>
#include <AK/LogStream.h>
#include <AK/Time.h>
// Make a reasonable guess as to which timespec/timeval definition to use.

View file

@ -105,11 +105,6 @@ private:
String m_data_payload;
};
inline const LogStream& operator<<(const LogStream& stream, const URL& value)
{
return stream << value.to_string();
}
template<>
struct Formatter<URL> : Formatter<StringView> {
void format(FormatBuilder& builder, const URL& value)

View file

@ -25,7 +25,7 @@
*/
#include <AK/Assertions.h>
#include <AK/LogStream.h>
#include <AK/Format.h>
#include <AK/Utf8View.h>
namespace AK {

View file

@ -26,7 +26,6 @@
#pragma once
#include <AK/LogStream.h>
#include <AK/Weakable.h>
namespace AK {
@ -236,17 +235,6 @@ inline WeakPtr<U> Weakable<T>::make_weak_ptr() const
return weak_ptr;
}
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const WeakPtr<T>& value)
{
#ifdef KERNEL
auto ref = value.strong_ref();
return stream << ref.ptr();
#else
return stream << value.ptr();
#endif
}
template<typename T>
struct Formatter<WeakPtr<T>> : Formatter<const T*> {
void format(FormatBuilder& builder, const WeakPtr<T>& value)

View file

@ -33,21 +33,20 @@
# include <AK/Types.h>
# include <stdarg.h>
extern "C" {
int dbgputstr(const char*, ssize_t);
void dbgputstr(const char*, size_t);
int sprintf(char* buf, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
int snprintf(char* buffer, size_t, const char* fmt, ...) __attribute__((format(printf, 3, 4)));
}
# endif
#else
# include <stdio.h>
inline int dbgputstr(const char* characters, ssize_t length)
inline void dbgputstr(const char* characters, size_t length)
{
fwrite(characters, 1, length, stderr);
return 0;
}
#endif
template<size_t N>
inline int dbgputstr(const char (&array)[N])
inline void dbgputstr(const char (&array)[N])
{
return ::dbgputstr(array, N);
}