diff options
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/Algorithm.h | 26 | ||||
-rw-r--r-- | Swiften/Base/DateTime.cpp | 4 | ||||
-rw-r--r-- | Swiften/Base/Log.cpp | 48 | ||||
-rw-r--r-- | Swiften/Base/Log.h | 25 | ||||
-rw-r--r-- | Swiften/Base/Platform.h | 8 | ||||
-rw-r--r-- | Swiften/Base/URL.cpp | 14 | ||||
-rw-r--r-- | Swiften/Base/URL.h | 10 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/LogTest.cpp | 49 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/URLTest.cpp | 26 |
9 files changed, 167 insertions, 43 deletions
diff --git a/Swiften/Base/Algorithm.h b/Swiften/Base/Algorithm.h index 108dbe3..ee761b7 100644 --- a/Swiften/Base/Algorithm.h +++ b/Swiften/Base/Algorithm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -160,4 +160,28 @@ namespace Swift { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin(), pred); } + + /** + * Ranges + */ + template <typename T> + class range_t { + public: + range_t(T b, T e) : b_(b), e_(e) {} + + T begin() { + return b_; + } + T end() { + return e_; + } + private: + T b_; + T e_; + }; + + template <typename T> + range_t<T> range(T b, T e) { + return range_t<T>(b, e); + } } diff --git a/Swiften/Base/DateTime.cpp b/Swiften/Base/DateTime.cpp index 4443566..23b3b84 100644 --- a/Swiften/Base/DateTime.cpp +++ b/Swiften/Base/DateTime.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -40,7 +40,7 @@ std::string dateTimeToLocalString(const boost::posix_time::ptime& time) { localString = boost::posix_time::to_simple_string(boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(time)); } catch(std::out_of_range& exception) { - SWIFT_LOG(debug) << exception.what() << std::endl; + SWIFT_LOG(debug) << exception.what(); } return localString; } diff --git a/Swiften/Base/Log.cpp b/Swiften/Base/Log.cpp index 0efac7e..b6f1851 100644 --- a/Swiften/Base/Log.cpp +++ b/Swiften/Base/Log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -16,6 +16,8 @@ namespace Swift { static Log::Severity logLevel = Log::warning; +std::unique_ptr<FILE, Log::LogFileClose> Log::logfile; +Log::Callback Log::logCallback; Log::Log() { } @@ -25,18 +27,38 @@ Log::~Log() { __android_log_print(ANDROID_LOG_VERBOSE, "Swift", stream.str().c_str(), 1); #else // Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic) - fprintf(stderr, "%s", stream.str().c_str()); - fflush(stderr); + if (logCallback) { + logCallback(severity_, std::move(file_), line_, std::move(function_), stream.str()); + } + else { + stream << std::endl; + if (logfile) { + fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), logfile.get()); + fflush(logfile.get()); + } + else { + fwrite(stream.str().c_str(), sizeof(char), stream.str().size(), stderr); + fflush(stderr); + } + } #endif } std::ostringstream& Log::getStream( - Severity /*severity*/, - const std::string& severityString, - const std::string& file, + Severity severity, + std::string severityString, + std::string file, int line, - const std::string& function) { - stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": "; + std::string function) { + if (logCallback) { + severity_ = severity; + file_ = std::move(file); + line_ = line; + function_ = std::move(function); + } + else { + stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": "; + } return stream; } @@ -48,4 +70,14 @@ void Log::setLogLevel(Severity level) { logLevel = level; } +void Log::setLogFile(const std::string& fileName) { + if (!fileName.empty()) { + logfile = std::unique_ptr<FILE, Log::LogFileClose>(fopen(fileName.c_str(), "a")); + } +} + +void Log::setLogCallback(Callback callback) { + Log::logCallback = callback; +} + } diff --git a/Swiften/Base/Log.h b/Swiften/Base/Log.h index 33c969d..255e478 100644 --- a/Swiften/Base/Log.h +++ b/Swiften/Base/Log.h @@ -6,6 +6,9 @@ #pragma once +#include <cstdio> +#include <functional> +#include <memory> #include <sstream> #include <Swiften/Base/API.h> @@ -16,22 +19,38 @@ namespace Swift { enum Severity { error, warning, info, debug }; + using Callback = std::function<void(Severity severity, std::string file, int line, std::string function, std::string message)>; Log(); ~Log(); std::ostringstream& getStream( Severity severity, - const std::string& severityString, - const std::string& file, + std::string severityString, + std::string file, int line, - const std::string& function); + std::string function); static Severity getLogLevel(); static void setLogLevel(Severity level); + static void setLogFile(const std::string& fileName); + static void setLogCallback(Callback callback); private: + struct LogFileClose { + void operator()(FILE* p) { + if (p) { + fclose(p); + } + } + }; std::ostringstream stream; + static std::unique_ptr<FILE, LogFileClose> logfile; + static Callback logCallback; + Severity severity_; + std::string file_; + int line_; + std::string function_; }; } diff --git a/Swiften/Base/Platform.h b/Swiften/Base/Platform.h index 4deba2b..22dff30 100644 --- a/Swiften/Base/Platform.h +++ b/Swiften/Base/Platform.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2019 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -43,9 +43,9 @@ #endif // Endianness -#include <boost/detail/endian.hpp> -#if defined(BOOST_LITTLE_ENDIAN) +#include <boost/predef/other/endian.h> +#if defined(BOOST_ENDIAN_LITTLE_BYTE) #define SWIFTEN_LITTLE_ENDIAN -#elif defined(BOOST_BIG_ENDIAN) +#elif defined(BOOST_ENDIAN_BIG_BYTE) #define SWIFTEN_BIG_ENDIAN #endif diff --git a/Swiften/Base/URL.cpp b/Swiften/Base/URL.cpp index 4a47a11..5c0f0d7 100644 --- a/Swiften/Base/URL.cpp +++ b/Swiften/Base/URL.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -11,7 +11,7 @@ namespace Swift { -int URL::getPortOrDefaultPort(const URL& url) { +unsigned short URL::getPortOrDefaultPort(const URL& url) { if (url.getPort()) { return *url.getPort(); } @@ -62,7 +62,7 @@ URL URL::fromString(const std::string& urlString) { } std::string host; - boost::optional<int> port; + boost::optional<unsigned short> port; if (hostAndPort[0] == '[') { // handle IPv6 address literals size_t addressEndIndex = hostAndPort.find(']'); @@ -71,9 +71,9 @@ URL URL::fromString(const std::string& urlString) { colonIndex = hostAndPort.find(':', addressEndIndex); if (colonIndex != std::string::npos) { try { - port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); + port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); } - catch (const boost::bad_lexical_cast&) { + catch (...) { return URL(); } } @@ -87,7 +87,7 @@ URL URL::fromString(const std::string& urlString) { if (colonIndex != std::string::npos) { host = unescape(hostAndPort.substr(0, colonIndex)); try { - port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1)); + port = boost::numeric_cast<unsigned short>(boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1))); } catch (const boost::bad_lexical_cast&) { return URL(); @@ -132,7 +132,7 @@ std::string URL::toString() const { } if (port) { result += ":"; - result += boost::lexical_cast<std::string>(*port); + result += std::to_string(*port); } result += path; return result; diff --git a/Swiften/Base/URL.h b/Swiften/Base/URL.h index 1a03efe..8fdb018 100644 --- a/Swiften/Base/URL.h +++ b/Swiften/Base/URL.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -21,7 +21,7 @@ class SWIFTEN_API URL { URL() : scheme(""), user(""), password(""), host(""), path(""), empty(true) { } - URL(const std::string& scheme, const std::string& host, int port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), empty(false) { + URL(const std::string& scheme, const std::string& host, unsigned short port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), empty(false) { } URL(const std::string& scheme, const std::string& host, const std::string& path) : scheme(scheme), user(), password(), host(host), path(path), empty(false) { @@ -51,7 +51,7 @@ class SWIFTEN_API URL { /** * Port number */ - boost::optional<int> getPort() const { + boost::optional<unsigned short> getPort() const { return port; } @@ -64,7 +64,7 @@ class SWIFTEN_API URL { std::string toString() const; - static int getPortOrDefaultPort(const URL& url); + static unsigned short getPortOrDefaultPort(const URL& url); static URL fromString(const std::string&); static std::string unescape(const std::string&); @@ -74,7 +74,7 @@ class SWIFTEN_API URL { std::string user; std::string password; std::string host; - boost::optional<int> port; + boost::optional<unsigned short> port; std::string path; bool empty; }; diff --git a/Swiften/Base/UnitTest/LogTest.cpp b/Swiften/Base/UnitTest/LogTest.cpp new file mode 100644 index 0000000..5d710db --- /dev/null +++ b/Swiften/Base/UnitTest/LogTest.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <vector> + +#include <boost/algorithm/string/predicate.hpp> + +#include <gtest/gtest.h> + +#include <Swiften/Base/Log.h> + +using namespace Swift; + +struct LogEntry { + LogEntry(Log::Severity severity, std::string file, int line, std::string function, std::string message) : severity(severity), file(std::move(file)), line(line), function(std::move(function)), message(std::move(message)) {} + + Log::Severity severity; + std::string file; + int line; + std::string function; + std::string message; +}; + +// Helper class to set the logging callback. Using this class to set it will ensure the +// logCallback is reset to empty (its default state) after each test. +class LogCallbackSetter { +public: + LogCallbackSetter(Log::Callback callback) { + Log::setLogCallback(callback); + } + ~LogCallbackSetter() { + Log::setLogCallback({}); + } +}; + +TEST(LogTest, testCallback) { + std::vector<LogEntry> logEntries; + LogCallbackSetter callbackSetter = {[&](Log::Severity severity, const std::string& file, int line, const std::string& function, const std::string& message) { + logEntries.emplace_back(severity, file, line, function, message); + }}; + + SWIFT_LOG(error) << "An error"; + ASSERT_EQ(1, logEntries.size()); + ASSERT_EQ(Log::error, logEntries[0].severity); + ASSERT_EQ("An error", logEntries[0].message); +} diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp index c38398a..da9f15c 100644 --- a/Swiften/Base/UnitTest/URLTest.cpp +++ b/Swiften/Base/UnitTest/URLTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2016 Isode Limited. + * Copyright (c) 2012-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -66,7 +66,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); } @@ -75,7 +75,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(11440, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(11440), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); } @@ -84,7 +84,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); } @@ -121,7 +121,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(12345, url.getPort().get_value_or(0)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12345), url.getPort().get_value_or(0)); CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); } @@ -137,7 +137,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(12435, url.getPort().get_value_or(0)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12435), url.getPort().get_value_or(0)); } void test_FromString_ToString_IPv6RFC2732() { @@ -147,7 +147,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -159,7 +159,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("1080:0:0:0:8:800:200C:417A"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -171,7 +171,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("3ffe:2a00:100:7031::1"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -183,7 +183,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("1080::8:800:200C:417A"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/foo"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -195,7 +195,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("::192.9.5.5"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/ipng"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -207,7 +207,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("::FFFF:129.144.52.38"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(80, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); @@ -219,7 +219,7 @@ class URLTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); CPPUNIT_ASSERT_EQUAL(std::string("2010:836B:4179::836B:4179"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(2, url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); CPPUNIT_ASSERT_EQUAL(std::string(), url.getPath()); CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); |