Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/LogStream.h>
28#include <AK/String.h>
29#include <AK/StringView.h>
30
31#ifdef KERNEL
32# include <Kernel/Process.h>
33# include <Kernel/Thread.h>
34#endif
35
36namespace AK {
37
38const LogStream& operator<<(const LogStream& stream, const String& value)
39{
40 stream.write(value.characters(), value.length());
41 return stream;
42}
43
44const LogStream& operator<<(const LogStream& stream, const StringView& value)
45{
46 stream.write(value.characters_without_null_termination(), value.length());
47 return stream;
48}
49
50const LogStream& operator<<(const LogStream& stream, int value)
51{
52 char buffer[32];
53 sprintf(buffer, "%d", value);
54 return stream << buffer;
55}
56
57const LogStream& operator<<(const LogStream& stream, long value)
58{
59 char buffer[32];
60 sprintf(buffer, "%ld", value);
61 return stream << buffer;
62}
63
64const LogStream& operator<<(const LogStream& stream, long long value)
65{
66 char buffer[32];
67 sprintf(buffer, "%lld", value);
68 return stream << buffer;
69}
70
71const LogStream& operator<<(const LogStream& stream, unsigned value)
72{
73 char buffer[32];
74 sprintf(buffer, "%u", value);
75 return stream << buffer;
76}
77
78const LogStream& operator<<(const LogStream& stream, unsigned long long value)
79{
80 char buffer[32];
81 sprintf(buffer, "%llu", value);
82 return stream << buffer;
83}
84
85const LogStream& operator<<(const LogStream& stream, unsigned long value)
86{
87 char buffer[32];
88 sprintf(buffer, "%lu", value);
89 return stream << buffer;
90}
91
92const LogStream& operator<<(const LogStream& stream, const void* value)
93{
94 char buffer[32];
95 sprintf(buffer, "%p", value);
96 return stream << buffer;
97}
98
99#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
100static TriState got_process_name = TriState::Unknown;
101static char process_name_buffer[256];
102#endif
103
104DebugLogStream dbg()
105{
106 DebugLogStream stream;
107#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
108 if (got_process_name == TriState::Unknown) {
109 if (get_process_name(process_name_buffer, sizeof(process_name_buffer)) == 0)
110 got_process_name = TriState::True;
111 else
112 got_process_name = TriState::False;
113 }
114 if (got_process_name == TriState::True)
115 stream << "\033[33;1m" << process_name_buffer << '(' << getpid() << ")\033[0m: ";
116#endif
117#if defined(__serenity__) && defined(KERNEL) && !defined(BOOTSTRAPPER)
118 if (Kernel::Thread::current)
119 stream << "\033[34;1m[" << *Kernel::Thread::current << "]\033[0m: ";
120 else
121 stream << "\033[36;1m[Kernel]\033[0m: ";
122#endif
123#if defined(BOOTSTRAPPER) && !defined(__serenity__) && !defined(KERNEL)
124 stream << "\033[36;1m[Bootstrapper]\033[0m: ";
125#endif
126 return stream;
127}
128
129DebugLogStream::~DebugLogStream()
130{
131 char newline = '\n';
132 write(&newline, 1);
133}
134
135}