mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +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:
parent
4237089a21
commit
d5ffb51a83
16 changed files with 202 additions and 96 deletions
|
@ -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>;
|
||||
|
|
43
AK/Format.h
43
AK/Format.h
|
@ -299,4 +299,47 @@ struct Formatter<bool> : StandardFormatter {
|
|||
void vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams);
|
||||
void vformat(const LogStream& stream, StringView fmtstr, TypeErasedFormatParams);
|
||||
|
||||
#ifndef KERNEL
|
||||
void vout(StringView fmtstr, TypeErasedFormatParams, bool newline = false);
|
||||
void raw_out(StringView string);
|
||||
|
||||
// FIXME: Rename this function to 'out' when that name becomes avaliable.
|
||||
template<typename... Parameters>
|
||||
void new_out(StringView fmtstr, const Parameters&... parameters) { vout(fmtstr, VariadicFormatParams { parameters... }); }
|
||||
template<typename... Parameters>
|
||||
void outln(StringView fmtstr, const Parameters&... parameters) { vout(fmtstr, VariadicFormatParams { parameters... }, true); }
|
||||
|
||||
void vwarn(StringView fmtstr, TypeErasedFormatParams, bool newline = false);
|
||||
void raw_warn(StringView string);
|
||||
|
||||
// FIXME: Rename this function to 'warn' when that name becomes avaliable.
|
||||
template<typename... Parameters>
|
||||
void new_warn(StringView fmtstr, const Parameters&... parameters) { vwarn(fmtstr, VariadicFormatParams { parameters... }); }
|
||||
template<typename... Parameters>
|
||||
void warnln(StringView fmtstr, const Parameters&... parameters) { vwarn(fmtstr, VariadicFormatParams { parameters... }, true); }
|
||||
#endif
|
||||
|
||||
void vdbg(StringView fmtstr, TypeErasedFormatParams, bool newline = false);
|
||||
void raw_dbg(StringView string);
|
||||
|
||||
// FIXME: Rename this function to 'dbg' when that name becomes avaliable.
|
||||
template<typename... Parameters>
|
||||
void new_dbg(StringView fmtstr, const Parameters&... parameters) { vdbg(fmtstr, VariadicFormatParams { parameters... }); }
|
||||
template<typename... Parameters>
|
||||
void dbgln(StringView fmtstr, const Parameters&... parameters) { vdbg(fmtstr, VariadicFormatParams { parameters... }, true); }
|
||||
|
||||
} // namespace AK
|
||||
|
||||
#ifndef KERNEL
|
||||
using AK::new_out;
|
||||
using AK::outln;
|
||||
using AK::raw_out;
|
||||
|
||||
using AK::new_warn;
|
||||
using AK::raw_warn;
|
||||
using AK::warnln;
|
||||
#endif
|
||||
|
||||
using AK::dbgln;
|
||||
using AK::new_dbg;
|
||||
using AK::raw_dbg;
|
||||
|
|
|
@ -115,6 +115,8 @@ static char process_name_buffer[256];
|
|||
DebugLogStream dbg()
|
||||
{
|
||||
DebugLogStream stream;
|
||||
|
||||
// FIXME: This logic is redundant with the stuff in Format.cpp.
|
||||
#if defined(__serenity__) && !defined(KERNEL)
|
||||
if (got_process_name == TriState::Unknown) {
|
||||
if (get_process_name(process_name_buffer, sizeof(process_name_buffer)) == 0)
|
||||
|
|
|
@ -207,35 +207,13 @@ DebugLogStream klog();
|
|||
|
||||
void dump_bytes(ReadonlyBytes);
|
||||
|
||||
#ifndef KERNEL
|
||||
template<typename... Parameters>
|
||||
void outf(StringView fmtstr, const Parameters&... parameters)
|
||||
{
|
||||
vformat(out(), fmtstr, VariadicFormatParams { parameters... });
|
||||
}
|
||||
template<typename... Parameters>
|
||||
void warnf(StringView fmtstr, const Parameters&... parameters)
|
||||
{
|
||||
vformat(warn(), fmtstr, VariadicFormatParams { parameters... });
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename... Parameters>
|
||||
void dbgf(StringView fmtstr, const Parameters&... parameters)
|
||||
{
|
||||
vformat(dbg(), fmtstr, VariadicFormatParams { parameters... });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::dbg;
|
||||
using AK::dbgf;
|
||||
using AK::klog;
|
||||
using AK::LogStream;
|
||||
|
||||
#if !defined(KERNEL)
|
||||
using AK::out;
|
||||
using AK::outf;
|
||||
using AK::warn;
|
||||
using AK::warnf;
|
||||
#endif
|
||||
|
|
|
@ -199,13 +199,13 @@ void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
|
|||
for (const auto& t : tests) {
|
||||
const auto test_type = t.is_benchmark() ? "benchmark" : "test";
|
||||
|
||||
warnf("Running {} '{}'.", test_type, t.name());
|
||||
warnln("Running {} '{}'.", test_type, t.name());
|
||||
|
||||
TestElapsedTimer timer;
|
||||
t.func()();
|
||||
const auto time = timer.elapsed_milliseconds();
|
||||
|
||||
dbgf("Completed {} '{}' in {}ms", test_type, t.name(), time);
|
||||
dbgln("Completed {} '{}' in {}ms", test_type, t.name(), time);
|
||||
|
||||
if (t.is_benchmark()) {
|
||||
m_benchtime += time;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue