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/FlyString.h>
28#include <AK/LogStream.h>
29#include <AK/String.h>
30#include <AK/StringView.h>
31
32#ifdef KERNEL
33# include <Kernel/Process.h>
34# include <Kernel/Thread.h>
35#endif
36
37#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
38#include <stdio.h>
39#endif
40
41namespace AK {
42
43const LogStream& operator<<(const LogStream& stream, const String& value)
44{
45 stream.write(value.characters(), value.length());
46 return stream;
47}
48
49const LogStream& operator<<(const LogStream& stream, const FlyString& value)
50{
51 return stream << value.view();
52}
53
54const LogStream& operator<<(const LogStream& stream, const StringView& value)
55{
56 stream.write(value.characters_without_null_termination(), value.length());
57 return stream;
58}
59
60const LogStream& operator<<(const LogStream& stream, int value)
61{
62 char buffer[32];
63 sprintf(buffer, "%d", value);
64 return stream << buffer;
65}
66
67const LogStream& operator<<(const LogStream& stream, long value)
68{
69 char buffer[32];
70 sprintf(buffer, "%ld", value);
71 return stream << buffer;
72}
73
74const LogStream& operator<<(const LogStream& stream, long long value)
75{
76 char buffer[32];
77 sprintf(buffer, "%lld", value);
78 return stream << buffer;
79}
80
81const LogStream& operator<<(const LogStream& stream, unsigned value)
82{
83 char buffer[32];
84 sprintf(buffer, "%u", value);
85 return stream << buffer;
86}
87
88const LogStream& operator<<(const LogStream& stream, unsigned long long value)
89{
90 char buffer[32];
91 sprintf(buffer, "%llu", value);
92 return stream << buffer;
93}
94
95const LogStream& operator<<(const LogStream& stream, unsigned long value)
96{
97 char buffer[32];
98 sprintf(buffer, "%lu", value);
99 return stream << buffer;
100}
101
102const LogStream& operator<<(const LogStream& stream, const void* value)
103{
104 char buffer[32];
105 sprintf(buffer, "%p", value);
106 return stream << buffer;
107}
108
109#if (defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)) || defined(__OpenBSD__)
110static TriState got_process_name = TriState::Unknown;
111static char process_name_buffer[256];
112#endif
113
114static bool s_dbg_raw = false;
115
116void dbg_raw(bool val)
117{
118 s_dbg_raw = val;
119}
120
121DebugLogStream dbg()
122{
123 DebugLogStream stream;
124#if (defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)) || defined(__OpenBSD__)
125 if (s_dbg_raw)
126 return stream;
127
128 if (got_process_name == TriState::Unknown) {
129 if (get_process_name(process_name_buffer, sizeof(process_name_buffer)) == 0)
130 got_process_name = TriState::True;
131 else
132 got_process_name = TriState::False;
133 }
134 if (got_process_name == TriState::True)
135 stream <<
136#ifndef __OpenBSD__
137 "\033[33;1m" <<
138#endif
139 process_name_buffer << '(' << getpid() << ")" <<
140#ifndef __OpenBSD__
141 "\033[0m" <<
142#endif
143 ": ";
144#endif
145#if defined(__serenity__) && defined(KERNEL) && !defined(BOOTSTRAPPER)
146 if (Kernel::Thread::current)
147 stream << "\033[34;1m[" << *Kernel::Thread::current << "]\033[0m: ";
148 else
149 stream << "\033[36;1m[Kernel]\033[0m: ";
150#endif
151#if defined(BOOTSTRAPPER) && !defined(__serenity__) && !defined(KERNEL)
152 stream << "\033[36;1m[Bootstrapper]\033[0m: ";
153#endif
154 return stream;
155}
156
157#if defined(KERNEL)
158KernelLogStream klog()
159{
160 KernelLogStream stream;
161 if (Kernel::Thread::current)
162 stream << "\033[34;1m[" << *Kernel::Thread::current << "]\033[0m: ";
163 else
164 stream << "\033[36;1m[Kernel]\033[0m: ";
165 return stream;
166}
167#elif !defined(BOOTSTRAPPER)
168DebugLogStream klog()
169{
170 return dbg();
171}
172#endif
173
174#if defined(KERNEL)
175KernelLogStream::~KernelLogStream()
176{
177 char newline = '\n';
178 write(&newline, 1);
179}
180#endif
181
182DebugLogStream::~DebugLogStream()
183{
184 char newline = '\n';
185 write(&newline, 1);
186}
187
188#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
189StdLogStream::~StdLogStream()
190{
191 char newline = '\n';
192 write(&newline, 1);
193}
194
195void StdLogStream::write(const char* characters, int length) const
196{
197 if (::write(m_fd, characters, length) < 0) {
198 perror("StdLogStream::write");
199 ASSERT_NOT_REACHED();
200 }
201}
202#endif
203
204}