convert remaining alerts to use operation_t instead of string literal

This commit is contained in:
arvidn 2017-06-18 14:35:05 -04:00 committed by Arvid Norberg
parent ff454a92d4
commit 35491bc476
8 changed files with 59 additions and 32 deletions

View File

@ -7,6 +7,7 @@
#include <libtorrent/alert_types.hpp> #include <libtorrent/alert_types.hpp>
#include <libtorrent/piece_picker.hpp> // for piece_block #include <libtorrent/piece_picker.hpp> // for piece_block
#include <libtorrent/session_stats.hpp> #include <libtorrent/session_stats.hpp>
#include <libtorrent/operations.hpp>
#include <memory> #include <memory>
#include "bytes.hpp" #include "bytes.hpp"
@ -328,8 +329,11 @@ void bind_alert()
.value("partfile_move", operation_t::partfile_move) .value("partfile_move", operation_t::partfile_move)
.value("partfile_read", operation_t::partfile_read) .value("partfile_read", operation_t::partfile_read)
.value("partfile_write", operation_t::partfile_write) .value("partfile_write", operation_t::partfile_write)
.value("hostname_lookup", operation_t::hostname_lookup)
; ;
def("operation_name", static_cast<char const*(*)(operation_t)>(&lt::operation_name));
class_<torrent_alert, bases<alert>, noncopyable>( class_<torrent_alert, bases<alert>, noncopyable>(
"torrent_alert", no_init) "torrent_alert", no_init)
.def_readonly("handle", &torrent_alert::handle) .def_readonly("handle", &torrent_alert::handle)
@ -605,11 +609,12 @@ void bind_alert()
class_<fastresume_rejected_alert, bases<torrent_alert>, noncopyable>( class_<fastresume_rejected_alert, bases<torrent_alert>, noncopyable>(
"fastresume_rejected_alert", no_init) "fastresume_rejected_alert", no_init)
.def_readonly("error", &fastresume_rejected_alert::error) .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 #ifndef TORRENT_NO_DEPRECATE
.def_readonly("operation", &fastresume_rejected_alert::operation)
.def_readonly("msg", &fastresume_rejected_alert::msg) .def_readonly("msg", &fastresume_rejected_alert::msg)
#endif #endif
.def("file_path", &fastresume_rejected_alert::file_path)
.def_readonly("operation", &fastresume_rejected_alert::operation)
; ;
class_<peer_blocked_alert, bases<peer_alert>, noncopyable>( class_<peer_blocked_alert, bases<peer_alert>, noncopyable>(

View File

@ -529,6 +529,15 @@ class test_example_client(unittest.TestCase):
default = lt.default_settings() default = lt.default_settings()
print(default) 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__': if __name__ == '__main__':
print(lt.__version__) print(lt.__version__)
shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', shutil.copy(os.path.join('..', '..', 'test', 'test_torrents',

View File

@ -1488,7 +1488,7 @@ namespace libtorrent {
// internal // internal
fastresume_rejected_alert(aux::stack_allocator& alloc fastresume_rejected_alert(aux::stack_allocator& alloc
, torrent_handle const& h, error_code const& ec, string_view file , torrent_handle const& h, error_code const& ec, string_view file
, char const* op); , operation_t op);
TORRENT_DEFINE_ALERT(fastresume_rejected_alert, 53) 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. // If the error happened to a specific file, this returns the path to it.
char const* file_path() const; char const* file_path() const;
// If the error happened in a disk operation. a 0-terminated string of // the underlying operation that failed
// the name of that operation. ``operation`` is nullptr otherwise. operation_t op;
char const* operation;
private: private:
aux::allocation_slot m_path_idx; aux::allocation_slot m_path_idx;
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
public: 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. // 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 file;
std::string TORRENT_DEPRECATED_MEMBER msg; std::string TORRENT_DEPRECATED_MEMBER msg;
@ -1989,7 +1992,8 @@ namespace libtorrent {
struct TORRENT_EXPORT dht_error_alert final : alert struct TORRENT_EXPORT dht_error_alert final : alert
{ {
// internal // 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) TORRENT_DEFINE_ALERT(dht_error_alert, 73)
@ -1999,14 +2003,19 @@ namespace libtorrent {
// the error code // the error code
error_code error; error_code error;
// the operation that failed
operation_t op;
#ifndef TORRENT_NO_DEPRECATE
enum op_t enum op_t
{ {
unknown, unknown TORRENT_DEPRECATED_ENUM,
hostname_lookup hostname_lookup TORRENT_DEPRECATED_ENUM
}; };
// the operation that failed // 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(), // this alert is posted as a response to a call to session::get_item(),

View File

@ -521,11 +521,13 @@ namespace libtorrent {
// kind of operation failed. // kind of operation failed.
operation_t operation; operation_t operation;
#ifndef TORRENT_NO_DEPRECATE
// Returns a string literal representing the file operation // Returns a string literal representing the file operation
// that failed. If there were no failure, it returns // that failed. If there were no failure, it returns
// an empty string. // an empty string.
char const* operation_str() const char const* operation_str() const TORRENT_DEPRECATED_MEMBER
{ return operation_name(operation); } { return operation_name(operation); }
#endif
}; };
} }

View File

@ -133,6 +133,7 @@ namespace libtorrent {
partfile_move, partfile_move,
partfile_read, partfile_read,
partfile_write, partfile_write,
hostname_lookup,
}; };
// maps an operation id (from peer_error_alert and peer_disconnected_alert) // maps an operation id (from peer_error_alert and peer_disconnected_alert)

View File

@ -854,6 +854,7 @@ namespace {
case o::partfile_move: return -1; case o::partfile_move: return -1;
case o::partfile_read: return -1; case o::partfile_read: return -1;
case o::partfile_write: return -1; case o::partfile_write: return -1;
case o::hostname_lookup: return -1;
}; };
return -1; return -1;
} }
@ -1092,12 +1093,13 @@ namespace {
, torrent_handle const& h , torrent_handle const& h
, error_code const& ec , error_code const& ec
, string_view f , string_view f
, char const* op) , operation_t const op_)
: torrent_alert(alloc, h) : torrent_alert(alloc, h)
, error(ec) , error(ec)
, operation(op) , op(op_)
, m_path_idx(alloc.copy_string(f)) , m_path_idx(alloc.copy_string(f))
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
, operation(operation_name(op_))
, file(f) , file(f)
, msg(convert_from_native(error.message())) , msg(convert_from_native(error.message()))
#endif #endif
@ -1107,7 +1109,7 @@ namespace {
std::string fastresume_rejected_alert::message() const std::string fastresume_rejected_alert::message() const
{ {
return torrent_alert::message() + " fast resume rejected. " return torrent_alert::message() + " fast resume rejected. "
+ (operation?operation:"") + "(" + file_path() + "): " + operation_name(op) + "(" + file_path() + "): "
+ convert_from_native(error.message()); + convert_from_native(error.message());
} }
@ -1492,6 +1494,7 @@ namespace {
"partfile_move", "partfile_move",
"partfile_read", "partfile_read",
"partfile_write", "partfile_write",
"hostname_lookup"
}; };
int const idx = static_cast<int>(op); int const idx = static_cast<int>(op);
@ -1575,26 +1578,22 @@ namespace {
return buf; 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_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 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]; char msg[600];
std::snprintf(msg, sizeof(msg), "DHT error [%s] (%d) %s" std::snprintf(msg, sizeof(msg), "DHT error [%s] (%d) %s"
, operation_names[op] , operation_name(op)
, error.value() , error.value()
, convert_from_native(error.message()).c_str()); , convert_from_native(error.message()).c_str());
return msg; return msg;

View File

@ -5782,7 +5782,7 @@ namespace {
{ {
if (m_alerts.should_post<dht_error_alert>()) if (m_alerts.should_post<dht_error_alert>())
m_alerts.emplace_alert<dht_error_alert>( m_alerts.emplace_alert<dht_error_alert>(
dht_error_alert::hostname_lookup, e); operation_t::hostname_lookup, e);
return; return;
} }
@ -5812,7 +5812,7 @@ namespace {
{ {
if (m_alerts.should_post<dht_error_alert>()) if (m_alerts.should_post<dht_error_alert>())
m_alerts.emplace_alert<dht_error_alert>( m_alerts.emplace_alert<dht_error_alert>(
dht_error_alert::hostname_lookup, e); operation_t::hostname_lookup, e);
if (m_outstanding_router_lookups == 0) update_dht(); if (m_outstanding_router_lookups == 0) update_dht();
return; return;

View File

@ -595,7 +595,8 @@ namespace libtorrent {
&& m_ses.alerts().should_post<fastresume_rejected_alert>()) && m_ses.alerts().should_post<fastresume_rejected_alert>())
{ {
m_ses.alerts().emplace_alert<fastresume_rejected_alert>(get_handle() m_ses.alerts().emplace_alert<fastresume_rejected_alert>(get_handle()
, m_add_torrent_params->internal_resume_data_error, "", ""); , m_add_torrent_params->internal_resume_data_error, ""
, operation_t::unknown);
} }
#endif #endif
@ -1028,7 +1029,8 @@ namespace libtorrent {
{ {
debug_log("disk error: (%d) %s [%*s : %s] in file: %s" debug_log("disk error: (%d) %s [%*s : %s] in file: %s"
, error.ec.value(), error.ec.message().c_str() , 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()); , resolve_filename(error.file()).c_str());
} }
#endif #endif
@ -1982,7 +1984,7 @@ namespace libtorrent {
m_ses.alerts().emplace_alert<fastresume_rejected_alert>(get_handle() m_ses.alerts().emplace_alert<fastresume_rejected_alert>(get_handle()
, error.ec , error.ec
, resolve_filename(error.file()) , resolve_filename(error.file())
, error.operation_str()); , error.operation);
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING