Skip to content

Commit

Permalink
Inital code for spdlog console logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Love committed Oct 8, 2018
1 parent 0774f19 commit 28a84d0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.1)

project(cpp-logging-playground)

#---------------------------------------------------------------------------------------
# compiler config
#---------------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options("-Wall")
add_compile_options("-Wextra")
add_compile_options("-Wconversion")
add_compile_options("-pedantic")
add_compile_options("-Wfatal-errors")

endif()

# Boost::log required Boost version >= 1.54.0
find_package(Boost 1.54.0 REQUIRED COMPONENTS log)

# fmt library dependency
find_package(fmt CONFIG REQUIRED)

# spdlog library dependency)
find_package(spdlog CONFIG REQUIRED)

add_subdirectory(spdlog_console)
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cpp-logging-playground

This repo includes sample code using various C++ logging frameworks.

Requirements:
- Filtering by various logging levels (e.g. info, warn, err, debug, fatal)
- Support logging to stdout/stderr (for use in containers)
- Support logging to file with rotation support (for use outside of containers)
- Thread-safe
- Type-safe
- Customizable logging formats
- Extensible for user-defined types

## spdlog/fmt
Dependencies:
* https://github.com/gabime/spdlog (header-only)
* https://github.com/fmtlib/fmt

## boost::log
Dependencies: boost
14 changes: 14 additions & 0 deletions spdlog_console/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.1)

project(spdlog_console)

include_directories(/usr/local/include)

set (spdlog_console_SRC
main.cpp
)

add_executable( spdlog_console ${spdlog_console_SRC})

target_link_libraries( spdlog_console spdlog::spdlog pthread )

59 changes: 59 additions & 0 deletions spdlog_console/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_sinks.h>
#include <thread>
#include <chrono>

class AppClass {
public:
AppClass(const std::string &name): m_thread([this]() { this->thread(); }), m_name(name),
m_logger(spdlog::stdout_logger_mt(name))
{}

~AppClass() {
{
std::lock_guard<std::mutex> l(m_mutex);
m_stop = true;
}
m_cond.notify_one();
m_thread.join();
}

void thread() {
m_logger->trace("Thread {} starting",m_name);
while (this->wait_for(std::chrono::seconds(5))) {
m_logger->info("Thread {} doing something",m_name);
}
m_logger->trace("Thread {} exiting",m_name);
}

template<class Duration>
bool wait_for(Duration duration) {
std::unique_lock<std::mutex> l(m_mutex);
return !m_cond.wait_for(l, duration, [this]() { return m_stop; });
}

private:
std::condition_variable m_cond;
std::mutex m_mutex;
std::thread m_thread;
bool m_stop = false;
std::string m_name;
std::shared_ptr<spdlog::logger> m_logger;
};


int main(int argc, char**argv) {
auto logger = spdlog::stdout_logger_mt("main");
logger->info("spdlog_console logging");

std::vector<std::unique_ptr<AppClass>> threads;
for (int i =0; i < 10; i++) {
threads.emplace_back(std::make_unique<AppClass>(std::string("Worker thread ") + std::to_string(i)));
}

logger->info("Main thread sleeping for 2 minutes");
std::this_thread::sleep_for(std::chrono::minutes(2));

logger->info("Exiting");

}

0 comments on commit 28a84d0

Please sign in to comment.