WIP
This commit is contained in:
parent
0e95d60bcd
commit
8e657df097
|
|
@ -1,3 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
add_subdirectory(example_boost)
|
add_subdirectory(example_boost)
|
||||||
|
add_subdirectory(hello_world)
|
||||||
|
add_subdirectory(hello_world_awesome)
|
||||||
|
add_subdirectory(awesome_log)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
project(awesome_log)
|
||||||
|
|
||||||
|
add_library(
|
||||||
|
${PROJECT_NAME}
|
||||||
|
./src/console_writer.cpp
|
||||||
|
./src/log.cpp
|
||||||
|
./src/null_writer.cpp
|
||||||
|
./src/scoped_log.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set_property(
|
||||||
|
TARGET ${PROJECT_NAME}
|
||||||
|
PROPERTY CXX_STANDARD 20
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(
|
||||||
|
${PROJECT_NAME}
|
||||||
|
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "writer_interface.hpp"
|
||||||
|
|
||||||
|
namespace awesome_log
|
||||||
|
{
|
||||||
|
class console_writer : public writer_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~console_writer() override;
|
||||||
|
void log(std::string_view msg) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace awesome_log
|
||||||
|
{
|
||||||
|
class writer_interface;
|
||||||
|
|
||||||
|
writer_interface& get_writer();
|
||||||
|
void set_writer(const std::shared_ptr<writer_interface>& writer);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "writer_interface.hpp"
|
||||||
|
|
||||||
|
namespace awesome_log
|
||||||
|
{
|
||||||
|
class null_writer : public writer_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~null_writer() override;
|
||||||
|
void log(std::string_view msg) override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace awesome_log
|
||||||
|
{
|
||||||
|
class scoped_log
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
scoped_log(std::string_view msg);
|
||||||
|
~scoped_log();
|
||||||
|
|
||||||
|
scoped_log() = delete;
|
||||||
|
|
||||||
|
// No copying or moving allowed.
|
||||||
|
scoped_log(const scoped_log&) = delete;
|
||||||
|
scoped_log(scoped_log&&) = delete;
|
||||||
|
scoped_log& operator=(const scoped_log&) = delete;
|
||||||
|
scoped_log& operator=(scoped_log&&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_msg;
|
||||||
|
int m_uncaught_exceptions;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace awesome_log
|
||||||
|
{
|
||||||
|
class writer_interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~writer_interface() = default;
|
||||||
|
|
||||||
|
virtual void log(std::string_view msg) = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
#include <awesome_log/console_writer.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace awesome_log;
|
||||||
|
|
||||||
|
console_writer::~console_writer() = default;
|
||||||
|
|
||||||
|
void console_writer::log(std::string_view msg)
|
||||||
|
{
|
||||||
|
std::cout << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include <awesome_log/log.hpp>
|
||||||
|
#include <awesome_log/null_writer.hpp>
|
||||||
|
|
||||||
|
using awesome_log::writer_interface;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
std::shared_ptr<writer_interface> g_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
writer_interface& awesome_log::get_writer()
|
||||||
|
{
|
||||||
|
if(!g_instance)
|
||||||
|
{
|
||||||
|
g_instance = std::make_shared<null_writer>();
|
||||||
|
}
|
||||||
|
return *g_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void awesome_log::set_writer(const std::shared_ptr<writer_interface>& writer)
|
||||||
|
{
|
||||||
|
g_instance = writer;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
#include <awesome_log/null_writer.hpp>
|
||||||
|
|
||||||
|
using namespace awesome_log;
|
||||||
|
|
||||||
|
null_writer::~null_writer() = default;
|
||||||
|
|
||||||
|
void null_writer::log(std::string_view)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include <awesome_log/scoped_log.hpp>
|
||||||
|
#include <awesome_log/log.hpp>
|
||||||
|
#include <awesome_log/writer_interface.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
using namespace awesome_log;
|
||||||
|
|
||||||
|
scoped_log::scoped_log(std::string_view msg)
|
||||||
|
: m_msg(msg)
|
||||||
|
, m_uncaught_exceptions(std::uncaught_exceptions())
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "> " << m_msg;
|
||||||
|
|
||||||
|
get_writer().log(ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
scoped_log::~scoped_log()
|
||||||
|
{
|
||||||
|
bool is_stack_unwinding = (m_uncaught_exceptions != std::uncaught_exceptions());
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "< " << m_msg;
|
||||||
|
|
||||||
|
if (is_stack_unwinding)
|
||||||
|
{
|
||||||
|
ss << " (stack unwinding)";
|
||||||
|
}
|
||||||
|
|
||||||
|
get_writer().log(ss.str());
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
find_package(Boost 1.88.0 REQUIRED COMPONENTS serialization)
|
find_package(Boost 1.88.0 REQUIRED COMPONENTS serialization)
|
||||||
|
|
||||||
project(hello_world_boost)
|
project(example_boost)
|
||||||
|
|
||||||
add_executable(
|
add_executable(
|
||||||
${PROJECT_NAME}
|
${PROJECT_NAME}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
|
|
||||||
struct position
|
struct position
|
||||||
{
|
{
|
||||||
double x;
|
int x;
|
||||||
double y;
|
int y;
|
||||||
|
|
||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive & ar, const unsigned int /*version*/)
|
void serialize(Archive & ar, const unsigned int /*version*/)
|
||||||
|
|
@ -25,7 +25,7 @@ struct position
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
position myposition{.x=123.0, .y=456.0};
|
position myposition{.x=123, .y=456};
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
project(hello_world)
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
${PROJECT_NAME}
|
||||||
|
src/main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set_property(
|
||||||
|
TARGET ${PROJECT_NAME}
|
||||||
|
PROPERTY CXX_STANDARD 20
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
std::cout << "Hello world" << std::endl;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
|
||||||
|
project(hello_world_awesome)
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
${PROJECT_NAME}
|
||||||
|
src/main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
${PROJECT_NAME}
|
||||||
|
awesome_log
|
||||||
|
)
|
||||||
|
|
||||||
|
set_property(
|
||||||
|
TARGET ${PROJECT_NAME}
|
||||||
|
PROPERTY CXX_STANDARD 20
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include <awesome_log/log.hpp>
|
||||||
|
#include <awesome_log/scoped_log.hpp>
|
||||||
|
#include <awesome_log/console_writer.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
using awesome_log::scoped_log;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
awesome_log::set_writer(std::make_shared<awesome_log::console_writer>());
|
||||||
|
|
||||||
|
{
|
||||||
|
scoped_log scoped("main.cpp, printing text");
|
||||||
|
std::cout << "Hello world" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
scoped_log scoped("main.cpp, playing with exceptions");
|
||||||
|
throw std::runtime_error("Let's play!");
|
||||||
|
// ... unreachable code goes here.
|
||||||
|
}
|
||||||
|
catch(const std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Caught an exception, as expected in this example" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue