OpenCPN Partial API docs
Loading...
Searching...
No Matches
zeroconf-util.hpp
1#ifndef ZEROCONF_UTIL_HPP
2#define ZEROCONF_UTIL_HPP
3
5// zeroconf-util.hpp
6
7// (C) Copyright 2016 Yuri Yakovlev <yvzmail@gmail.com>
8// Use, modification and distribution is subject to the GNU General Public
9// License
10
11#include <string>
12#include <istream>
13#include <streambuf>
14
15#if _MSC_VER == 1700
16#define thread_local __declspec(thread)
17#endif
18
19namespace Zeroconf {
20namespace Detail {
21namespace stdext {
22class membuf : public std::streambuf {
23public:
24 membuf(const unsigned char* base, int size) {
25 auto p = reinterpret_cast<char*>(const_cast<unsigned char*>(base));
26 setg(p, p, p + size);
27 }
28
29protected:
30 virtual pos_type seekoff(off_type offset, std::ios_base::seekdir,
31 std::ios_base::openmode mode) override {
32 if (offset == 0 && mode == std::ios_base::in)
33 return pos_type(gptr() - eback());
34
35 return pos_type(-1);
36 }
37};
38} // namespace stdext
39
40namespace Log {
41enum class LogLevel { Error, Warning };
42
43typedef void (*LogCallback)(LogLevel, const std::string&);
44
45static thread_local LogCallback g_logcb = nullptr;
46
47inline void Error(const std::string& message) {
48 if (g_logcb != nullptr) g_logcb(LogLevel::Error, message);
49}
50
51inline void Warning(const std::string& message) {
52 if (g_logcb != nullptr) g_logcb(LogLevel::Warning, message);
53}
54
55inline void SetLogCallback(LogCallback logcb) { g_logcb = logcb; }
56} // namespace Log
57} // namespace Detail
58} // namespace Zeroconf
59
60#endif // ZEROCONF_UTIL_HPP