Skip to content

Commit

Permalink
Replace logger (#955)
Browse files Browse the repository at this point in the history
Replace Logger by a similar but improved infrastructure.

A follow-up PR will replace the diagnostics with this.

The Logger design is similar to before, however there are separate loggers dawn::log::info, dawn::log::warning, and dawn::log::error which are globals similar to std::cout and std::cerr.

Each logger has an LoggerProxy operator()(string, int) as before that returns the stream operator

Each logger also acts as a container that stores the messages (a la DiagnosticsEngine).

Instead of a virtual LoggerInterface that potentially leaks memory when registering other class instance pointers, this is replaced with a lightweight std::function object Logger::Formatter, so GTClang and other frameworks can register their own message formatting.

Loggers also take underlying std::ostreams, which can be customized to write to strings, files, etc.

Part of #950.

Integrates into ctest.
  • Loading branch information
jdahm authored May 12, 2020
1 parent fddfdf0 commit e06dcfd
Show file tree
Hide file tree
Showing 38 changed files with 670 additions and 410 deletions.
2 changes: 1 addition & 1 deletion dawn/src/dawn/CodeGen/CXXNaive-ico/CXXNaiveCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/StringUtil.h"
#include <algorithm>
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/CodeGen/CXXNaive/CXXNaiveCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/StringUtil.h"
#include <algorithm>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/CodeGen/Cuda/CudaCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "dawn/Support/Array.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/Iterator.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/StringUtil.h"
#include <algorithm>
#include <numeric>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/CodeGen/GridTools/GTCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/DiagnosticsEngine.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/StringUtil.h"
#include <map>
#include <optional>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Compiler/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "dawn/CodeGen/TranslationUnit.h"
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Iterator.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/StringSwitch.h"

#include "dawn/Optimizer/OptimizerContext.h"
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/IIR/DoMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "dawn/IIR/Stencil.h"
#include "dawn/IIR/StencilMetaInformation.h"
#include "dawn/Support/IndexGenerator.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include <limits>
#include <memory>

Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/IIR/Stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "dawn/IIR/StencilFunctionInstantiation.h"
#include "dawn/IIR/StencilMetaInformation.h"
#include "dawn/SIR/ASTVisitor.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include <algorithm>
#include <iterator>
#include <set>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/IIR/StencilFunctionInstantiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Casting.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/Printing.h"
#include "dawn/Support/Unreachable.h"
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/IIR/StencilInstantiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "dawn/Support/DiagnosticsEngine.h"
#include "dawn/Support/Format.h"
#include "dawn/Support/Json.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/Printing.h"
#include "dawn/Support/RemoveIf.hpp"
#include "dawn/Support/Twine.h"
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/OptimizerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "dawn/Optimizer/PassTemporaryType.h"
#include "dawn/Optimizer/StatementMapper.h"
#include "dawn/SIR/SIR.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/STLExtras.h"
#include <stack>

Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/PassFixVersionedInputFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "dawn/IIR/NodeUpdateType.h"
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"

#include <memory>

Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/PassInlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "dawn/IIR/InstantiationHelper.h"
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/STLExtras.h"
#include <iostream>
#include <stack>
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Support/Exception.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include <vector>

namespace dawn {
Expand Down
12 changes: 5 additions & 7 deletions dawn/src/dawn/Optimizer/PassRemoveScalars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/IIR/StencilMetaInformation.h"
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"

#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -234,8 +234,8 @@ bool PassRemoveScalars::run(
// Check if we have unsupported statements. If we do, warn the user and skip the pass execution.
for(const auto& stmt : iterateIIROverStmt(*stencilInstantiation->getIIR())) {
if(isStatementUnsupported(stmt, stencilInstantiation->getMetaData())) {
DAWN_LOG(INFO) << "Unsupported statement at line " << stmt->getSourceLocation()
<< ". Skipping removal of scalar variables.";
DAWN_DIAG(INFO, stencilInstantiation->getMetaData().getFileName(), stmt->getSourceLocation())
<< "Unsupported statement. Skipping removal of scalar variables...";
return true;
}
}
Expand All @@ -245,11 +245,9 @@ bool PassRemoveScalars::run(
auto removedScalars = removeScalarsFromDoMethod(*doMethod, stencilInstantiation->getMetaData());
if(context_.getOptions().ReportPassRemoveScalars) {
for(const auto& varName : removedScalars) {
std::cout << "PASS: " << getName() << ": " << stencilInstantiation->getName()
<< ": DoMethod: " << doMethod->getID() << " removed variable: " << varName
<< std::endl;
DAWN_LOG(INFO) << stencilInstantiation->getName() << ": DoMethod: " << doMethod->getID()
<< " removed variable: " << varName;
}
std::cout.flush();
}
// Recompute extents of fields
doMethod->update(iir::NodeUpdateType::level);
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/PassSetBoundaryCondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include <iostream>
#include <set>
#include <unordered_map>
Expand Down
10 changes: 5 additions & 5 deletions dawn/src/dawn/Optimizer/PassSetStageLocationType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
#include "dawn/IIR/Stage.h"
#include "dawn/IIR/StencilInstantiation.h"
#include "dawn/IIR/StencilMetaInformation.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Unreachable.h"
#include "dawn/Support/Exception.h"
#include "dawn/Support/Logger.h"
#include <deque>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>

namespace dawn {
namespace {
Expand Down Expand Up @@ -83,9 +84,8 @@ ast::LocationType deduceLocationType(const std::shared_ptr<iir::Stmt>& stmt,
dawn_unreachable("unsupported loop descriptor!\n");
}
}
DAWN_LOG(ERROR) << "Couldn't deduce location type for statement at line "
<< stmt->getSourceLocation() << ".";
dawn_unreachable("Couldn't deduce location type.");
throw SemanticError(std::string("Couldn't deduce location type for statement at line ") +
static_cast<std::string>(stmt->getSourceLocation()) + ".");
}

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion dawn/src/dawn/Optimizer/PassStageSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "dawn/Optimizer/OptimizerContext.h"
#include "dawn/Optimizer/ReadBeforeWriteConflict.h"
#include "dawn/Support/Format.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include <deque>
#include <iostream>
#include <iterator>
Expand Down
7 changes: 4 additions & 3 deletions dawn/src/dawn/Serialization/SIRSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#include "dawn/SIR/SIR/statements.pb.h"
#include "dawn/Serialization/ASTSerializer.h"
#include "dawn/Serialization/SIRSerializer.h"
#include "dawn/Support/Exception.h"
#include "dawn/Support/Format.h"
#include "dawn/Support/Logging.h"
#include "dawn/Support/Logger.h"
#include "dawn/Support/Unreachable.h"
#include <fstream>
#include <google/protobuf/util/json_util.h>
Expand Down Expand Up @@ -52,10 +53,10 @@ class ProtobufLogger : public NonCopyable {
DAWN_LOG(WARNING) << "Protobuf: " << message;
break;
case google::protobuf::LOGLEVEL_ERROR:
DAWN_LOG(ERROR) << "Protobuf: " << message;
throw SyntacticError(std::string("[ERROR] Protobuf error: ") + message);
break;
case google::protobuf::LOGLEVEL_FATAL:
DAWN_LOG(FATAL) << "Protobuf: " << message;
throw SyntacticError(std::string("[FATAL] Protobuf error occurred: ") + message);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions dawn/src/dawn/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ add_library(DawnSupport
IndexRange.h
Iterator.h
Json.h
Logging.cpp
Logging.h
Logger.cpp
Logger.h
MathExtras.h
NonCopyable.h
Printing.h
Expand Down
157 changes: 157 additions & 0 deletions dawn/src/dawn/Support/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//===--------------------------------------------------------------------------------*- C++ -*-===//
// _
// | |
// __| | __ ___ ___ ___
// / _` |/ _` \ \ /\ / / '_ |
// | (_| | (_| |\ V V /| | | |
// \__,_|\__,_| \_/\_/ |_| |_| - Compiler Toolchain
//
//
// This file is distributed under the MIT License (MIT).
// See LICENSE.txt for details.
//
//===------------------------------------------------------------------------------------------===//

#include "dawn/Support/Logger.h"
#include "dawn/Support/Assert.h"
#include "dawn/Support/FileSystem.h"
#include "dawn/Support/Format.h"
#include <chrono>
#include <iostream>

namespace dawn {

Logger::MessageFormatter makeMessageFormatter(const std::string type) {
return [type](const std::string& msg, const std::string& file, int line) {
std::stringstream ss;
ss << "[" << file << ":" << line << "] ";
if(type != "")
ss << type << ": ";
ss << msg;
return ss.str();
};
}

Logger::DiagnosticFormatter makeDiagnosticFormatter(const std::string type) {
return [type](const std::string& msg, const std::string& file, int line,
const std::string& source, SourceLocation loc) {
std::stringstream ss;
ss << "[" << file << ":" << line << "]";
if(source != "")
ss << " " << source;
if(loc.Line >= 0) {
ss << ":" << loc.Line;
}
if(loc.Column >= 0) {
ss << ":" << loc.Column;
}
if(type != "")
ss << ": " << type;
ss << ": " << msg;
return ss.str();
};
}

MessageProxy::MessageProxy(const MessageProxy& other)
: logger_(other.logger_), ss_(other.ss_.str()), file_(other.file_), line_(other.line_) {}

MessageProxy::MessageProxy(Logger& logger, const std::string& file, int line)
: logger_(logger), file_(file), line_(line) {}

MessageProxy::~MessageProxy() { logger_.enqueue(ss_.str(), file_, line_); }

DiagnosticProxy::DiagnosticProxy(const DiagnosticProxy& other)
: logger_(other.logger_), ss_(other.ss_.str()), file_(other.file_), line_(other.line_),
source_(other.source_), loc_(other.loc_) {}

DiagnosticProxy::DiagnosticProxy(Logger& logger, const std::string& file, int line,
const std::string& source, SourceLocation loc)
: logger_(logger), file_(file), line_(line), source_(source), loc_(loc) {}

DiagnosticProxy::~DiagnosticProxy() { logger_.enqueue(ss_.str(), file_, line_, source_, loc_); }

Logger::Logger(MessageFormatter msgFmt, DiagnosticFormatter diagFmt, std::ostream& os, bool show)
: msgFmt_(msgFmt), diagFmt_(diagFmt), os_(&os), data_(), show_(show) {}

MessageProxy Logger::operator()(const std::string& file, int line) {
return MessageProxy(*this, file, line);
}

DiagnosticProxy Logger::operator()(const std::string& file, int line, const std::string& source,
SourceLocation loc) {
return DiagnosticProxy(*this, file, line, source, loc);
}

void Logger::doEnqueue(const std::string& message) {
data_.push_back(message);
if(show_) {
*os_ << data_.back();
if(data_.back().back() != '\n')
*os_ << '\n';
}
}

void Logger::enqueue(std::string msg, const std::string& file, int line) {
doEnqueue(msgFmt_(msg, file, line));
}

void Logger::enqueue(std::string msg, const std::string& file, int line, const std::string& source,
SourceLocation loc) {
doEnqueue(diagFmt_(msg, file, line, source, loc));
}

std::ostream& Logger::stream() const { return *os_; }
void Logger::stream(std::ostream& os) { os_ = &os; }

Logger::MessageFormatter Logger::messageFormatter() const { return msgFmt_; }
void Logger::messageFormatter(const MessageFormatter& msgFmt) { msgFmt_ = msgFmt; }

Logger::DiagnosticFormatter Logger::diagnosticFormatter() const { return diagFmt_; }
void Logger::diagnosticFormatter(const DiagnosticFormatter& diagFmt) { diagFmt_ = diagFmt; }

void Logger::clear() { data_.clear(); }

void Logger::show() { show_ = true; }
void Logger::hide() { show_ = false; }

// Expose container of messages
Logger::iterator Logger::begin() { return std::begin(data_); }
Logger::iterator Logger::end() { return std::end(data_); }
Logger::const_iterator Logger::begin() const { return std::begin(data_); }
Logger::const_iterator Logger::end() const { return std::end(data_); }
Logger::Container::size_type Logger::size() const { return std::size(data_); }

namespace log {

Logger info(makeMessageFormatter("INFO"), makeDiagnosticFormatter("INFO"), std::cout, false);
Logger warn(makeMessageFormatter("WARNING"), makeDiagnosticFormatter("WARNING"), std::cout, true);
Logger error(makeMessageFormatter("ERROR"), makeDiagnosticFormatter("ERROR"), std::cerr, true);

void setVerbosity(Level level) {
switch(level) {
case Level::All:
info.show();
warn.show();
error.show();
break;
case Level::Warnings:
info.hide();
warn.show();
error.show();
break;
case Level::Errors:
info.hide();
warn.hide();
error.show();
break;
case Level::None:
info.hide();
warn.hide();
error.hide();
break;
}
}

} // namespace log

} // namespace dawn
Loading

0 comments on commit e06dcfd

Please sign in to comment.