From f2716db11825088fc0f7a5125f77c009d5418222 Mon Sep 17 00:00:00 2001 From: Kevin Rocard Date: Mon, 1 Dec 2014 11:34:01 +0100 Subject: [PATCH 1/2] Refactor varadic argument log formating Handling of varadic log formating. It used to be duplicated in 4 functions. Create a new vLog function, taking a va_list as parameter and formating the log. All 4 previous mention varadic argument log functions now forward their argument to vLog. Signed-off-by: Kevin Rocard --- parameter/Element.cpp | 32 ++++++++++---------------------- parameter/Element.h | 2 ++ parameter/SubsystemObject.cpp | 24 ++---------------------- 3 files changed, 14 insertions(+), 44 deletions(-) diff --git a/parameter/Element.cpp b/parameter/Element.cpp index 547c5914d..67d711ca3 100644 --- a/parameter/Element.cpp +++ b/parameter/Element.cpp @@ -52,38 +52,18 @@ CElement::~CElement() // Logging void CElement::log_info(const char* strMessage, ...) const { - char *pacBuffer; va_list listPointer; - va_start(listPointer, strMessage); - - vasprintf(&pacBuffer, strMessage, listPointer); - + vLog(false, strMessage, listPointer); va_end(listPointer); - - if (pacBuffer != NULL) { - doLog(false, pacBuffer); - } - - free(pacBuffer); } void CElement::log_warning(const char* strMessage, ...) const { - char *pacBuffer; va_list listPointer; - va_start(listPointer, strMessage); - - vasprintf(&pacBuffer, strMessage, listPointer); - + vLog(true, strMessage, listPointer); va_end(listPointer); - - if (pacBuffer != NULL) { - doLog(true, pacBuffer); - } - - free(pacBuffer); } // Log each element of the string list @@ -99,6 +79,14 @@ void CElement::log_table(bool bIsWarning, const std::list lstrMessage) c } } +void CElement::vLog(bool bIsWarning, const char *strMessage, va_list args) const +{ + char *pacBuffer; + vasprintf(&pacBuffer, strMessage, args); + doLog(bIsWarning, pacBuffer); + free(pacBuffer); +} + void CElement::doLog(bool bIsWarning, const string& strLog) const { assert(_pParent); diff --git a/parameter/Element.h b/parameter/Element.h index 9bc6e876a..6f7ebba48 100644 --- a/parameter/Element.h +++ b/parameter/Element.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "XmlSink.h" #include "XmlSource.h" @@ -52,6 +53,7 @@ class CElement : public IXmlSink, public IXmlSource void log_info(const char* strMessage, ...) const; void log_warning(const char* strMessage, ...) const; void log_table(bool bIsWarning, const std::list lstrMessage) const; + void vLog(bool bIsWarning, const char *strMessage, va_list args) const; // Description void setDescription(const std::string& strDescription); diff --git a/parameter/SubsystemObject.cpp b/parameter/SubsystemObject.cpp index e6b7b442d..644389e29 100644 --- a/parameter/SubsystemObject.cpp +++ b/parameter/SubsystemObject.cpp @@ -215,38 +215,18 @@ void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize) // Logging void CSubsystemObject::log_info(std::string strMessage, ...) const { - char *pacBuffer; va_list listPointer; - va_start(listPointer, strMessage); - - vasprintf(&pacBuffer, strMessage.c_str(), listPointer); - + _pInstanceConfigurableElement->vLog(false, strMessage.c_str(), listPointer); va_end(listPointer); - - if (pacBuffer != NULL) { - _pInstanceConfigurableElement->log_info("%s", pacBuffer); - } - - free(pacBuffer); } void CSubsystemObject::log_warning(std::string strMessage, ...) const { - char *pacBuffer; va_list listPointer; - va_start(listPointer, strMessage); - - vasprintf(&pacBuffer, strMessage.c_str(), listPointer); - + _pInstanceConfigurableElement->vLog(true, strMessage.c_str(), listPointer); va_end(listPointer); - - if (pacBuffer != NULL) { - _pInstanceConfigurableElement->log_warning("%s", pacBuffer); - } - - free(pacBuffer); } // Configurable element retrieval From b7bfb4b581150fa09189ddc5de008e712950da38 Mon Sep 17 00:00:00 2001 From: Kevin Rocard Date: Mon, 1 Dec 2014 11:34:01 +0100 Subject: [PATCH 2/2] vasprintf fails by returning -1 int vasprintf(char **strp, const char *fmt, va_list ap) return -1 on failure, not by setting strp to NULL. Replace check for NULL by checking for -1 return. Signed-off-by: Kevin Rocard --- parameter/Element.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/parameter/Element.cpp b/parameter/Element.cpp index 67d711ca3..e401d4ec2 100644 --- a/parameter/Element.cpp +++ b/parameter/Element.cpp @@ -82,7 +82,11 @@ void CElement::log_table(bool bIsWarning, const std::list lstrMessage) c void CElement::vLog(bool bIsWarning, const char *strMessage, va_list args) const { char *pacBuffer; - vasprintf(&pacBuffer, strMessage, args); + if(vasprintf(&pacBuffer, strMessage, args) == -1) { + return doLog(true, std::string() + "Unable to format " + + (bIsWarning ? "warning" : "info log") + + ": " + strMessage); + } doLog(bIsWarning, pacBuffer); free(pacBuffer); }