Extract logger into own header

This commit is contained in:
Jonathan Müller 2017-06-28 18:20:24 +02:00
commit 3c9d53ae0c
4 changed files with 75 additions and 61 deletions

View file

@ -0,0 +1,71 @@
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_DIAGNOSTIC_LOGGER_HPP_INCLUDED
#define CPPAST_DIAGNOSTIC_LOGGER_HPP_INCLUDED
#include <type_safe/reference.hpp>
#include <cppast/diagnostic.hpp>
namespace cppast
{
/// Base class for a [cppast::diagnostic]() logger.
///
/// Its task is controlling how diagnostic are being displayed.
class diagnostic_logger
{
public:
/// \effects Creates it either as verbose or not.
explicit diagnostic_logger(bool is_verbose = false) noexcept : verbose_(is_verbose)
{
}
diagnostic_logger(const diagnostic_logger&) = delete;
diagnostic_logger& operator=(const diagnostic_logger&) = delete;
virtual ~diagnostic_logger() noexcept = default;
/// \effects Logs the diagnostic by invoking the `do_log()` member function.
/// \returns Whether or not the diagnostic was logged.
/// \notes `source` points to a string literal that gives additional context to what generates the message.
bool log(const char* source, const diagnostic& d) const;
/// \effects Sets whether or not the logger prints debugging diagnostics.
void set_verbose(bool value) noexcept
{
verbose_ = value;
}
/// \returns Whether or not the logger prints debugging diagnostics.
bool is_verbose() const noexcept
{
return verbose_;
}
private:
virtual bool do_log(const char* source, const diagnostic& d) const = 0;
bool verbose_;
};
/// \returns The default logger object.
type_safe::object_ref<const diagnostic_logger> default_logger() noexcept;
/// \returns The default verbose logger object.
type_safe::object_ref<const diagnostic_logger> default_verbose_logger() noexcept;
/// A [cppast::diagnostic_logger]() that logs to `stderr`.
///
/// It prints all diagnostics in an implementation-defined format.
class stderr_diagnostic_logger final : public diagnostic_logger
{
public:
using diagnostic_logger::diagnostic_logger;
private:
bool do_log(const char* source, const diagnostic& d) const override;
};
} // namespace cppast
#endif // CPPAST_DIAGNOSTIC_LOGGER_HPP_INCLUDED

View file

@ -10,67 +10,11 @@
#include <cppast/compile_config.hpp>
#include <cppast/cpp_file.hpp>
#include <cppast/cpp_preprocessor.hpp>
#include <cppast/diagnostic_logger.hpp>
namespace cppast
{
class cpp_entity_index;
struct diagnostic;
/// Base class for a [cppast::diagnostic]() logger.
///
/// Its task is controlling how diagnostic are being displayed.
class diagnostic_logger
{
public:
/// \effects Creates it either as verbose or not.
explicit diagnostic_logger(bool is_verbose = false) noexcept : verbose_(is_verbose)
{
}
diagnostic_logger(const diagnostic_logger&) = delete;
diagnostic_logger& operator=(const diagnostic_logger&) = delete;
virtual ~diagnostic_logger() noexcept = default;
/// \effects Logs the diagnostic by invoking the `do_log()` member function.
/// \returns Whether or not the diagnostic was logged.
/// \notes `source` points to a string literal that gives additional context to what generates the message.
bool log(const char* source, const diagnostic& d) const;
/// \effects Sets whether or not the logger prints debugging diagnostics.
void set_verbose(bool value) noexcept
{
verbose_ = value;
}
/// \returns Whether or not the logger prints debugging diagnostics.
bool is_verbose() const noexcept
{
return verbose_;
}
private:
virtual bool do_log(const char* source, const diagnostic& d) const = 0;
bool verbose_;
};
/// \returns The default logger object.
type_safe::object_ref<const diagnostic_logger> default_logger() noexcept;
/// \returns The default verbose logger object.
type_safe::object_ref<const diagnostic_logger> default_verbose_logger() noexcept;
/// A [cppast::diagnostic_logger]() that logs to `stderr`.
///
/// It prints all diagnostics in an implementation-defined format.
class stderr_diagnostic_logger final : public diagnostic_logger
{
public:
using diagnostic_logger::diagnostic_logger;
private:
bool do_log(const char* source, const diagnostic& d) const override;
};
/// Base class for a parser.
///

View file

@ -42,6 +42,7 @@ set(header
../include/cppast/cpp_variable_base.hpp
../include/cppast/cpp_variable_template.hpp
../include/cppast/diagnostic.hpp
../include/cppast/diagnostic_logger.hpp
../include/cppast/libclang_parser.hpp
../include/cppast/parser.hpp
../include/cppast/visitor.hpp)
@ -71,7 +72,7 @@ set(source
cpp_type_alias.cpp
cpp_variable.cpp
cpp_variable_template.cpp
parser.cpp
diagnostic_logger.cpp
visitor.cpp)
set(libclang_source
libclang/class_parser.cpp

View file

@ -2,13 +2,11 @@
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <cppast/parser.hpp>
#include <cppast/diagnostic_logger.hpp>
#include <cstdio>
#include <mutex>
#include <cppast/diagnostic.hpp>
using namespace cppast;
bool diagnostic_logger::log(const char* source, const diagnostic& d) const