diff options
Diffstat (limited to 'apt-pkg/contrib/error.cc')
-rw-r--r-- | apt-pkg/contrib/error.cc | 166 |
1 files changed, 86 insertions, 80 deletions
diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index e2e8d6e57..18810d2a4 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -18,6 +18,7 @@ #include <iostream> #include <errno.h> #include <stdio.h> +#include <stdlib.h> #include <unistd.h> #include <string> @@ -57,108 +58,112 @@ // GlobalError::GlobalError - Constructor /*{{{*/ GlobalError::GlobalError() : PendingFlag(false) {} /*}}}*/ -// GlobalError::FatalE - Get part of the error string from errno /*{{{*/ -bool GlobalError::FatalE(const char *Function,const char *Description,...) { - va_list args; - va_start(args,Description); - return InsertErrno(FATAL, Function, Description, args); -} - /*}}}*/ -// GlobalError::Errno - Get part of the error string from errno /*{{{*/ -bool GlobalError::Errno(const char *Function,const char *Description,...) { - va_list args; - va_start(args,Description); - return InsertErrno(ERROR, Function, Description, args); -} - /*}}}*/ -// GlobalError::WarningE - Get part of the warning string from errno /*{{{*/ -bool GlobalError::WarningE(const char *Function,const char *Description,...) { - va_list args; - va_start(args,Description); - return InsertErrno(WARNING, Function, Description, args); -} - /*}}}*/ -// GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/ -bool GlobalError::NoticeE(const char *Function,const char *Description,...) { - va_list args; - va_start(args,Description); - return InsertErrno(NOTICE, Function, Description, args); -} - /*}}}*/ -// GlobalError::DebugE - Get part of the debug string from errno /*{{{*/ -bool GlobalError::DebugE(const char *Function,const char *Description,...) { - va_list args; - va_start(args,Description); - return InsertErrno(DEBUG, Function, Description, args); +// GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/ +#define GEMessage(NAME, TYPE) \ +bool GlobalError::NAME (const char *Function, const char *Description,...) { \ + va_list args; \ + size_t msgSize = 400; \ + int const errsv = errno; \ + while (true) { \ + va_start(args,Description); \ + if (InsertErrno(TYPE, Function, Description, args, errsv, msgSize) == false) \ + break; \ + va_end(args); \ + } \ + return false; \ } +GEMessage(FatalE, FATAL) +GEMessage(Errno, ERROR) +GEMessage(WarningE, WARNING) +GEMessage(NoticeE, NOTICE) +GEMessage(DebugE, DEBUG) +#undef GEMessage /*}}}*/ // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/ bool GlobalError::InsertErrno(MsgType const &type, const char *Function, const char *Description,...) { va_list args; - va_start(args,Description); - return InsertErrno(type, Function, Description, args); + size_t msgSize = 400; + int const errsv = errno; + while (true) { + va_start(args,Description); + if (InsertErrno(type, Function, Description, args, errsv, msgSize) == false) + break; + va_end(args); + } + return false; } /*}}}*/ // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/ bool GlobalError::InsertErrno(MsgType type, const char* Function, - const char* Description, va_list &args) { - char S[400]; - snprintf(S, sizeof(S), "%s - %s (%i: %s)", Description, - Function, errno, strerror(errno)); - return Insert(type, S, args); -} - /*}}}*/ -// GlobalError::Fatal - Add a fatal error to the list /*{{{*/ -bool GlobalError::Fatal(const char *Description,...) { - va_list args; - va_start(args,Description); - return Insert(FATAL, Description, args); -} - /*}}}*/ -// GlobalError::Error - Add an error to the list /*{{{*/ -bool GlobalError::Error(const char *Description,...) { - va_list args; - va_start(args,Description); - return Insert(ERROR, Description, args); -} - /*}}}*/ -// GlobalError::Warning - Add a warning to the list /*{{{*/ -bool GlobalError::Warning(const char *Description,...) { - va_list args; - va_start(args,Description); - return Insert(WARNING, Description, args); -} - /*}}}*/ -// GlobalError::Notice - Add a notice to the list /*{{{*/ -bool GlobalError::Notice(const char *Description,...) -{ - va_list args; - va_start(args,Description); - return Insert(NOTICE, Description, args); + const char* Description, va_list &args, + int const errsv, size_t &msgSize) { + char* S = (char*) malloc(msgSize); + int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description, + Function, errsv, strerror(errsv)); + if (n > -1 && ((unsigned int) n) < msgSize); + else { + if (n > -1) + msgSize = n + 1; + else + msgSize *= 2; + free(S); + return true; + } + + bool const geins = Insert(type, S, args, msgSize); + free(S); + return geins; } /*}}}*/ -// GlobalError::Debug - Add a debug to the list /*{{{*/ -bool GlobalError::Debug(const char *Description,...) -{ - va_list args; - va_start(args,Description); - return Insert(DEBUG, Description, args); +// GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/ +#define GEMessage(NAME, TYPE) \ +bool GlobalError::NAME (const char *Description,...) { \ + va_list args; \ + size_t msgSize = 400; \ + while (true) { \ + va_start(args,Description); \ + if (Insert(TYPE, Description, args, msgSize) == false) \ + break; \ + va_end(args); \ + } \ + return false; \ } +GEMessage(Fatal, FATAL) +GEMessage(Error, ERROR) +GEMessage(Warning, WARNING) +GEMessage(Notice, NOTICE) +GEMessage(Debug, DEBUG) +#undef GEMessage /*}}}*/ // GlobalError::Insert - Add a errotype message to the list /*{{{*/ bool GlobalError::Insert(MsgType const &type, const char *Description,...) { va_list args; - va_start(args,Description); - return Insert(type, Description, args); + size_t msgSize = 400; + while (true) { + va_start(args,Description); + if (Insert(type, Description, args, msgSize) == false) + break; + va_end(args); + } + return false; } /*}}}*/ // GlobalError::Insert - Insert a new item at the end /*{{{*/ bool GlobalError::Insert(MsgType type, const char* Description, - va_list &args) { - char S[400]; - vsnprintf(S,sizeof(S),Description,args); + va_list &args, size_t &msgSize) { + char* S = (char*) malloc(msgSize); + int const n = vsnprintf(S, msgSize, Description, args); + if (n > -1 && ((unsigned int) n) < msgSize); + else { + if (n > -1) + msgSize = n + 1; + else + msgSize *= 2; + free(S); + return true; + } Item const m(S, type); Messages.push_back(m); @@ -169,6 +174,7 @@ bool GlobalError::Insert(MsgType type, const char* Description, if (type == FATAL || type == DEBUG) std::clog << m << std::endl; + free(S); return false; } /*}}}*/ |