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
|
|
|
|
|
2019-12-09 16:45:11 +00:00
|
|
|
#include <AK/Types.h>
|
2019-07-04 04:43:30 +00:00
|
|
|
#include <AK/kstdio.h>
|
|
|
|
|
2020-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
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 {
|
|
|
|
|
|
|
|
class String;
|
|
|
|
class StringView;
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
class LogStream {
|
|
|
|
public:
|
2019-07-25 05:00:33 +00:00
|
|
|
LogStream()
|
2020-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
2019-07-25 05:00:33 +00:00
|
|
|
: m_errno_restorer(errno)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2019-07-04 04:43:30 +00:00
|
|
|
virtual ~LogStream() {}
|
|
|
|
|
|
|
|
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-02-09 14:50:13 +00:00
|
|
|
#if !defined(KERNEL) && !defined(BOOTSTRAPPER)
|
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
|
|
|
};
|
|
|
|
|
|
|
|
class DebugLogStream final : public LogStream {
|
|
|
|
public:
|
|
|
|
DebugLogStream() {}
|
2020-02-14 20:41:10 +00:00
|
|
|
virtual ~DebugLogStream() override;
|
2019-07-04 04:43:30 +00:00
|
|
|
|
|
|
|
virtual void write(const char* characters, int length) const override
|
|
|
|
{
|
2019-07-21 19:43:37 +00:00
|
|
|
dbgputstr(characters, length);
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const char* value)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
|
2019-07-04 04:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
using AK::dbg;
|
2019-07-15 18:22:30 +00:00
|
|
|
using AK::LogStream;
|