1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 07:34:57 +00:00

AK: Don't add newline for outf/dbgf/warnf.

In the future all (normal) output should be written by any of the
following functions:

    out    (currently called new_out)
    outln
    dbg    (currently called new_dbg)
    dbgln
    warn   (currently called new_warn)
    warnln

However, there are still a ton of uses of the old out/warn/dbg in the
code base so the new functions are called new_out/new_warn/new_dbg. I am
going to rename them as soon as all the other usages are gone (this
might take a while.)

I also added raw_out/raw_dbg/raw_warn which don't do any escaping,
this should be useful if no formatting is required and if the input
contains tons of curly braces. (I am not entirely sure if this function
will stay, but I am adding it for now.)
This commit is contained in:
asynts 2020-10-04 15:35:43 +02:00 committed by Andreas Kling
parent 4237089a21
commit d5ffb51a83
16 changed files with 202 additions and 96 deletions

View file

@ -30,6 +30,14 @@
#include <AK/StringBuilder.h>
#include <ctype.h>
#ifdef KERNEL
# include <Kernel/Process.h>
# include <Kernel/Thread.h>
#else
# include <stdio.h>
# include <unistd.h>
#endif
namespace AK {
namespace {
@ -540,6 +548,81 @@ void Formatter<bool>::format(TypeErasedFormatParams& params, FormatBuilder& buil
}
}
#ifndef KERNEL
void raw_out(StringView string)
{
const auto retval = ::fwrite(string.characters_without_null_termination(), 1, string.length(), stdout);
ASSERT(retval == string.length());
}
void vout(StringView fmtstr, TypeErasedFormatParams params, bool newline)
{
StringBuilder builder;
vformat(builder, fmtstr, params);
if (newline && !builder.is_empty())
builder.append('\n');
raw_out(builder.to_string());
}
void raw_warn(StringView string)
{
const auto retval = ::write(STDERR_FILENO, string.characters_without_null_termination(), string.length());
ASSERT(static_cast<size_t>(retval) == string.length());
}
void vwarn(StringView fmtstr, TypeErasedFormatParams params, bool newline)
{
StringBuilder builder;
vformat(builder, fmtstr, params);
if (newline && !builder.is_empty())
builder.append('\n');
raw_warn(builder.to_string());
}
#endif
void raw_dbg(StringView string)
{
const auto retval = dbgputstr(string.characters_without_null_termination(), string.length());
ASSERT(static_cast<size_t>(retval) == string.length());
}
void vdbg(StringView fmtstr, TypeErasedFormatParams params, bool newline)
{
StringBuilder builder;
// FIXME: This logic is redundant with the stuff in LogStream.cpp.
#if defined(__serenity__)
# ifdef KERNEL
if (Kernel::Processor::is_initialized() && Kernel::Thread::current()) {
auto& thread = *Kernel::Thread::current();
builder.appendff("\033[34;1m[{}({}:{})]\033[0m: ", thread.process().name(), thread.pid().value(), thread.tid().value());
} else {
builder.appendff("\033[34;1m[Kernel]\033[0m: ");
}
# else
static TriState got_process_name = TriState::Unknown;
static char process_name_buffer[256];
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)
builder.appendff("\033[33;1m{}({})\033[0m: ", process_name_buffer, getpid());
# endif
#endif
vformat(builder, fmtstr, params);
if (newline && !builder.is_empty())
builder.append('\n');
raw_dbg(builder.to_string());
}
template struct Formatter<unsigned char, void>;
template struct Formatter<unsigned short, void>;
template struct Formatter<unsigned int, void>;