logd: switch asprintf to std::string
(cherry pick from commit 73160acc5c
)
Bug: 23350706
Change-Id: I715cdd4563a09de3680081947a3439f0cac623be
This commit is contained in:
parent
8a7f778c01
commit
5c3835d943
7 changed files with 41 additions and 93 deletions
|
@ -25,6 +25,9 @@
|
|||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <base/stringprintf.h>
|
||||
#include <cutils/sockets.h>
|
||||
#include <private/android_filesystem_config.h>
|
||||
#include <sysutils/SocketClient.h>
|
||||
|
@ -189,22 +192,13 @@ CommandListener::GetStatisticsCmd::GetStatisticsCmd(LogBuffer *buf) :
|
|||
mBuf(*buf) {
|
||||
}
|
||||
|
||||
static void package_string(char **strp) {
|
||||
const char *a = *strp;
|
||||
if (!a) {
|
||||
a = "";
|
||||
}
|
||||
|
||||
static std::string package_string(const std::string &str) {
|
||||
// Calculate total buffer size prefix, count is the string length w/o nul
|
||||
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);
|
||||
}
|
||||
|
||||
char *b = *strp;
|
||||
*strp = NULL;
|
||||
asprintf(strp, fmt, a);
|
||||
free(b);
|
||||
return android::base::StringPrintf(fmt, str.c_str());
|
||||
}
|
||||
|
||||
int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli,
|
||||
|
@ -228,16 +222,7 @@ int CommandListener::GetStatisticsCmd::runCommand(SocketClient *cli,
|
|||
}
|
||||
}
|
||||
|
||||
char *buf = NULL;
|
||||
|
||||
mBuf.formatStatistics(&buf, uid, logMask);
|
||||
if (!buf) {
|
||||
cli->sendMsg("Failed");
|
||||
} else {
|
||||
package_string(&buf);
|
||||
cli->sendMsg(buf);
|
||||
free(buf);
|
||||
}
|
||||
cli->sendMsg(package_string(mBuf.formatStatistics(uid, logMask)).c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -249,15 +234,7 @@ CommandListener::GetPruneListCmd::GetPruneListCmd(LogBuffer *buf) :
|
|||
int CommandListener::GetPruneListCmd::runCommand(SocketClient *cli,
|
||||
int /*argc*/, char ** /*argv*/) {
|
||||
setname();
|
||||
char *buf = NULL;
|
||||
mBuf.formatPrune(&buf);
|
||||
if (!buf) {
|
||||
cli->sendMsg("Failed");
|
||||
} else {
|
||||
package_string(&buf);
|
||||
cli->sendMsg(buf);
|
||||
free(buf);
|
||||
}
|
||||
cli->sendMsg(package_string(mBuf.formatPrune()).c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -274,20 +251,15 @@ int CommandListener::SetPruneListCmd::runCommand(SocketClient *cli,
|
|||
return 0;
|
||||
}
|
||||
|
||||
char *cp = NULL;
|
||||
std::string str;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
char *p = cp;
|
||||
if (p) {
|
||||
cp = NULL;
|
||||
asprintf(&cp, "%s %s", p, argv[i]);
|
||||
free(p);
|
||||
} else {
|
||||
asprintf(&cp, "%s", argv[i]);
|
||||
if (str.length()) {
|
||||
str += " ";
|
||||
}
|
||||
str += argv[i];
|
||||
}
|
||||
|
||||
int ret = mBuf.initPrune(cp);
|
||||
free(cp);
|
||||
int ret = mBuf.initPrune(str.c_str());
|
||||
|
||||
if (ret) {
|
||||
cli->sendMsg("Invalid");
|
||||
|
|
|
@ -749,10 +749,12 @@ uint64_t LogBuffer::flushTo(
|
|||
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);
|
||||
|
||||
stats.format(strp, uid, logMask);
|
||||
std::string ret = stats.format(uid, logMask);
|
||||
|
||||
pthread_mutex_unlock(&mLogElementsLock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include <log/log.h>
|
||||
#include <sysutils/SocketClient.h>
|
||||
|
@ -67,15 +68,14 @@ public:
|
|||
int setSize(log_id_t id, unsigned long size);
|
||||
unsigned long getSizeUsed(log_id_t id);
|
||||
// *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() {
|
||||
stats.enableStatistics();
|
||||
}
|
||||
|
||||
int initPrune(char *cp) { return mPrune.init(cp); }
|
||||
// *strp uses malloc, use free to release.
|
||||
void formatPrune(char **strp) { mPrune.format(strp); }
|
||||
int initPrune(const char *cp) { return mPrune.init(cp); }
|
||||
std::string formatPrune() { return mPrune.format(); }
|
||||
|
||||
// helper must be protected directly or implicitly by lock()/unlock()
|
||||
char *pidToName(pid_t pid) { return stats.pidToName(pid); }
|
||||
|
|
|
@ -20,8 +20,6 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <base/stringprintf.h>
|
||||
#include <log/logger.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;
|
||||
|
||||
if (*buf) {
|
||||
free(*buf);
|
||||
*buf = NULL;
|
||||
}
|
||||
|
||||
// Report on total logging, current and for all time
|
||||
|
||||
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 {
|
||||
|
|
|
@ -331,8 +331,7 @@ public:
|
|||
size_t sizesTotal(log_id_t id) const { return mSizesTotal[id]; }
|
||||
size_t elementsTotal(log_id_t id) const { return mElementsTotal[id]; }
|
||||
|
||||
// *strp = malloc, balance with free
|
||||
void format(char **strp, uid_t uid, unsigned int logMask);
|
||||
std::string format(uid_t uid, unsigned int logMask);
|
||||
|
||||
// helper (must be locked directly or implicitly by mLogElementsLock)
|
||||
char *pidToName(pid_t pid);
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
#include <ctype.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <base/stringprintf.h>
|
||||
|
||||
#include "LogWhiteBlackList.h"
|
||||
|
@ -37,18 +35,18 @@ int Prune::cmp(uid_t uid, pid_t pid) const {
|
|||
return uid - mUid;
|
||||
}
|
||||
|
||||
void Prune::format(char **strp) {
|
||||
std::string Prune::format() {
|
||||
if (mUid != uid_all) {
|
||||
if (mPid != pid_all) {
|
||||
asprintf(strp, "%u/%u", mUid, mPid);
|
||||
} else {
|
||||
asprintf(strp, "%u", mUid);
|
||||
return android::base::StringPrintf("%u/%u", mUid, mPid);
|
||||
}
|
||||
} else if (mPid != pid_all) {
|
||||
asprintf(strp, "/%u", mPid);
|
||||
} else { // NB: mPid == pid_all can not happen if mUid == uid_all
|
||||
asprintf(strp, "/");
|
||||
return android::base::StringPrintf("%u", mUid);
|
||||
}
|
||||
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) {
|
||||
|
@ -64,7 +62,7 @@ PruneList::~PruneList() {
|
|||
}
|
||||
}
|
||||
|
||||
int PruneList::init(char *str) {
|
||||
int PruneList::init(const char *str) {
|
||||
mWorstUidEnabled = true;
|
||||
PruneCollection::iterator it;
|
||||
for (it = mNice.begin(); it != mNice.end();) {
|
||||
|
@ -169,12 +167,7 @@ int PruneList::init(char *str) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void PruneList::format(char **strp) {
|
||||
if (*strp) {
|
||||
free(*strp);
|
||||
*strp = NULL;
|
||||
}
|
||||
|
||||
std::string PruneList::format() {
|
||||
static const char nice_format[] = " %s";
|
||||
const char *fmt = nice_format + 1;
|
||||
|
||||
|
@ -188,28 +181,18 @@ void PruneList::format(char **strp) {
|
|||
PruneCollection::iterator it;
|
||||
|
||||
for (it = mNice.begin(); it != mNice.end(); ++it) {
|
||||
char *a = NULL;
|
||||
(*it).format(&a);
|
||||
|
||||
string += android::base::StringPrintf(fmt, a);
|
||||
string += android::base::StringPrintf(fmt, (*it).format().c_str());
|
||||
fmt = nice_format;
|
||||
|
||||
free(a);
|
||||
}
|
||||
|
||||
static const char naughty_format[] = " ~%s";
|
||||
fmt = naughty_format + (*fmt != ' ');
|
||||
for (it = mNaughty.begin(); it != mNaughty.end(); ++it) {
|
||||
char *a = NULL;
|
||||
(*it).format(&a);
|
||||
|
||||
string += android::base::StringPrintf(fmt, a);
|
||||
string += android::base::StringPrintf(fmt, (*it).format().c_str());
|
||||
fmt = naughty_format;
|
||||
|
||||
free(a);
|
||||
}
|
||||
|
||||
*strp = strdup(string.c_str());
|
||||
return string;
|
||||
}
|
||||
|
||||
// ToDo: Lists are in sorted order, Prune->cmp() returns + or -
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
#include <list>
|
||||
#include <string.h>
|
||||
|
||||
#include <LogBufferElement.h>
|
||||
|
||||
|
@ -43,8 +44,7 @@ public:
|
|||
|
||||
int cmp(LogBufferElement *e) const { return cmp(e->getUid(), e->getPid()); }
|
||||
|
||||
// *strp is malloc'd, use free to release
|
||||
void format(char **strp);
|
||||
std::string format();
|
||||
};
|
||||
|
||||
typedef std::list<Prune> PruneCollection;
|
||||
|
@ -58,7 +58,7 @@ public:
|
|||
PruneList();
|
||||
~PruneList();
|
||||
|
||||
int init(char *str);
|
||||
int init(const char *str);
|
||||
|
||||
bool naughty(LogBufferElement *element);
|
||||
bool naughty(void) { return !mNaughty.empty(); }
|
||||
|
@ -66,8 +66,7 @@ public:
|
|||
bool nice(void) { return !mNice.empty(); }
|
||||
bool worstUidEnabled() const { return mWorstUidEnabled; }
|
||||
|
||||
// *strp is malloc'd, use free to release
|
||||
void format(char **strp);
|
||||
std::string format();
|
||||
};
|
||||
|
||||
#endif // _LOGD_LOG_WHITE_BLACK_LIST_H__
|
||||
|
|
Loading…
Reference in a new issue