Merge "logd: switch asprintf to std::string"

This commit is contained in:
Mark Salyzyn 2015-08-21 16:47:20 +00:00 committed by Gerrit Code Review
commit 8a4beeb694
7 changed files with 41 additions and 93 deletions

View file

@ -25,6 +25,9 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <string>
#include <base/stringprintf.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
#include <sysutils/SocketClient.h> #include <sysutils/SocketClient.h>
@ -189,22 +192,13 @@ CommandListener::GetStatisticsCmd::GetStatisticsCmd(LogBuffer *buf) :
mBuf(*buf) { mBuf(*buf) {
} }
static void package_string(char **strp) { static std::string package_string(const std::string &str) {
const char *a = *strp;
if (!a) {
a = "";
}
// Calculate total buffer size prefix, count is the string length w/o nul // Calculate total buffer size prefix, count is the string length w/o nul
char fmt[32]; char fmt[32];
for(size_t l = strlen(a), y = 0, x = 6; y != x; y = x, x = strlen(fmt) - 2) { for(size_t l = str.length(), y = 0, x = 6; y != x; y = x, x = strlen(fmt) - 2) {
snprintf(fmt, sizeof(fmt), "%zu\n%%s\n\f", l + x); snprintf(fmt, sizeof(fmt), "%zu\n%%s\n\f", l + x);
} }
return android::base::StringPrintf(fmt, str.c_str());
char *b = *strp;
*strp = NULL;
asprintf(strp, fmt, a);
free(b);
} }
int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli, int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli,
@ -228,16 +222,7 @@ int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli,
} }
} }
char *buf = NULL; cli->sendMsg(package_string(mBuf.formatStatistics(uid, logMask)).c_str());
mBuf.formatStatistics(&buf, uid, logMask);
if (!buf) {
cli->sendMsg("Failed");
} else {
package_string(&buf);
cli->sendMsg(buf);
free(buf);
}
return 0; return 0;
} }
@ -249,15 +234,7 @@ CommandListener::GetPruneListCmd::GetPruneListCmd(LogBuffer *buf) :
int CommandListener::GetPruneListCmd::runCommand(SocketClient *cli, int CommandListener::GetPruneListCmd::runCommand(SocketClient *cli,
int /*argc*/, char ** /*argv*/) { int /*argc*/, char ** /*argv*/) {
setname(); setname();
char *buf = NULL; cli->sendMsg(package_string(mBuf.formatPrune()).c_str());
mBuf.formatPrune(&buf);
if (!buf) {
cli->sendMsg("Failed");
} else {
package_string(&buf);
cli->sendMsg(buf);
free(buf);
}
return 0; return 0;
} }
@ -274,20 +251,15 @@ int CommandListener::SetPruneListCmd::runCommand(SocketClient *cli,
return 0; return 0;
} }
char *cp = NULL; std::string str;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
char *p = cp; if (str.length()) {
if (p) { str += " ";
cp = NULL;
asprintf(&cp, "%s %s", p, argv[i]);
free(p);
} else {
asprintf(&cp, "%s", argv[i]);
} }
str += argv[i];
} }
int ret = mBuf.initPrune(cp); int ret = mBuf.initPrune(str.c_str());
free(cp);
if (ret) { if (ret) {
cli->sendMsg("Invalid"); cli->sendMsg("Invalid");

View file

@ -690,10 +690,12 @@ uint64_t LogBuffer::flushTo(
return max; return max;
} }
void LogBuffer::formatStatistics(char **strp, uid_t uid, unsigned int logMask) { std::string LogBuffer::formatStatistics(uid_t uid, unsigned int logMask) {
pthread_mutex_lock(&mLogElementsLock); pthread_mutex_lock(&mLogElementsLock);
stats.format(strp, uid, logMask); std::string ret = stats.format(uid, logMask);
pthread_mutex_unlock(&mLogElementsLock); pthread_mutex_unlock(&mLogElementsLock);
return ret;
} }

View file

@ -20,6 +20,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <list> #include <list>
#include <string>
#include <log/log.h> #include <log/log.h>
#include <sysutils/SocketClient.h> #include <sysutils/SocketClient.h>
@ -67,15 +68,14 @@ public:
int setSize(log_id_t id, unsigned long size); int setSize(log_id_t id, unsigned long size);
unsigned long getSizeUsed(log_id_t id); unsigned long getSizeUsed(log_id_t id);
// *strp uses malloc, use free to release. // *strp uses malloc, use free to release.
void formatStatistics(char **strp, uid_t uid, unsigned int logMask); std::string formatStatistics(uid_t uid, unsigned int logMask);
void enableStatistics() { void enableStatistics() {
stats.enableStatistics(); stats.enableStatistics();
} }
int initPrune(char *cp) { return mPrune.init(cp); } int initPrune(const char *cp) { return mPrune.init(cp); }
// *strp uses malloc, use free to release. std::string formatPrune() { return mPrune.format(); }
void formatPrune(char **strp) { mPrune.format(strp); }
// helper must be protected directly or implicitly by lock()/unlock() // helper must be protected directly or implicitly by lock()/unlock()
char *pidToName(pid_t pid) { return stats.pidToName(pid); } char *pidToName(pid_t pid) { return stats.pidToName(pid); }

View file

@ -20,8 +20,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <string>
#include <base/stringprintf.h> #include <base/stringprintf.h>
#include <log/logger.h> #include <log/logger.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
@ -206,14 +204,9 @@ static std::string format_line(
} }
} }
void LogStatistics::format(char **buf, uid_t uid, unsigned int logMask) { std::string LogStatistics::format(uid_t uid, unsigned int logMask) {
static const unsigned short spaces_total = 19; static const unsigned short spaces_total = 19;
if (*buf) {
free(*buf);
*buf = NULL;
}
// Report on total logging, current and for all time // Report on total logging, current and for all time
std::string output = "size/num"; std::string output = "size/num";
@ -514,7 +507,7 @@ void LogStatistics::format(char **buf, uid_t uid, unsigned int logMask) {
} }
} }
*buf = strdup(output.c_str()); return output;
} }
namespace android { namespace android {

View file

@ -331,8 +331,7 @@ public:
size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; } size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; }
size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; } size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; }
// *strp = malloc, balance with free std::string format(uid_t uid, unsigned int logMask);
void format(char **strp, uid_t uid, unsigned int logMask);
// helper (must be locked directly or implicitly by mLogElementsLock) // helper (must be locked directly or implicitly by mLogElementsLock)
char *pidToName(pid_t pid); char *pidToName(pid_t pid);

View file

@ -16,8 +16,6 @@
#include <ctype.h> #include <ctype.h>
#include <string>
#include <base/stringprintf.h> #include <base/stringprintf.h>
#include "LogWhiteBlackList.h" #include "LogWhiteBlackList.h"
@ -37,18 +35,18 @@ int Prune::cmp(uid_t uid, pid_t pid) const {
return uid - mUid; return uid - mUid;
} }
void Prune::format(char **strp) { std::string Prune::format() {
if (mUid != uid_all) { if (mUid != uid_all) {
if (mPid != pid_all) { if (mPid != pid_all) {
asprintf(strp, "%u/%u", mUid, mPid); return android::base::StringPrintf("%u/%u", mUid, mPid);
} else {
asprintf(strp, "%u", mUid);
} }
} else if (mPid != pid_all) { return android::base::StringPrintf("%u", mUid);
asprintf(strp, "/%u", mPid);
} else { // NB: mPid == pid_all can not happen if mUid == uid_all
asprintf(strp, "/");
} }
if (mPid != pid_all) {
return android::base::StringPrintf("/%u", mPid);
}
// NB: mPid == pid_all can not happen if mUid == uid_all
return std::string("/");
} }
PruneList::PruneList() : mWorstUidEnabled(true) { PruneList::PruneList() : mWorstUidEnabled(true) {
@ -64,7 +62,7 @@ PruneList::~PruneList() {
} }
} }
int PruneList::init(char *str) { int PruneList::init(const char *str) {
mWorstUidEnabled = true; mWorstUidEnabled = true;
PruneCollection::iterator it; PruneCollection::iterator it;
for (it = mNice.begin(); it != mNice.end();) { for (it = mNice.begin(); it != mNice.end();) {
@ -169,12 +167,7 @@ int PruneList::init(char *str) {
return 0; return 0;
} }
void PruneList::format(char **strp) { std::string PruneList::format() {
if (*strp) {
free(*strp);
*strp = NULL;
}
static const char nice_format[] = " %s"; static const char nice_format[] = " %s";
const char *fmt = nice_format + 1; const char *fmt = nice_format + 1;
@ -188,28 +181,18 @@ void PruneList::format(char **strp) {
PruneCollection::iterator it; PruneCollection::iterator it;
for (it = mNice.begin(); it != mNice.end(); ++it) { for (it = mNice.begin(); it != mNice.end(); ++it) {
char *a = NULL; string += android::base::StringPrintf(fmt, (*it).format().c_str());
(*it).format(&a);
string += android::base::StringPrintf(fmt, a);
fmt = nice_format; fmt = nice_format;
free(a);
} }
static const char naughty_format[] = " ~%s"; static const char naughty_format[] = " ~%s";
fmt = naughty_format + (*fmt != ' '); fmt = naughty_format + (*fmt != ' ');
for (it = mNaughty.begin(); it != mNaughty.end(); ++it) { for (it = mNaughty.begin(); it != mNaughty.end(); ++it) {
char *a = NULL; string += android::base::StringPrintf(fmt, (*it).format().c_str());
(*it).format(&a);
string += android::base::StringPrintf(fmt, a);
fmt = naughty_format; fmt = naughty_format;
free(a);
} }
*strp = strdup(string.c_str()); return string;
} }
// ToDo: Lists are in sorted order, Prune->cmp() returns + or - // ToDo: Lists are in sorted order, Prune->cmp() returns + or -

View file

@ -20,6 +20,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <list> #include <list>
#include <string.h>
#include <LogBufferElement.h> #include <LogBufferElement.h>
@ -43,8 +44,7 @@ public:
int cmp(LogBufferElement *e) const { return cmp(e->getUid(), e->getPid()); } int cmp(LogBufferElement *e) const { return cmp(e->getUid(), e->getPid()); }
// *strp is malloc'd, use free to release std::string format();
void format(char **strp);
}; };
typedef std::list<Prune> PruneCollection; typedef std::list<Prune> PruneCollection;
@ -58,7 +58,7 @@ public:
PruneList(); PruneList();
~PruneList(); ~PruneList();
int init(char *str); int init(const char *str);
bool naughty(LogBufferElement *element); bool naughty(LogBufferElement *element);
bool naughty(void) { return !mNaughty.empty(); } bool naughty(void) { return !mNaughty.empty(); }
@ -66,8 +66,7 @@ public:
bool nice(void) { return !mNice.empty(); } bool nice(void) { return !mNice.empty(); }
bool worstUidEnabled() const { return mWorstUidEnabled; } bool worstUidEnabled() const { return mWorstUidEnabled; }
// *strp is malloc'd, use free to release std::string format();
void format(char **strp);
}; };
#endif // _LOGD_LOG_WHITE_BLACK_LIST_H__ #endif // _LOGD_LOG_WHITE_BLACK_LIST_H__