replaced boost.lexical_cast with stringstream in alert message formaters to avoid throwing exceptions

This commit is contained in:
Arvid Norberg 2008-10-18 17:22:31 +00:00
parent 85bde58902
commit 9bcd5be3fc
1 changed files with 75 additions and 51 deletions

View File

@ -114,9 +114,10 @@ namespace libtorrent
virtual char const* what() const { return "file renamed"; } virtual char const* what() const { return "file renamed"; }
virtual std::string message() const virtual std::string message() const
{ {
return torrent_alert::message() + ": file " std::stringstream ret;
+ boost::lexical_cast<std::string>(index) + " renamed to " ret << torrent_alert::message() << ": file "
+ name; << index << " renamed to " << name;
return ret.str();
} }
std::string name; std::string name;
@ -139,9 +140,10 @@ namespace libtorrent
virtual char const* what() const { return "file rename failed"; } virtual char const* what() const { return "file rename failed"; }
virtual std::string message() const virtual std::string message() const
{ {
return torrent_alert::message() + ": failed to rename file " std::stringstream ret;
+ boost::lexical_cast<std::string>(index) + ": " ret << torrent_alert::message() << ": failed to rename file "
+ msg; << index << ": " << msg;
return ret.str();
} }
const static int static_category = alert::storage_notification; const static int static_category = alert::storage_notification;
@ -236,10 +238,10 @@ namespace libtorrent
virtual char const* what() const { return "tracker error"; } virtual char const* what() const { return "tracker error"; }
virtual std::string message() const virtual std::string message() const
{ {
return tracker_alert::message() + " (" std::stringstream ret;
+ boost::lexical_cast<std::string>(status_code) ret << tracker_alert::message() << " (" << status_code << ") "
+ ") " + msg + " (" + boost::lexical_cast<std::string>(times_in_row) << msg << " (" << times_in_row << ")";
+ ")"; return ret.str();
} }
int times_in_row; int times_in_row;
@ -288,9 +290,10 @@ namespace libtorrent
virtual char const* what() const { return "tracker scrape reply"; } virtual char const* what() const { return "tracker scrape reply"; }
virtual std::string message() const virtual std::string message() const
{ {
return tracker_alert::message() + " scrape reply: " std::stringstream ret;
+ boost::lexical_cast<std::string>(incomplete) + " " ret << tracker_alert::message() << " scrape reply: " << incomplete
+ boost::lexical_cast<std::string>(complete); << " " << complete;
return ret.str();
} }
}; };
@ -332,8 +335,10 @@ namespace libtorrent
virtual char const* what() const { return "tracker reply"; } virtual char const* what() const { return "tracker reply"; }
virtual std::string message() const virtual std::string message() const
{ {
return tracker_alert::message() + " received peers: " std::stringstream ret;
+ boost::lexical_cast<std::string>(num_peers); ret << tracker_alert::message() << " received peers: "
<< num_peers;
return ret.str();
} }
}; };
@ -352,8 +357,10 @@ namespace libtorrent
virtual char const* what() const { return "DHT reply"; } virtual char const* what() const { return "DHT reply"; }
virtual std::string message() const virtual std::string message() const
{ {
return torrent_alert::message() + " received DHT peers: " std::stringstream ret;
+ boost::lexical_cast<std::string>(num_peers); ret << torrent_alert::message() << " received DHT peers: "
<< num_peers;
return ret.str();
} }
}; };
@ -393,8 +400,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return torrent_alert::message() + " hash for piece " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " failed"; ret << torrent_alert::message() << " hash for piece "
<< piece_index << " failed";
return ret.str();
} }
int piece_index; int piece_index;
@ -523,10 +532,11 @@ namespace libtorrent
virtual char const* what() const { return "invalid piece request"; } virtual char const* what() const { return "invalid piece request"; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " peer sent an invalid piece request " std::stringstream ret;
"( piece: " + boost::lexical_cast<std::string>(request.piece) ret << peer_alert::message() << " peer sent an invalid piece request "
+ " start: " + boost::lexical_cast<std::string>(request.start) "( piece: " << request.piece << " start: " << request.start
+ " len: " + boost::lexical_cast<std::string>(request.length) + ")"; << " len: " << request.length << ")";
return ret.str();
} }
peer_request request; peer_request request;
@ -568,8 +578,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return torrent_alert::message() + " piece " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " finished downloading"; ret << torrent_alert::message() << " piece " << piece_index
<< " finished downloading";
return ret.str();
} }
}; };
@ -593,9 +605,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " peer dropped block ( piece: " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " block: " ret << peer_alert::message() << " peer dropped block ( piece: "
+ boost::lexical_cast<std::string>(block_index) + ")"; << piece_index << " block: " << block_index << ")";
return ret.str();
} }
}; };
@ -619,9 +632,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " peer timed out request ( piece: " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " block: " ret << peer_alert::message() << " peer timed out request ( piece: "
+ boost::lexical_cast<std::string>(block_index) + ")"; << piece_index << " block: " << block_index << ")";
return ret.str();
} }
}; };
@ -644,9 +658,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " block finished downloading ( piece: " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " block: " ret << peer_alert::message() << " block finished downloading ( piece: "
+ boost::lexical_cast<std::string>(block_index) + ")"; << piece_index << " block: " << block_index << ")";
return ret.str();
} }
}; };
@ -671,10 +686,10 @@ namespace libtorrent
virtual int category() const { return static_category; } virtual int category() const { return static_category; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " requested block ( piece: " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " block: " ret << peer_alert::message() << " requested block ( piece: "
+ boost::lexical_cast<std::string>(block_index) + ") " << piece_index << " block: " << block_index << ") " << peer_speedmsg;
+ peer_speedmsg; return ret.str();
} }
}; };
@ -695,9 +710,10 @@ namespace libtorrent
virtual char const* what() const { return "unwanted block received"; } virtual char const* what() const { return "unwanted block received"; }
virtual std::string message() const virtual std::string message() const
{ {
return peer_alert::message() + " received block not in download queue ( piece: " std::stringstream ret;
+ boost::lexical_cast<std::string>(piece_index) + " block: " ret << peer_alert::message() << " received block not in download queue ( piece: "
+ boost::lexical_cast<std::string>(block_index) + ")"; << piece_index << " block: " << block_index << ")";
return ret.str();
} }
}; };
@ -1009,8 +1025,10 @@ namespace libtorrent
virtual std::string message() const virtual std::string message() const
{ {
error_code ec; error_code ec;
return "listening on " + boost::lexical_cast<std::string>(endpoint) std::stringstream ret;
+ " failed: " + error.message(); ret << "listening on " << endpoint
<< " failed: " << error.message();
return ret.str();
} }
}; };
@ -1030,7 +1048,9 @@ namespace libtorrent
virtual std::string message() const virtual std::string message() const
{ {
error_code ec; error_code ec;
return "successfully listening on " + boost::lexical_cast<std::string>(endpoint); std::stringstream ret;
ret << "successfully listening on " << endpoint;
return ret.str();
} }
}; };
@ -1076,8 +1096,10 @@ namespace libtorrent
virtual std::string message() const virtual std::string message() const
{ {
static char const* type_str[] = {"NAT-PMP", "UPnP"}; static char const* type_str[] = {"NAT-PMP", "UPnP"};
return std::string("successfully mapped port using ") + type_str[type] std::stringstream ret;
+ ". external port: " + boost::lexical_cast<std::string>(external_port); ret << "successfully mapped port using " << type_str[type]
<< ". external port: " << external_port;
return ret.str();
} }
}; };
@ -1144,9 +1166,10 @@ namespace libtorrent
virtual std::string message() const virtual std::string message() const
{ {
error_code ec; error_code ec;
return "incoming dht annonce: " + ip.to_string(ec) + ":" std::stringstream ret;
+ boost::lexical_cast<std::string>(port) + " (" ret << "incoming dht annonce: " << ip.to_string(ec) << ":"
+ boost::lexical_cast<std::string>(info_hash) + ")"; << port << " (" << info_hash << ")";
return ret.str();
} }
}; };
@ -1166,8 +1189,9 @@ namespace libtorrent
virtual std::string message() const virtual std::string message() const
{ {
error_code ec; error_code ec;
return "incoming dht get_peers: " std::stringstream ret;
+ boost::lexical_cast<std::string>(info_hash); ret << "incoming dht get_peers: " << info_hash;
return ret.str();
} }
}; };
} }