summaryrefslogtreecommitdiff
path: root/AK/LogStream.h
blob: e18e3fef5a8d92681f99109489736a990d5d2601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

#include <AK/Types.h>
#include <AK/kstdio.h>

#ifndef KERNEL
#    include <AK/ScopedValueRollback.h>
#    include <AK/StringView.h>
#    include <errno.h>
#    include <unistd.h>
#endif

namespace AK {

class String;
class StringView;

class LogStream {
public:
    LogStream()
#ifndef KERNEL
        : m_errno_restorer(errno)
#endif
    {
    }
    virtual ~LogStream() {}

    virtual void write(const char*, int) const = 0;

private:
#ifndef KERNEL
    ScopedValueRollback<int> m_errno_restorer;
#endif
};

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
    {
        dbgputstr(characters, length);
    }
};

inline const LogStream& operator<<(const LogStream& stream, const char* value)
{
    int length = 0;
    const char* p = value;
    while (*(p++))
        ++length;
    stream.write(value, length);
    return stream;
}

const LogStream& operator<<(const LogStream&, const String&);
const LogStream& operator<<(const LogStream&, const StringView&);
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

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");
}

DebugLogStream dbg();

}

using AK::dbg;
using AK::LogStream;