Skip to content

Commit f682b13

Browse files
committed
write default functions
1 parent 808b265 commit f682b13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+13301
-4
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# json-downloader
22

33
## What is it?
4-
- Downloader using curl library.
5-
- TODO
4+
- Downloader for Windows Console
5+
- It is written for personal purpose.
6+
- DONE:
67
- Ability to download files continuously
78
- Getting a download list from a json file
9+
- TODO:
10+
- Extract files from compressed files
811

912
## Requred Environment
1013
- Visual Studio 2017 x64
@@ -13,3 +16,5 @@
1316
## License
1417
- json-downloader is under GPL 3 license. [https://github.com/j2doll/json-downloader](https://github.com/j2doll/json-downloader)
1518
- curl is under MIT style license. [https://curl.haxx.se/docs/copyright.html](https://curl.haxx.se/docs/copyright.html)
19+
- [![JSON for Modern C++](https://raw.githubusercontent.com/nlohmann/json/master/doc/json.gif)](https://github.com/nlohmann/json/releases) is under MIT License.
20+
- spdlog is under MIT License [https://github.com/gabime/spdlog](https://github.com/gabime/spdlog)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"https://raw.githubusercontent.com/j2doll/json-downloader/master/README.md" : "README.md" ,
3+
"https://raw.githubusercontent.com/j2doll/json-downloader/master/LICENSE" : "LICENSE"
4+
}
236 Bytes
Binary file not shown.

json-downloader/json-downloader/json-downloader.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
</PropertyGroup>
7676
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7777
<LinkIncremental>true</LinkIncremental>
78-
<IncludePath>..\..\json\include;..\..\libcurl-vc14-AMD64-release-static-ipv6-sspi-winssl\include;$(IncludePath)</IncludePath>
78+
<IncludePath>..\..\json\include;..\..\spdlog\include;..\..\libcurl-vc14-AMD64-release-static-ipv6-sspi-winssl\include;$(IncludePath)</IncludePath>
7979
</PropertyGroup>
8080
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8181
<LinkIncremental>false</LinkIncremental>
8282
</PropertyGroup>
8383
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8484
<LinkIncremental>false</LinkIncremental>
85-
<IncludePath>..\..\json\include;..\..\libcurl-vc14-AMD64-release-static-ipv6-sspi-winssl\include;$(IncludePath)</IncludePath>
85+
<IncludePath>..\..\json\include;..\..\spdlog\include;..\..\libcurl-vc14-AMD64-release-static-ipv6-sspi-winssl\include;$(IncludePath)</IncludePath>
8686
</PropertyGroup>
8787
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
8888
<ClCompile>
192 Bytes
Binary file not shown.

spdlog/include/spdlog/async.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
//
3+
// Copyright(c) 2018 Gabi Melman.
4+
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
5+
//
6+
7+
#pragma once
8+
9+
//
10+
// Async logging using global thread pool
11+
// All loggers created here share same global thread pool.
12+
// Each log message is pushed to a queue along withe a shared pointer to the
13+
// logger.
14+
// If a logger deleted while having pending messages in the queue, it's actual
15+
// destruction will defer
16+
// until all its messages are processed by the thread pool.
17+
// This is because each message in the queue holds a shared_ptr to the
18+
// originating logger.
19+
20+
#include "spdlog/async_logger.h"
21+
#include "spdlog/details/registry.h"
22+
#include "spdlog/details/thread_pool.h"
23+
24+
#include <memory>
25+
#include <mutex>
26+
27+
namespace spdlog {
28+
29+
namespace details {
30+
static const size_t default_async_q_size = 8192;
31+
}
32+
33+
// async logger factory - creates async loggers backed with thread pool.
34+
// if a global thread pool doesn't already exist, create it with default queue
35+
// size of 8192 items and single thread.
36+
template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
37+
struct async_factory_impl
38+
{
39+
template<typename Sink, typename... SinkArgs>
40+
static std::shared_ptr<async_logger> create(const std::string &logger_name, SinkArgs &&... args)
41+
{
42+
auto &registry_inst = details::registry::instance();
43+
44+
// create global thread pool if not already exists..
45+
std::lock_guard<std::recursive_mutex> tp_lock(registry_inst.tp_mutex());
46+
auto tp = registry_inst.get_tp();
47+
if (tp == nullptr)
48+
{
49+
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
50+
registry_inst.set_tp(tp);
51+
}
52+
53+
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
54+
auto new_logger = std::make_shared<async_logger>(logger_name, std::move(sink), std::move(tp), OverflowPolicy);
55+
registry_inst.register_and_init(new_logger);
56+
return new_logger;
57+
}
58+
};
59+
60+
using async_factory = async_factory_impl<async_overflow_policy::block>;
61+
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
62+
63+
template<typename Sink, typename... SinkArgs>
64+
inline std::shared_ptr<spdlog::logger> create_async(const std::string &logger_name, SinkArgs &&... sink_args)
65+
{
66+
return async_factory::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
67+
}
68+
69+
template<typename Sink, typename... SinkArgs>
70+
inline std::shared_ptr<spdlog::logger> create_async_nb(const std::string &logger_name, SinkArgs &&... sink_args)
71+
{
72+
return async_factory_nonblock::create<Sink>(logger_name, std::forward<SinkArgs>(sink_args)...);
73+
}
74+
75+
// set global thread pool.
76+
inline void init_thread_pool(size_t q_size, size_t thread_count)
77+
{
78+
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count);
79+
details::registry::instance().set_tp(std::move(tp));
80+
}
81+
82+
// get the global thread pool.
83+
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
84+
{
85+
return details::registry::instance().get_tp();
86+
}
87+
} // namespace spdlog
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// Copyright(c) 2015 Gabi Melman.
3+
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4+
//
5+
6+
#pragma once
7+
8+
// Very fast asynchronous logger (millions of logs per second on an average
9+
// desktop)
10+
// Uses pre allocated lockfree queue for maximum throughput even under large
11+
// number of threads.
12+
// Creates a single back thread to pop messages from the queue and log them.
13+
//
14+
// Upon each log write the logger:
15+
// 1. Checks if its log level is enough to log the message
16+
// 2. Push a new copy of the message to a queue (or block the caller until
17+
// space is available in the queue)
18+
// 3. will throw spdlog_ex upon log exceptions
19+
// Upon destruction, logs all remaining messages in the queue before
20+
// destructing..
21+
22+
#include "spdlog/common.h"
23+
#include "spdlog/logger.h"
24+
25+
#include <chrono>
26+
#include <memory>
27+
#include <string>
28+
29+
namespace spdlog {
30+
31+
// Async overflow policy - block by default.
32+
enum class async_overflow_policy
33+
{
34+
block, // Block until message can be enqueued
35+
overrun_oldest // Discard oldest message in the queue if full when trying to
36+
// add new item.
37+
};
38+
39+
namespace details {
40+
class thread_pool;
41+
}
42+
43+
class async_logger final : public std::enable_shared_from_this<async_logger>, public logger
44+
{
45+
friend class details::thread_pool;
46+
47+
public:
48+
template<typename It>
49+
async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp,
50+
async_overflow_policy overflow_policy = async_overflow_policy::block);
51+
52+
async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp,
53+
async_overflow_policy overflow_policy = async_overflow_policy::block);
54+
55+
async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
56+
async_overflow_policy overflow_policy = async_overflow_policy::block);
57+
58+
std::shared_ptr<logger> clone(std::string new_name) override;
59+
60+
protected:
61+
void sink_it_(details::log_msg &msg) override;
62+
void flush_() override;
63+
64+
void backend_log_(const details::log_msg &incoming_log_msg);
65+
void backend_flush_();
66+
67+
private:
68+
std::weak_ptr<details::thread_pool> thread_pool_;
69+
async_overflow_policy overflow_policy_;
70+
};
71+
} // namespace spdlog
72+
73+
#include "details/async_logger_impl.h"

spdlog/include/spdlog/common.h

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
//
2+
// Copyright(c) 2015 Gabi Melman.
3+
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4+
//
5+
6+
#pragma once
7+
8+
#include "spdlog/tweakme.h"
9+
10+
#include <atomic>
11+
#include <chrono>
12+
#include <functional>
13+
#include <initializer_list>
14+
#include <memory>
15+
#include <stdexcept>
16+
#include <string>
17+
#include <type_traits>
18+
#include <unordered_map>
19+
20+
#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
21+
#include <codecvt>
22+
#include <locale>
23+
#endif
24+
25+
#include "spdlog/details/null_mutex.h"
26+
27+
// visual studio upto 2013 does not support noexcept nor constexpr
28+
#if defined(_MSC_VER) && (_MSC_VER < 1900)
29+
#define SPDLOG_NOEXCEPT throw()
30+
#define SPDLOG_CONSTEXPR
31+
#else
32+
#define SPDLOG_NOEXCEPT noexcept
33+
#define SPDLOG_CONSTEXPR constexpr
34+
#endif
35+
36+
#if defined(__GNUC__) || defined(__clang__)
37+
#define SPDLOG_DEPRECATED __attribute__((deprecated))
38+
#elif defined(_MSC_VER)
39+
#define SPDLOG_DEPRECATED __declspec(deprecated)
40+
#else
41+
#define SPDLOG_DEPRECATED
42+
#endif
43+
44+
#include "spdlog/fmt/fmt.h"
45+
46+
namespace spdlog {
47+
48+
class formatter;
49+
50+
namespace sinks {
51+
class sink;
52+
}
53+
54+
using log_clock = std::chrono::system_clock;
55+
using sink_ptr = std::shared_ptr<sinks::sink>;
56+
using sinks_init_list = std::initializer_list<sink_ptr>;
57+
using log_err_handler = std::function<void(const std::string &err_msg)>;
58+
59+
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
60+
using level_t = details::null_atomic_int;
61+
#else
62+
using level_t = std::atomic<int>;
63+
#endif
64+
65+
// Log level enum
66+
namespace level {
67+
enum level_enum
68+
{
69+
trace = 0,
70+
debug = 1,
71+
info = 2,
72+
warn = 3,
73+
err = 4,
74+
critical = 5,
75+
off = 6
76+
};
77+
78+
#if !defined(SPDLOG_LEVEL_NAMES)
79+
#define SPDLOG_LEVEL_NAMES \
80+
{ \
81+
"trace", "debug", "info", "warning", "error", "critical", "off" \
82+
}
83+
#endif
84+
static const char *level_names[] SPDLOG_LEVEL_NAMES;
85+
86+
static const char *short_level_names[]{"T", "D", "I", "W", "E", "C", "O"};
87+
88+
inline const char *to_c_str(spdlog::level::level_enum l)
89+
{
90+
return level_names[l];
91+
}
92+
93+
inline const char *to_short_c_str(spdlog::level::level_enum l)
94+
{
95+
return short_level_names[l];
96+
}
97+
98+
inline spdlog::level::level_enum from_str(const std::string &name)
99+
{
100+
static std::unordered_map<std::string, level_enum> name_to_level = // map string->level
101+
{{level_names[0], level::trace}, // trace
102+
{level_names[1], level::debug}, // debug
103+
{level_names[2], level::info}, // info
104+
{level_names[3], level::warn}, // warn
105+
{level_names[4], level::err}, // err
106+
{level_names[5], level::critical}, // critical
107+
{level_names[6], level::off}}; // off
108+
109+
auto lvl_it = name_to_level.find(name);
110+
return lvl_it != name_to_level.end() ? lvl_it->second : level::off;
111+
}
112+
113+
using level_hasher = std::hash<int>;
114+
} // namespace level
115+
116+
//
117+
// Pattern time - specific time getting to use for pattern_formatter.
118+
// local time by default
119+
//
120+
enum class pattern_time_type
121+
{
122+
local, // log localtime
123+
utc // log utc
124+
};
125+
126+
//
127+
// Log exception
128+
//
129+
class spdlog_ex : public std::exception
130+
{
131+
public:
132+
explicit spdlog_ex(std::string msg)
133+
: msg_(std::move(msg))
134+
{
135+
}
136+
137+
spdlog_ex(const std::string &msg, int last_errno)
138+
{
139+
fmt::memory_buffer outbuf;
140+
fmt::format_system_error(outbuf, last_errno, msg);
141+
msg_ = fmt::to_string(outbuf);
142+
}
143+
144+
const char *what() const SPDLOG_NOEXCEPT override
145+
{
146+
return msg_.c_str();
147+
}
148+
149+
private:
150+
std::string msg_;
151+
};
152+
153+
//
154+
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
155+
//
156+
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
157+
using filename_t = std::wstring;
158+
#else
159+
using filename_t = std::string;
160+
#endif
161+
162+
#define SPDLOG_CATCH_AND_HANDLE \
163+
catch (const std::exception &ex) \
164+
{ \
165+
err_handler_(ex.what()); \
166+
} \
167+
catch (...) \
168+
{ \
169+
err_handler_("Unknown exeption in logger"); \
170+
}
171+
172+
namespace details {
173+
// make_unique support for pre c++14
174+
175+
#if __cplusplus >= 201402L // C++14 and beyond
176+
using std::make_unique;
177+
#else
178+
template<typename T, typename... Args>
179+
std::unique_ptr<T> make_unique(Args &&... args)
180+
{
181+
static_assert(!std::is_array<T>::value, "arrays not supported");
182+
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
183+
}
184+
#endif
185+
} // namespace details
186+
} // namespace spdlog

0 commit comments

Comments
 (0)