From 35491bc476876d22f1c4b9c970c97555bf1963ea Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 18 Jun 2017 14:35:05 -0400 Subject: [PATCH] convert remaining alerts to use operation_t instead of string literal --- bindings/python/src/alert.cpp | 9 +++++++-- bindings/python/test.py | 9 +++++++++ include/libtorrent/alert_types.hpp | 25 ++++++++++++++++-------- include/libtorrent/error_code.hpp | 4 +++- include/libtorrent/operations.hpp | 1 + src/alert.cpp | 31 +++++++++++++++--------------- src/session_impl.cpp | 4 ++-- src/torrent.cpp | 8 +++++--- 8 files changed, 59 insertions(+), 32 deletions(-) diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 24792f747..f036ba267 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -7,6 +7,7 @@ #include #include // for piece_block #include +#include #include #include "bytes.hpp" @@ -328,8 +329,11 @@ void bind_alert() .value("partfile_move", operation_t::partfile_move) .value("partfile_read", operation_t::partfile_read) .value("partfile_write", operation_t::partfile_write) + .value("hostname_lookup", operation_t::hostname_lookup) ; + def("operation_name", static_cast(<::operation_name)); + class_, noncopyable>( "torrent_alert", no_init) .def_readonly("handle", &torrent_alert::handle) @@ -605,11 +609,12 @@ void bind_alert() class_, noncopyable>( "fastresume_rejected_alert", no_init) .def_readonly("error", &fastresume_rejected_alert::error) + .def("file_path", &fastresume_rejected_alert::file_path) + .def_readonly("op", &fastresume_rejected_alert::op) #ifndef TORRENT_NO_DEPRECATE + .def_readonly("operation", &fastresume_rejected_alert::operation) .def_readonly("msg", &fastresume_rejected_alert::msg) #endif - .def("file_path", &fastresume_rejected_alert::file_path) - .def_readonly("operation", &fastresume_rejected_alert::operation) ; class_, noncopyable>( diff --git a/bindings/python/test.py b/bindings/python/test.py index 4c44a6057..0340324a3 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -529,6 +529,15 @@ class test_example_client(unittest.TestCase): default = lt.default_settings() print(default) +class test_operation_t(unittest.TestCase): + + def test_enum(self): + self.assertEqual(lt.operation_name(lt.operation_t.sock_accept), "sock_accept") + self.assertEqual(lt.operation_name(lt.operation_t.unknown), "unknown") + self.assertEqual(lt.operation_name(lt.operation_t.mkdir), "mkdir") + self.assertEqual(lt.operation_name(lt.operation_t.partfile_write), "partfile_write") + self.assertEqual(lt.operation_name(lt.operation_t.hostname_lookup), "hostname_lookup") + if __name__ == '__main__': print(lt.__version__) shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 872279d18..2e05f47b8 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1488,7 +1488,7 @@ namespace libtorrent { // internal fastresume_rejected_alert(aux::stack_allocator& alloc , torrent_handle const& h, error_code const& ec, string_view file - , char const* op); + , operation_t op); TORRENT_DEFINE_ALERT(fastresume_rejected_alert, 53) @@ -1501,14 +1501,17 @@ namespace libtorrent { // If the error happened to a specific file, this returns the path to it. char const* file_path() const; - // If the error happened in a disk operation. a 0-terminated string of - // the name of that operation. ``operation`` is nullptr otherwise. - char const* operation; + // the underlying operation that failed + operation_t op; private: aux::allocation_slot m_path_idx; #ifndef TORRENT_NO_DEPRECATE public: + // If the error happened in a disk operation. a 0-terminated string of + // the name of that operation. ``operation`` is nullptr otherwise. + char const* TORRENT_DEPRECATED_MEMBER operation; + // If the error happened to a specific file, ``file`` is the path to it. std::string TORRENT_DEPRECATED_MEMBER file; std::string TORRENT_DEPRECATED_MEMBER msg; @@ -1989,7 +1992,8 @@ namespace libtorrent { struct TORRENT_EXPORT dht_error_alert final : alert { // internal - dht_error_alert(aux::stack_allocator& alloc, int op, error_code const& ec); + dht_error_alert(aux::stack_allocator& alloc, operation_t op + , error_code const& ec); TORRENT_DEFINE_ALERT(dht_error_alert, 73) @@ -1999,14 +2003,19 @@ namespace libtorrent { // the error code error_code error; + // the operation that failed + operation_t op; + +#ifndef TORRENT_NO_DEPRECATE enum op_t { - unknown, - hostname_lookup + unknown TORRENT_DEPRECATED_ENUM, + hostname_lookup TORRENT_DEPRECATED_ENUM }; // the operation that failed - op_t const operation; + op_t const TORRENT_DEPRECATED_MEMBER operation; +#endif }; // this alert is posted as a response to a call to session::get_item(), diff --git a/include/libtorrent/error_code.hpp b/include/libtorrent/error_code.hpp index 7da8342c0..5daea9bb7 100644 --- a/include/libtorrent/error_code.hpp +++ b/include/libtorrent/error_code.hpp @@ -521,11 +521,13 @@ namespace libtorrent { // kind of operation failed. operation_t operation; +#ifndef TORRENT_NO_DEPRECATE // Returns a string literal representing the file operation // that failed. If there were no failure, it returns // an empty string. - char const* operation_str() const + char const* operation_str() const TORRENT_DEPRECATED_MEMBER { return operation_name(operation); } +#endif }; } diff --git a/include/libtorrent/operations.hpp b/include/libtorrent/operations.hpp index 459c7fdbd..5b6fd64d6 100644 --- a/include/libtorrent/operations.hpp +++ b/include/libtorrent/operations.hpp @@ -133,6 +133,7 @@ namespace libtorrent { partfile_move, partfile_read, partfile_write, + hostname_lookup, }; // maps an operation id (from peer_error_alert and peer_disconnected_alert) diff --git a/src/alert.cpp b/src/alert.cpp index 2294b6273..d672b0c35 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -854,6 +854,7 @@ namespace { case o::partfile_move: return -1; case o::partfile_read: return -1; case o::partfile_write: return -1; + case o::hostname_lookup: return -1; }; return -1; } @@ -1092,12 +1093,13 @@ namespace { , torrent_handle const& h , error_code const& ec , string_view f - , char const* op) + , operation_t const op_) : torrent_alert(alloc, h) , error(ec) - , operation(op) + , op(op_) , m_path_idx(alloc.copy_string(f)) #ifndef TORRENT_NO_DEPRECATE + , operation(operation_name(op_)) , file(f) , msg(convert_from_native(error.message())) #endif @@ -1107,7 +1109,7 @@ namespace { std::string fastresume_rejected_alert::message() const { return torrent_alert::message() + " fast resume rejected. " - + (operation?operation:"") + "(" + file_path() + "): " + + operation_name(op) + "(" + file_path() + "): " + convert_from_native(error.message()); } @@ -1492,6 +1494,7 @@ namespace { "partfile_move", "partfile_read", "partfile_write", + "hostname_lookup" }; int const idx = static_cast(op); @@ -1575,26 +1578,22 @@ namespace { return buf; } - dht_error_alert::dht_error_alert(aux::stack_allocator&, int op + dht_error_alert::dht_error_alert(aux::stack_allocator& + , operation_t const op_ , error_code const& ec) - : error(ec), operation(op_t(op)) + : error(ec) + , op(op_) +#ifndef TORRENT_NO_DEPRECATE + , operation(op_ == operation_t::hostname_lookup + ? op_t::hostname_lookup : op_t::unknown) +#endif {} std::string dht_error_alert::message() const { - static const char* const operation_names[] = - { - "unknown", - "hostname lookup" - }; - - int op = operation; - if (op < 0 || op >= int(sizeof(operation_names)/sizeof(operation_names[0]))) - op = 0; - char msg[600]; std::snprintf(msg, sizeof(msg), "DHT error [%s] (%d) %s" - , operation_names[op] + , operation_name(op) , error.value() , convert_from_native(error.message()).c_str()); return msg; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index f8a46874e..8b1f883db 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -5782,7 +5782,7 @@ namespace { { if (m_alerts.should_post()) m_alerts.emplace_alert( - dht_error_alert::hostname_lookup, e); + operation_t::hostname_lookup, e); return; } @@ -5812,7 +5812,7 @@ namespace { { if (m_alerts.should_post()) m_alerts.emplace_alert( - dht_error_alert::hostname_lookup, e); + operation_t::hostname_lookup, e); if (m_outstanding_router_lookups == 0) update_dht(); return; diff --git a/src/torrent.cpp b/src/torrent.cpp index d63f7488f..d35e49f5d 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -595,7 +595,8 @@ namespace libtorrent { && m_ses.alerts().should_post()) { m_ses.alerts().emplace_alert(get_handle() - , m_add_torrent_params->internal_resume_data_error, "", ""); + , m_add_torrent_params->internal_resume_data_error, "" + , operation_t::unknown); } #endif @@ -1028,7 +1029,8 @@ namespace libtorrent { { debug_log("disk error: (%d) %s [%*s : %s] in file: %s" , error.ec.value(), error.ec.message().c_str() - , int(job_name.size()), job_name.data(), error.operation_str() + , int(job_name.size()), job_name.data() + , operation_name(error.operation) , resolve_filename(error.file()).c_str()); } #endif @@ -1982,7 +1984,7 @@ namespace libtorrent { m_ses.alerts().emplace_alert(get_handle() , error.ec , resolve_filename(error.file()) - , error.operation_str()); + , error.operation); } #ifndef TORRENT_DISABLE_LOGGING