From f129841b385a991e9c7caeaf40f340dc0beb6ff1 Mon Sep 17 00:00:00 2001 From: crs Date: Thu, 25 Jul 2002 17:52:40 +0000 Subject: [PATCH] made all getWhat() methods on exceptions consistent. they now all use format() the same way. also changed format() to actually do formatting. however, it doesn't try looking up formatting strings by id, it just uses the fallback format string. --- base/XBase.cpp | 20 +++++++++++++++++--- http/XHTTP.cpp | 20 ++++++++------------ io/XIO.cpp | 2 +- net/XNetwork.cpp | 7 ++++--- net/XSocket.cpp | 21 +++++++++++++++------ server/CConfig.cpp | 2 +- server/server.cpp | 12 ++++++------ synergy/CProtocolUtil.cpp | 2 +- synergy/XScreen.cpp | 2 +- synergy/XSynergy.cpp | 8 +++++--- 10 files changed, 59 insertions(+), 37 deletions(-) diff --git a/base/XBase.cpp b/base/XBase.cpp index fba37293d..19a93a13e 100644 --- a/base/XBase.cpp +++ b/base/XBase.cpp @@ -1,5 +1,6 @@ #include "XBase.h" #include +#include // win32 wants a const char* argument to std::exception c'tor #if WINDOWS_LIKE @@ -46,9 +47,22 @@ XBase::what() const CString XBase::format(const char* /*id*/, const char* fmt, ...) const throw() { - // FIXME -- use id to lookup formating string - // FIXME -- format string with arguments - return fmt; + // FIXME -- lookup message string using id as an index. set + // fmt to that string if it exists. + + // format + CString result; + va_list args; + va_start(args, fmt); + try { + result = CStringUtil::vformat(fmt, args); + } + catch (...) { + // ignore + } + va_end(args); + + return result; } diff --git a/http/XHTTP.cpp b/http/XHTTP.cpp index 8ef27b61d..b6535d9f1 100644 --- a/http/XHTTP.cpp +++ b/http/XHTTP.cpp @@ -48,20 +48,16 @@ XHTTP::addHeaders(CHTTPReply&) const CString XHTTP::getWhat() const throw() { - try { - std::ostringstream s; - s << m_status << " "; - if (!m_reason.empty()) { - s << m_reason.c_str(); - } - else { - s << getReason(m_status); - } - return s.str(); + const char* reason; + if (m_reason.empty()) { + reason = getReason(m_status); } - catch (...) { - return CString(); + else { + reason = m_reason.c_str(); } + return format("XHTTP", "%{1} %{2}", + CStringUtil::print("%d", m_status).c_str(), + reason); } const char* diff --git a/io/XIO.cpp b/io/XIO.cpp index 675088653..e5b07e3d3 100644 --- a/io/XIO.cpp +++ b/io/XIO.cpp @@ -24,7 +24,7 @@ XIOErrno::XIOErrno(int err) : CString XIOClose::getWhat() const throw() { - return format("XIOClose", "close: %1", XIOErrno::getErrstr()); + return format("XIOClose", "close: %{1}", XIOErrno::getErrstr()); } diff --git a/net/XNetwork.cpp b/net/XNetwork.cpp index ece106bde..a6e77e587 100644 --- a/net/XNetwork.cpp +++ b/net/XNetwork.cpp @@ -49,8 +49,9 @@ CString XNetworkVersion::getWhat() const throw() { return format("XNetworkVersion", - "unsupported network version %d.%d", - m_major, m_minor); + "unsupported network version %{1}.%{2}", + CStringUtil::print("%d", m_major).c_str(), + CStringUtil::print("%d", m_minor).c_str()); } @@ -73,6 +74,6 @@ CString XNetworkFunctionUnavailable::getWhat() const throw() { return format("XNetworkFunctionUnavailable", - "missing network function %s", + "missing network function %{1}", m_name.c_str()); } diff --git a/net/XSocket.cpp b/net/XSocket.cpp index 7cb6e995f..f746abc5c 100644 --- a/net/XSocket.cpp +++ b/net/XSocket.cpp @@ -34,12 +34,21 @@ XSocketAddress::getPort() const throw() CString XSocketAddress::getWhat() const throw() { - return "no address"; -/* FIXME - return format("XSocketAddress", "no address: %1:%2", - m_hostname.t_str(), - CString::sprintf("%d", m_port).t_str()); -*/ + static const char* s_errorID[] = { + "XSocketAddressUnknown", + "XSocketAddressNotFound", + "XSocketAddressNoAddress", + "XSocketAddressBadPort" + }; + static const char* s_errorMsg[] = { + "unknown error for: %{1}:%{2}", + "address not found for: %{1}", + "no address for: %{1}", + "invalid port: %{2}" + }; + return format(s_errorID[m_error], s_errorMsg[m_error], + m_hostname.c_str(), + CStringUtil::print("%d", m_port).c_str()); } diff --git a/server/CConfig.cpp b/server/CConfig.cpp index 082f7a477..62fcf35ec 100644 --- a/server/CConfig.cpp +++ b/server/CConfig.cpp @@ -695,5 +695,5 @@ XConfigRead::~XConfigRead() CString XConfigRead::getWhat() const throw() { - return m_error; + return format("XConfigRead", "read error: %s", m_error.c_str()); } diff --git a/server/server.cpp b/server/server.cpp index 14e1f6969..86081fa54 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -336,9 +336,9 @@ parse(int argc, const char** argv) try { s_synergyAddress = CNetworkAddress(argv[i + 1], kDefaultPort); } - catch (XSocketAddress&) { - log((CLOG_PRINT "%s: invalid address for `%s'" BYE, - pname, argv[i], pname)); + catch (XSocketAddress& e) { + log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE, + pname, argv[i], e.what(), pname)); bye(kExitArgs); } ++i; @@ -349,9 +349,9 @@ parse(int argc, const char** argv) try { s_httpAddress = CNetworkAddress(argv[i + 1], kDefaultPort + 1); } - catch (XSocketAddress&) { - log((CLOG_PRINT "%s: invalid address for `%s'" BYE, - pname, argv[i], pname)); + catch (XSocketAddress& e) { + log((CLOG_PRINT "%s: invalid address for `%s'. %s." BYE, + pname, argv[i], e.what(), pname)); bye(kExitArgs); } ++i; diff --git a/synergy/CProtocolUtil.cpp b/synergy/CProtocolUtil.cpp index 9426a2f64..0853a8fb8 100644 --- a/synergy/CProtocolUtil.cpp +++ b/synergy/CProtocolUtil.cpp @@ -370,5 +370,5 @@ CProtocolUtil::read(IInputStream* stream, void* vbuffer, UInt32 count) CString XIOReadMismatch::getWhat() const throw() { - return "CProtocolUtil::readf() mismatch"; + return format("XIOReadMismatch", "CProtocolUtil::readf() mismatch"); } diff --git a/synergy/XScreen.cpp b/synergy/XScreen.cpp index 2354b04c5..b011b1319 100644 --- a/synergy/XScreen.cpp +++ b/synergy/XScreen.cpp @@ -7,5 +7,5 @@ CString XScreenOpenFailure::getWhat() const throw() { - return "XScreenOpenFailure"; + return format("XScreenOpenFailure", "unable to open screen"); } diff --git a/synergy/XSynergy.cpp b/synergy/XSynergy.cpp index 8c6edd113..c8b976f84 100644 --- a/synergy/XSynergy.cpp +++ b/synergy/XSynergy.cpp @@ -37,7 +37,9 @@ XIncompatibleClient::getMinor() const throw() CString XIncompatibleClient::getWhat() const throw() { - return "XIncompatibleClient"; + return format("XIncompatibleClient", "incompatible client %{1}.%{2}", + CStringUtil::print("%d", m_major).c_str(), + CStringUtil::print("%d", m_minor).c_str()); } @@ -60,7 +62,7 @@ XDuplicateClient::getName() const throw() CString XDuplicateClient::getWhat() const throw() { - return "XDuplicateClient"; + return format("XDuplicateClient", "duplicate client %{1}", m_name.c_str()); } @@ -83,5 +85,5 @@ XUnknownClient::getName() const throw() CString XUnknownClient::getWhat() const throw() { - return "XUnknownClient"; + return format("XUnknownClient", "unknown client %{1}", m_name.c_str()); }