diff options
author | gabime <gmelman1@gmail.com> | 2019-05-22 12:19:55 +0300 |
---|---|---|
committer | gabime <gmelman1@gmail.com> | 2019-05-22 12:19:55 +0300 |
commit | 9205c9d031931012b1699007743caf7c7fd51ff7 (patch) | |
tree | dc2137235a85aac31a583b7123ef406f261be19e /example | |
parent | 92387b152718fc3341511913f2db82e52f9d023e (diff) | |
parent | 1ef80d63304c19015164c1e7fafd99385eac4deb (diff) |
Merge branch 'static-lib' of https://github.com/gabime/spdlog into static-lib
Diffstat (limited to 'example')
-rw-r--r-- | example/CMakeLists.txt | 60 | ||||
-rw-r--r-- | example/jni/Android.mk | 15 | ||||
-rw-r--r-- | example/jni/Application.mk | 2 | ||||
-rw-r--r-- | example/jni/example.cpp | 157 | ||||
-rw-r--r-- | example/logs/.gitignore | 1 | ||||
-rw-r--r-- | example/multisink.cpp | 47 | ||||
-rw-r--r-- | example/utils.h | 34 |
7 files changed, 21 insertions, 295 deletions
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f0a917f2..2fbbf1d2 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,49 +1,31 @@ -# *************************************************************************/ -# * Copyright (c) 2015 Ruslan Baratov. */ -# * */ -# * Permission is hereby granted, free of charge, to any person obtaining */ -# * a copy of this software and associated documentation files (the */ -# * "Software"), to deal in the Software without restriction, including */ -# * without limitation the rights to use, copy, modify, merge, publish, */ -# * distribute, sublicense, and/or sell copies of the Software, and to */ -# * permit persons to whom the Software is furnished to do so, subject to */ -# * the following conditions: */ -# * */ -# * The above copyright notice and this permission notice shall be */ -# * included in all copies or substantial portions of the Software. */ -# * */ -# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -# * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -# * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -# * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -# * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -# * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -# * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -# *************************************************************************/ +# Copyright(c) 2019 spdlog authors +# Distributed under the MIT License (http://opensource.org/licenses/MIT) cmake_minimum_required(VERSION 3.1) project(SpdlogExamples CXX) -if(NOT TARGET spdlog) - # Stand-alone build - find_package(spdlog CONFIG REQUIRED) -endif() - -find_package(Threads REQUIRED) - -add_executable(example example.cpp) -if(CMAKE_SYSTEM_NAME STREQUAL "Android") - find_library(log-lib log) - target_link_libraries(example spdlog::static Threads::Threads log) +if(TARGET spdlog) + # If we're running this example as part of the primary spdlog applciation + # then add an alias. This allows us to use the same "spdlog::spdlog" + # below that a user would use (with the namespace) + add_library(spdlog::spdlog ALIAS spdlog) + add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only) else() - target_link_libraries(example spdlog::static Threads::Threads) + # Stand-alone build + find_package(spdlog REQUIRED) endif() +#--------------------------------------------------------------------------------------- +# Example of using pre-compiled library +#--------------------------------------------------------------------------------------- +add_executable(example example.cpp) +target_link_libraries(example spdlog::spdlog) -add_executable(multisink multisink.cpp) -target_link_libraries(multisink spdlog::static Threads::Threads) +#--------------------------------------------------------------------------------------- +# Example of using header-only library +#--------------------------------------------------------------------------------------- +add_executable(example_header_only example.cpp) +target_link_libraries(example_header_only spdlog::spdlog_header_only) +# Create logs directory file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") - -enable_testing() -add_test(NAME example COMMAND example) diff --git a/example/jni/Android.mk b/example/jni/Android.mk deleted file mode 100644 index 7accbad3..00000000 --- a/example/jni/Android.mk +++ /dev/null @@ -1,15 +0,0 @@ -# Setup a project -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_MODULE := example -LOCAL_SRC_FILES := example.cpp -LOCAL_CPPFLAGS += -Wall -Wshadow -Wextra -pedantic -std=c++11 -fPIE -pie -LOCAL_LDFLAGS += -fPIE -pie - -# Add exception support and set path for spdlog's headers -LOCAL_CPPFLAGS += -fexceptions -I../include -# Use android's log library -LOCAL_LDFLAGS += -llog - -include $(BUILD_EXECUTABLE) diff --git a/example/jni/Application.mk b/example/jni/Application.mk deleted file mode 100644 index dccd2a5a..00000000 --- a/example/jni/Application.mk +++ /dev/null @@ -1,2 +0,0 @@ -# Exceptions are used in spdlog. Link to an exception-ready C++ runtime. -APP_STL = gnustl_static diff --git a/example/jni/example.cpp b/example/jni/example.cpp deleted file mode 100644 index 48c4b19e..00000000 --- a/example/jni/example.cpp +++ /dev/null @@ -1,157 +0,0 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// -// -// spdlog usage example -// -// - -#define SPDLOG_TRACE_ON -#define SPDLOG_DEBUG_ON - -#include "spdlog/spdlog.h" - -#include <iostream> -#include <memory> - -void async_example(); -void syslog_example(); -void android_example(); -void user_defined_example(); -void err_handler_example(); - -namespace spd = spdlog; -int main(int, char *[]) -{ - try - { - // Console logger with color - auto console = spd::stdout_color_mt("console"); - console->info("Welcome to spdlog!"); - console->error("Some error message with arg{}..", 1); - - // Formatting examples - console->warn("Easy padding in numbers like {:08d}", 12); - console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); - console->info("Support for floats {:03.2f}", 1.23456); - console->info("Positional args are {1} {0}..", "too", "supported"); - console->info("{:<30}", "left aligned"); - - spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); - - // Create basic file logger (not rotated) - auto my_logger = spd::basic_logger_mt("basic_logger", "logs/basic-log.txt"); - my_logger->info("Some log message"); - - // Create a file rotating logger with 5mb size max and 3 rotated files - auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); - for (int i = 0; i < 10; ++i) - rotating_logger->info("{} * {} equals {:>10}", i, i, i * i); - - // Create a daily logger - a new file is created every day on 2:30am - auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30); - // trigger flush if the log severity is error or higher - daily_logger->flush_on(spd::level::err); - daily_logger->info(123.44); - - // Customize msg format for all messages - spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***"); - rotating_logger->info("This is another message with custom format"); - - // Runtime log levels - spd::set_level(spd::level::info); // Set global log level to info - console->debug("This message should not be displayed!"); - console->set_level(spd::level::debug); // Set specific logger's log level - console->debug("This message should be displayed.."); - - // Compile time log levels - // define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON - SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); - SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23); - - // Asynchronous logging is very fast.. - // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. - async_example(); - - // syslog example. linux/osx only - syslog_example(); - - // android example. compile with NDK - android_example(); - - // Log user-defined types example - user_defined_example(); - - // Change default log error handler - err_handler_example(); - - // Apply a function on all registered loggers - spd::apply_all([&](std::shared_ptr<spdlog::logger> l) { l->info("End of example."); }); - - // Release and close all loggers - spdlog::drop_all(); - } - // Exceptions will only be thrown upon failed logger or sink construction (not during logging) - catch (const spd::spdlog_ex &ex) - { - std::cout << "Log init failed: " << ex.what() << std::endl; - return 1; - } -} - -void async_example() -{ - size_t q_size = 4096; // queue size must be power of 2 - spdlog::set_async_mode(q_size); - auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); - for (int i = 0; i < 100; ++i) - async_file->info("Async message #{}", i); -} - -// syslog example (linux/osx/freebsd) -void syslog_example() -{ -#ifdef SPDLOG_ENABLE_SYSLOG - std::string ident = "spdlog-example"; - auto syslog_logger = spd::syslog_logger("syslog", ident, LOG_PID); - syslog_logger->warn("This is warning that will end up in syslog."); -#endif -} - -// Android example -void android_example() -{ -#if defined(__ANDROID__) - std::string tag = "spdlog-android"; - auto android_logger = spd::android_logger("android", tag); - android_logger->critical("Use \"adb shell logcat\" to view this message."); -#endif -} - -// user defined types logging by implementing operator<< -struct my_type -{ - int i; - template<typename OStream> - friend OStream &operator<<(OStream &os, const my_type &c) - { - return os << "[my_type i=" << c.i << "]"; - } -}; - -#include "spdlog/fmt/ostr.h" // must be included -void user_defined_example() -{ - spd::get("console")->info("user defined type: {}", my_type{14}); -} - -// -// custom error handler -// -void err_handler_example() -{ - // can be set globaly or per logger(logger->set_error_handler(..)) - spdlog::set_error_handler([](const std::string &msg) { std::cerr << "my err handler: " << msg << std::endl; }); - spd::get("console")->info("some invalid message to trigger an error {}{}{}{}", 3); -} diff --git a/example/logs/.gitignore b/example/logs/.gitignore deleted file mode 100644 index 20325135..00000000 --- a/example/logs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.txt
diff --git a/example/multisink.cpp b/example/multisink.cpp deleted file mode 100644 index fd79231c..00000000 --- a/example/multisink.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "spdlog/spdlog.h" -#include "spdlog/sinks/basic_file_sink.h" -#include "spdlog/sinks/stdout_sinks.h" - -#include <iostream> -#include <memory> - -int main(int, char *[]) -{ - bool enable_debug = true; - try - { - // This other example use a single logger with multiple sinks. - // This means that the same log_msg is forwarded to multiple sinks; - // Each sink can have it's own log level and a message will be logged. - std::vector<spdlog::sink_ptr> sinks; - sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>()); - sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_regular_file.txt")); - sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_debug_file.txt")); - - spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end()); - console_multisink.set_level(spdlog::level::warn); - - sinks[0]->set_level(spdlog::level::trace); // console. Allow everything. Default value - sinks[1]->set_level(spdlog::level::trace); // regular file. Allow everything. Default value - sinks[2]->set_level(spdlog::level::off); // regular file. Ignore everything. - - console_multisink.warn("warn: will print only on console and regular file"); - - if (enable_debug) - { - console_multisink.set_level(spdlog::level::debug); // level of the logger - sinks[1]->set_level(spdlog::level::debug); // regular file - sinks[2]->set_level(spdlog::level::debug); // debug file - } - console_multisink.debug("Debug: you should see this on console and both files"); - - // Release and close all loggers - spdlog::drop_all(); - } - // Exceptions will only be thrown upon failed logger or sink construction (not during logging) - catch (const spdlog::spdlog_ex &ex) - { - std::cout << "Log init failed: " << ex.what() << std::endl; - return 1; - } -} diff --git a/example/utils.h b/example/utils.h deleted file mode 100644 index 91610128..00000000 --- a/example/utils.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -#include <iomanip> -#include <locale> -#include <sstream> - -namespace utils { - -template<typename T> -inline std::string format(const T &value) -{ - static std::locale loc(""); - std::stringstream ss; - ss.imbue(loc); - ss << value; - return ss.str(); -} - -template<> -inline std::string format(const double &value) -{ - static std::locale loc(""); - std::stringstream ss; - ss.imbue(loc); - ss << std::fixed << std::setprecision(1) << value; - return ss.str(); -} - -} // namespace utils |