Skip to content

Commit

Permalink
Merge pull request #21 from contour-terminal/improvement/logging
Browse files Browse the repository at this point in the history
use crispy/logstore for logging
  • Loading branch information
christianparpart authored Jan 20, 2024
2 parents 7a39380 + 43f845a commit c2269bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 67 deletions.
19 changes: 7 additions & 12 deletions src/shell/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
module;

#include <fmt/format.h>

#include <crispy/logstore.h>
#include <cstring>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

// clang-format off
#if defined(ENDO_TRACE_LEXER)
#define TRACE(message, ...) do { ::fmt::print("Lexer: " message, __VA_ARGS__); } while (0)
#else
#define TRACE(message, ...) do {} while (0)
#endif
// clang-format on

auto inline lexerLog = logstore::category("lexer ", "Lexer logger ", logstore::category::state::Enabled );

using namespace std::string_view_literals;

Expand Down Expand Up @@ -160,7 +155,7 @@ export class Lexer
Token nextToken()
{
// auto const postLogger = crispy::finally { [this]() mutable {
// TRACE("Lexer.nextToken ~> {}\n", _currentToken);
// lexerLog()("Lexer.nextToken ~> {}\n", _currentToken);
// } };

consumeWhitespace();
Expand Down Expand Up @@ -291,9 +286,9 @@ export class Lexer
{
_nextToken.literal += static_cast<char>(_currentChar); // TODO: UTF-8
nextChar();
// TRACE("consumeIdentifier: '{}'\n", (char) _currentChar);
// lexerLog()("consumeIdentifier: '{}'\n", (char) _currentChar);
}
TRACE("consumeIdentifier: result: '{}'\n", _nextToken.literal);
lexerLog()("consumeIdentifier: result: '{}'\n", _nextToken.literal);
return confirmToken(token);
}

Expand All @@ -315,7 +310,7 @@ export class Lexer
char32_t nextChar()
{
_currentChar = _source->readChar();
// TRACE("Lexer.nextChar: '{}'\n", (char) _currentChar);
// lexerLog()("Lexer.nextChar: '{}'\n", (char) _currentChar);
return _currentChar;
}

Expand Down
19 changes: 6 additions & 13 deletions src/shell/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@ module;
#include <shell/AST.h>

#include <crispy/utils.h>
#include <crispy/logstore.h>

#include <memory>
#include <optional>

// {{{ trace macros
// clang-format off
#if defined(ENDO_TRACE_PARSER)
#define TRACE_SCOPE(message) ScopedLogger _logger { message }
#define TRACE_FMT(message, ...) do { ScopedLogger::write(::fmt::format(message, __VA_ARGS__)); } while (0)
#define TRACE(message) do { ScopedLogger::write(::fmt::format(message)); } while (0)
#else
#define TRACE_SCOPE(message) do {} while (0)
#define TRACE_FMT(message, ...) do {} while (0)
#define TRACE(message) do {} while (0)
#endif
// clang-format on
// }}}
auto inline parserLog = logstore::category("parser", "Parser logger", logstore::category::state::Enabled);
#define TRACE_SCOPE(message) ScopedLogger _logger { message, parserLog };
#define TRACE_FMT(message, ...) do { parserLog()(ScopedLogger::write(::fmt::format(message, __VA_ARGS__))); } while (0)
#define TRACE(message) do { parserLog()(ScopedLogger::write(::fmt::format(message))); } while (0)


import ASTPrinter;
import Lexer;
Expand Down
11 changes: 7 additions & 4 deletions src/shell/ScopedLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <fmt/format.h>
#include <crispy/logstore.h>
#include <string>

struct ScopedLogger
Expand All @@ -16,19 +17,21 @@ struct ScopedLogger
return result;
}

static void write(std::string message) { fmt::print("{}{}\n", indentation(), message); }
static auto write(std::string message) { return fmt::format("{}{}\n", indentation(), message); }
auto writeInternal(std::string message) { _category()("{}{}\n", indentation(), message); }

ScopedLogger(std::string message): _message(std::move(message))
ScopedLogger(std::string message, auto&& log): _message(std::move(message)), _category(log)
{
++depth;
write("{{ " + _message);
writeInternal("{{ " + _message);
}

~ScopedLogger()
{
write("}} " + _message);
writeInternal("}} " + _message);
--depth;
}

std::string _message;
logstore::category const& _category;
};
50 changes: 14 additions & 36 deletions src/shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ import CoreVM;

export module Shell;

// {{{ trace macros
// clang-format off
#if defined(ENDO_TRACE)
#define DEBUG(msg) do { fmt::print("{}\n", (msg)); } while (0)
#define DEBUGF(msg, ...) do { fmt::print("{}\n", fmt::format((msg), __VA_ARGS__)); } while (0)
#else
#define DEBUG(msg) do {} while (0)
#define DEBUGF(msg, ...) do {} while (0)
#endif
// clang-format on
// }}}
auto inline debugLog = logstore::category("debug ", "Debug log", logstore::category::state::Enabled);

namespace endo
{
Expand Down Expand Up @@ -208,7 +198,7 @@ export class Shell final: public CoreVM::Runtime
while (!_quit && prompt.ready())
{
auto const lineBuffer = prompt.read();
DEBUGF("input buffer: {}", lineBuffer);
debugLog()("input buffer: {}", lineBuffer);

_exitCode = execute(lineBuffer);
// _tty.writeToStdout("exit code: {}\n", _exitCode);
Expand All @@ -229,7 +219,7 @@ export class Shell final: public CoreVM::Runtime
return EXIT_FAILURE;
}

DEBUGF("Parsed & printed: {}", endo::ast::ASTPrinter::print(*rootNode));
debugLog()("Parsed & printed: {}", endo::ast::ASTPrinter::print(*rootNode));

CoreVM::IRProgram* irProgram = IRGenerator::generate(*rootNode);
if (irProgram == nullptr)
Expand All @@ -255,12 +245,10 @@ export class Shell final: public CoreVM::Runtime
pm.run(irProgram);
}

if (_debugIR)
{
DEBUG("================================================\n");
DEBUG("Optimized IR program:\n");
debugLog()("================================================\n");
debugLog()("Optimized IR program:\n");
if (debugLog.is_enabled())
irProgram->dump();
}

_currentProgram = CoreVM::TargetCodeGenerator {}.generate(irProgram);
if (!_currentProgram)
Expand All @@ -270,12 +258,10 @@ export class Shell final: public CoreVM::Runtime
}
_currentProgram->link(this, &report);

if (_debugIR)
{
DEBUG("================================================\n");
DEBUG("Linked target code:\n");
debugLog()("================================================\n");
debugLog()("Linked target code:\n");
if (debugLog.is_enabled())
_currentProgram->dump();
}

CoreVM::Handler* main = _currentProgram->findHandler("@main");
assert(main != nullptr);
Expand Down Expand Up @@ -404,7 +390,7 @@ export class Shell final: public CoreVM::Runtime
// TODO: setup redirects
// CoreVM::CoreIntArray const& outputRedirects = context.getIntArray(1);
// for (size_t i = 0; i + 2 < outputRedirects.size(); i += 2)
// DEBUGF("redirect: {} -> {}\n", outputRedirects[i], outputRedirects[i + 1]);
// debugLog()("redirect: {} -> {}\n", outputRedirects[i], outputRedirects[i + 1]);

pid_t const pid = fork();
switch (pid)
Expand Down Expand Up @@ -461,7 +447,7 @@ export class Shell final: public CoreVM::Runtime
// TODO: setup redirects
// CoreVM::CoreIntArray const& outputRedirects = context.getIntArray(1);
// for (size_t i = 0; i + 2 < outputRedirects.size(); i += 2)
// DEBUGF("redirect: {} -> {}\n", outputRedirects[i], outputRedirects[i + 1]);
// debugLog()("redirect: {} -> {}\n", outputRedirects[i], outputRedirects[i + 1]);

pid_t const pid = fork();
switch (pid)
Expand Down Expand Up @@ -612,7 +598,7 @@ export class Shell final: public CoreVM::Runtime
auto const programPath = path / program;
if (std::filesystem::exists(programPath))
{
DEBUGF("Found program: {}", programPath.string());
debugLog()("Found program: {}", programPath.string());
return programPath;
}
}
Expand All @@ -622,9 +608,8 @@ export class Shell final: public CoreVM::Runtime

void trace(CoreVM::Instruction instr, size_t ip, size_t sp)
{
if (_traceVM)
std::cerr << fmt::format("trace: {}\n",
CoreVM::disassemble(instr, ip, sp, &_currentProgram->constants()));
debugLog()(
fmt::format("trace: {}\n", CoreVM::disassemble(instr, ip, sp, &_currentProgram->constants())));
}

template <typename... Args>
Expand All @@ -641,13 +626,6 @@ export class Shell final: public CoreVM::Runtime
std::unique_ptr<CoreVM::Program> _currentProgram;
CoreVM::Runner::Globals _globals;

#if defined(ENDO_TRACE_VM)
bool _debugIR = true;
bool _traceVM = true;
#else
bool _debugIR = false;
bool _traceVM = false;
#endif
bool _optimize = false;

PipelineBuilder _currentPipelineBuilder;
Expand Down
6 changes: 4 additions & 2 deletions src/shell/UnixPipe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
module;
#include <fmt/format.h>
#include <crispy/logstore.h>

#include <cstring>
#include <stdexcept>
Expand All @@ -13,13 +14,14 @@ export module UnixPipe;
namespace endo
{

auto inline pipeLog = logstore::category("pipe ", "Unix pipe log", logstore::category::state::Enabled);
using namespace std::string_literals;

export inline void saveClose(int* fd) noexcept
{
if (fd && *fd != -1)
{
fmt::print("Closing fd {}\n", *fd);
pipeLog()("Closing fd {}\n", *fd);
::close(*fd);
*fd = -1;
}
Expand All @@ -41,7 +43,7 @@ export struct UnixPipe
if (!detail::setFileFlags(fd, flags))
break;
#endif
fmt::print("Created pipe: {} {}\n", pfd[0], pfd[1]);
pipeLog()("Created pipe: {} {}\n", pfd[0], pfd[1]);
}

inline UnixPipe(UnixPipe&& v) noexcept: pfd { v.pfd[0], v.pfd[1] }
Expand Down

0 comments on commit c2269bc

Please sign in to comment.