liblog: cleanup opaque type usage

There's a lot of unnecessary boilerplate around these opaque types,
and this change simplifies it.

Test: liblog-unit-tests
Change-Id: I0c4e133037fd5f04157ac22175181a6a496e18c4
This commit is contained in:
Tom Cherry 2019-11-14 10:39:40 -08:00
parent bace5995f1
commit 828db1a901
5 changed files with 165 additions and 240 deletions

View file

@ -47,9 +47,6 @@ struct android_log_context_internal {
uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD];
};
// TODO(tomcherry): real C++ structs.
typedef struct android_log_context_internal android_log_context_internal;
static void init_context(android_log_context_internal* context, uint32_t tag) {
context->tag = tag;
context->read_write_flag = kAndroidLoggerWrite;
@ -110,11 +107,9 @@ int android_log_destroy(android_log_context* ctx) {
return 0;
}
int android_log_reset(android_log_context ctx) {
android_log_context_internal* context;
int android_log_reset(android_log_context context) {
uint32_t tag;
context = (android_log_context_internal*)ctx;
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -126,10 +121,7 @@ int android_log_reset(android_log_context ctx) {
return 0;
}
int android_log_parser_reset(android_log_context ctx, const char* msg, size_t len) {
android_log_context_internal* context;
context = (android_log_context_internal*)ctx;
int android_log_parser_reset(android_log_context context, const char* msg, size_t len) {
if (!context || (kAndroidLoggerRead != context->read_write_flag)) {
return -EBADF;
}
@ -140,10 +132,7 @@ int android_log_parser_reset(android_log_context ctx, const char* msg, size_t le
return 0;
}
int android_log_write_list_begin(android_log_context ctx) {
android_log_context_internal* context;
context = (android_log_context_internal*)ctx;
int android_log_write_list_begin(android_log_context context) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -174,8 +163,7 @@ int android_log_write_list_begin(android_log_context ctx) {
return 0;
}
int android_log_write_int32(android_log_context ctx, int32_t value) {
android_log_context_internal* context = (android_log_context_internal*)ctx;
int android_log_write_int32(android_log_context context, int32_t value) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -195,8 +183,7 @@ int android_log_write_int32(android_log_context ctx, int32_t value) {
return 0;
}
int android_log_write_int64(android_log_context ctx, int64_t value) {
android_log_context_internal* context = (android_log_context_internal*)ctx;
int android_log_write_int64(android_log_context context, int64_t value) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -216,8 +203,7 @@ int android_log_write_int64(android_log_context ctx, int64_t value) {
return 0;
}
int android_log_write_string8_len(android_log_context ctx, const char* value, size_t maxlen) {
android_log_context_internal* context = (android_log_context_internal*)ctx;
int android_log_write_string8_len(android_log_context context, const char* value, size_t maxlen) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -252,8 +238,7 @@ int android_log_write_string8(android_log_context ctx, const char* value) {
return android_log_write_string8_len(ctx, value, MAX_EVENT_PAYLOAD);
}
int android_log_write_float32(android_log_context ctx, float value) {
android_log_context_internal* context = (android_log_context_internal*)ctx;
int android_log_write_float32(android_log_context context, float value) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -273,10 +258,7 @@ int android_log_write_float32(android_log_context ctx, float value) {
return 0;
}
int android_log_write_list_end(android_log_context ctx) {
android_log_context_internal* context;
context = (android_log_context_internal*)ctx;
int android_log_write_list_end(android_log_context context) {
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -303,8 +285,7 @@ int android_log_write_list_end(android_log_context ctx) {
/*
* Logs the list of elements to the event log.
*/
int android_log_write_list(android_log_context ctx, log_id_t id) {
android_log_context_internal* context;
int android_log_write_list(android_log_context context, log_id_t id) {
const char* msg;
ssize_t len;
@ -312,7 +293,6 @@ int android_log_write_list(android_log_context ctx, log_id_t id) {
return -EINVAL;
}
context = (android_log_context_internal*)ctx;
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -337,12 +317,10 @@ int android_log_write_list(android_log_context ctx, log_id_t id) {
: __android_log_security_bwrite(context->tag, msg, len));
}
int android_log_write_list_buffer(android_log_context ctx, const char** buffer) {
android_log_context_internal* context;
int android_log_write_list_buffer(android_log_context context, const char** buffer) {
const char* msg;
ssize_t len;
context = (android_log_context_internal*)ctx;
if (!context || (kAndroidLoggerWrite != context->read_write_flag)) {
return -EBADF;
}
@ -375,12 +353,10 @@ int android_log_write_list_buffer(android_log_context ctx, const char** buffer)
* this and continues to call this function, the behavior is undefined
* (although it won't crash).
*/
static android_log_list_element android_log_read_next_internal(android_log_context ctx, int peek) {
static android_log_list_element android_log_read_next_internal(android_log_context context,
int peek) {
android_log_list_element elem;
unsigned pos;
android_log_context_internal* context;
context = (android_log_context_internal*)ctx;
memset(&elem, 0, sizeof(elem));

View file

@ -39,47 +39,43 @@
#include "logd_reader.h"
#include "logger.h"
static int logdAvailable(log_id_t LogId);
static int logdVersion(struct android_log_logger* logger,
struct android_log_transport_context* transp);
static int logdRead(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, struct log_msg* log_msg);
static int logdPoll(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp);
static void logdClose(struct android_log_logger_list* logger_list,
static int LogdAvailable(log_id_t LogId);
static int LogdVersion(struct logger* logger, struct android_log_transport_context* transp);
static int LogdRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
struct log_msg* log_msg);
static int LogdPoll(struct logger_list* logger_list, struct android_log_transport_context* transp);
static void LogdClose(struct logger_list* logger_list,
struct android_log_transport_context* transp);
static int logdClear(struct android_log_logger* logger,
struct android_log_transport_context* transp);
static ssize_t logdSetSize(struct android_log_logger* logger,
struct android_log_transport_context* transp, size_t size);
static ssize_t logdGetSize(struct android_log_logger* logger,
struct android_log_transport_context* transp);
static ssize_t logdGetReadableSize(struct android_log_logger* logger,
static int LogdClear(struct logger* logger, struct android_log_transport_context* transp);
static ssize_t LogdSetSize(struct logger* logger, struct android_log_transport_context* transp,
size_t size);
static ssize_t LogdGetSize(struct logger* logger, struct android_log_transport_context* transp);
static ssize_t LogdGetReadableSize(struct logger* logger,
struct android_log_transport_context* transp);
static ssize_t logdGetPrune(struct android_log_logger_list* logger,
static ssize_t LogdGetPrune(struct logger_list* logger,
struct android_log_transport_context* transp, char* buf, size_t len);
static ssize_t logdSetPrune(struct android_log_logger_list* logger,
static ssize_t LogdSetPrune(struct logger_list* logger,
struct android_log_transport_context* transp, char* buf, size_t len);
static ssize_t logdGetStats(struct android_log_logger_list* logger,
static ssize_t LogdGetStats(struct logger_list* logger,
struct android_log_transport_context* transp, char* buf, size_t len);
struct android_log_transport_read logdLoggerRead = {
.name = "logd",
.available = logdAvailable,
.version = logdVersion,
.close = logdClose,
.read = logdRead,
.poll = logdPoll,
.clear = logdClear,
.setSize = logdSetSize,
.getSize = logdGetSize,
.getReadableSize = logdGetReadableSize,
.getPrune = logdGetPrune,
.setPrune = logdSetPrune,
.getStats = logdGetStats,
.available = LogdAvailable,
.version = LogdVersion,
.close = LogdClose,
.read = LogdRead,
.poll = LogdPoll,
.clear = LogdClear,
.setSize = LogdSetSize,
.getSize = LogdGetSize,
.getReadableSize = LogdGetReadableSize,
.getPrune = LogdGetPrune,
.setPrune = LogdSetPrune,
.getStats = LogdGetStats,
};
static int logdAvailable(log_id_t logId) {
static int LogdAvailable(log_id_t logId) {
if (logId >= LOG_ID_MAX) {
return -EINVAL;
}
@ -120,8 +116,7 @@ static int socket_local_client(const std::string& name, int type) {
}
/* worker for sending the command to the logger */
static ssize_t send_log_msg(struct android_log_logger* logger, const char* msg, char* buf,
size_t buf_size) {
static ssize_t send_log_msg(struct logger* logger, const char* msg, char* buf, size_t buf_size) {
ssize_t ret;
size_t len;
char* cp;
@ -202,16 +197,14 @@ static int check_log_success(char* buf, ssize_t ret) {
return 0;
}
static int logdClear(struct android_log_logger* logger,
struct android_log_transport_context* transp __unused) {
static int LogdClear(struct logger* logger, struct android_log_transport_context*) {
char buf[512];
return check_log_success(buf, send_log_msg(logger, "clear %d", buf, sizeof(buf)));
}
/* returns the total size of the log's ring buffer */
static ssize_t logdGetSize(struct android_log_logger* logger,
struct android_log_transport_context* transp __unused) {
static ssize_t LogdGetSize(struct logger* logger, struct android_log_transport_context*) {
char buf[512];
ssize_t ret = send_log_msg(logger, "getLogSize %d", buf, sizeof(buf));
@ -226,8 +219,8 @@ static ssize_t logdGetSize(struct android_log_logger* logger,
return atol(buf);
}
static ssize_t logdSetSize(struct android_log_logger* logger,
struct android_log_transport_context* transp __unused, size_t size) {
static ssize_t LogdSetSize(struct logger* logger, struct android_log_transport_context*,
size_t size) {
char buf[512];
snprintf(buf, sizeof(buf), "setLogSize %d %zu", logger->logId, size);
@ -239,8 +232,7 @@ static ssize_t logdSetSize(struct android_log_logger* logger,
* returns the readable size of the log's ring buffer (that is, amount of the
* log consumed)
*/
static ssize_t logdGetReadableSize(struct android_log_logger* logger,
struct android_log_transport_context* transp __unused) {
static ssize_t LogdGetReadableSize(struct logger* logger, struct android_log_transport_context*) {
char buf[512];
ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf));
@ -258,8 +250,7 @@ static ssize_t logdGetReadableSize(struct android_log_logger* logger,
/*
* returns the logger version
*/
static int logdVersion(struct android_log_logger* logger __unused,
struct android_log_transport_context* transp __unused) {
static int LogdVersion(struct logger*, struct android_log_transport_context*) {
uid_t uid = __android_log_uid();
return ((uid != AID_ROOT) && (uid != AID_LOG) && (uid != AID_SYSTEM)) ? 3 : 4;
}
@ -267,10 +258,9 @@ static int logdVersion(struct android_log_logger* logger __unused,
/*
* returns statistics
*/
static ssize_t logdGetStats(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp __unused, char* buf,
size_t len) {
struct android_log_logger* logger;
static ssize_t LogdGetStats(struct logger_list* logger_list, struct android_log_transport_context*,
char* buf, size_t len) {
struct logger* logger;
char* cp = buf;
size_t remaining = len;
size_t n;
@ -294,14 +284,12 @@ static ssize_t logdGetStats(struct android_log_logger_list* logger_list,
return send_log_msg(NULL, NULL, buf, len);
}
static ssize_t logdGetPrune(struct android_log_logger_list* logger_list __unused,
struct android_log_transport_context* transp __unused, char* buf,
static ssize_t LogdGetPrune(struct logger_list*, struct android_log_transport_context*, char* buf,
size_t len) {
return send_log_msg(NULL, "getPruneList", buf, len);
}
static ssize_t logdSetPrune(struct android_log_logger_list* logger_list __unused,
struct android_log_transport_context* transp __unused, char* buf,
static ssize_t LogdSetPrune(struct logger_list*, struct android_log_transport_context*, char* buf,
size_t len) {
const char cmd[] = "setPruneList ";
const size_t cmdlen = sizeof(cmd) - 1;
@ -316,9 +304,8 @@ static ssize_t logdSetPrune(struct android_log_logger_list* logger_list __unused
return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
}
static int logdOpen(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp) {
struct android_log_logger* logger;
static int logdOpen(struct logger_list* logger_list, struct android_log_transport_context* transp) {
struct logger* logger;
char buffer[256], *cp, c;
int ret, remaining, sock;
@ -404,8 +391,8 @@ static int logdOpen(struct android_log_logger_list* logger_list,
}
/* Read from the selected logs */
static int logdRead(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, struct log_msg* log_msg) {
static int LogdRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
struct log_msg* log_msg) {
int ret = logdOpen(logger_list, transp);
if (ret < 0) {
return ret;
@ -425,8 +412,7 @@ static int logdRead(struct android_log_logger_list* logger_list,
return ret;
}
static int logdPoll(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp) {
static int LogdPoll(struct logger_list* logger_list, struct android_log_transport_context* transp) {
struct pollfd p;
int ret = logdOpen(logger_list, transp);
@ -448,8 +434,7 @@ static int logdPoll(struct android_log_logger_list* logger_list,
}
/* Close all the logs */
static void logdClose(struct android_log_logger_list* logger_list __unused,
struct android_log_transport_context* transp) {
static void LogdClose(struct logger_list*, struct android_log_transport_context* transp) {
int sock = atomic_exchange(&transp->context.sock, -1);
if (sock > 0) {
close(sock);

View file

@ -46,49 +46,37 @@ struct android_log_transport_write {
size_t nr);
};
struct android_log_logger_list;
struct android_log_transport_context;
struct android_log_logger;
struct android_log_transport_read {
const char* name; /* human name to describe the transport */
/* Does not cause resources to be taken */
int (*available)(log_id_t logId);
int (*version)(struct android_log_logger* logger,
struct android_log_transport_context* transp);
int (*version)(struct logger* logger, struct android_log_transport_context* transp);
/* Release resources taken by the following interfaces */
void (*close)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp);
void (*close)(struct logger_list* logger_list, struct android_log_transport_context* transp);
/*
* Expect all to instantiate open automagically on any call,
* so we do not have an explicit open call.
*/
int (*read)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp,
int (*read)(struct logger_list* logger_list, struct android_log_transport_context* transp,
struct log_msg* log_msg);
/* Must only be called if not ANDROID_LOG_NONBLOCK (blocking) */
int (*poll)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp);
int (*poll)(struct logger_list* logger_list, struct android_log_transport_context* transp);
int (*clear)(struct android_log_logger* logger,
struct android_log_transport_context* transp);
ssize_t (*setSize)(struct android_log_logger* logger,
struct android_log_transport_context* transp, size_t size);
ssize_t (*getSize)(struct android_log_logger* logger,
struct android_log_transport_context* transp);
ssize_t (*getReadableSize)(struct android_log_logger* logger,
struct android_log_transport_context* transp);
int (*clear)(struct logger* logger, struct android_log_transport_context* transp);
ssize_t (*setSize)(struct logger* logger, struct android_log_transport_context* transp,
size_t size);
ssize_t (*getSize)(struct logger* logger, struct android_log_transport_context* transp);
ssize_t (*getReadableSize)(struct logger* logger, struct android_log_transport_context* transp);
ssize_t (*getPrune)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, char* buf,
size_t len);
ssize_t (*setPrune)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, char* buf,
size_t len);
ssize_t (*getStats)(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, char* buf,
size_t len);
ssize_t (*getPrune)(struct logger_list* logger_list, struct android_log_transport_context* transp,
char* buf, size_t len);
ssize_t (*setPrune)(struct logger_list* logger_list, struct android_log_transport_context* transp,
char* buf, size_t len);
ssize_t (*getStats)(struct logger_list* logger_list, struct android_log_transport_context* transp,
char* buf, size_t len);
};
struct android_log_transport_context {
@ -98,7 +86,7 @@ struct android_log_transport_context {
unsigned logMask; /* mask of requested log buffers */
};
struct android_log_logger_list {
struct logger_list {
struct listnode logger;
android_log_transport_context transport_context;
bool transport_initialized;
@ -108,22 +96,19 @@ struct android_log_logger_list {
pid_t pid;
};
struct android_log_logger {
struct logger {
struct listnode node;
struct android_log_logger_list* parent;
struct logger_list* parent;
log_id_t logId;
};
/* assumes caller has structures read-locked, single threaded, or fenced */
#define logger_for_each(logp, logger_list) \
for ((logp) = node_to_item((logger_list)->logger.next, \
struct android_log_logger, node); \
((logp) != node_to_item(&(logger_list)->logger, \
struct android_log_logger, node)) && \
((logp)->parent == (logger_list)); \
(logp) = \
node_to_item((logp)->node.next, struct android_log_logger, node))
#define logger_for_each(logp, logger_list) \
for ((logp) = node_to_item((logger_list)->logger.next, struct logger, node); \
((logp) != node_to_item(&(logger_list)->logger, struct logger, node)) && \
((logp)->parent == (logger_list)); \
(logp) = node_to_item((logp)->node.next, struct logger, node))
/* OS specific dribs and drabs */

View file

@ -35,25 +35,23 @@
/* android_logger_alloc unimplemented, no use case */
/* android_logger_free not exported */
static void android_logger_free(struct logger* logger) {
struct android_log_logger* logger_internal = (struct android_log_logger*)logger;
if (!logger_internal) {
if (!logger) {
return;
}
list_remove(&logger_internal->node);
list_remove(&logger->node);
free(logger_internal);
free(logger);
}
/* android_logger_alloc unimplemented, no use case */
/* method for getting the associated sublog id */
log_id_t android_logger_get_id(struct logger* logger) {
return ((struct android_log_logger*)logger)->logId;
return ((struct logger*)logger)->logId;
}
static int init_transport_context(struct android_log_logger_list* logger_list) {
static int init_transport_context(struct logger_list* logger_list) {
if (!logger_list) {
return -EINVAL;
}
@ -73,7 +71,7 @@ static int init_transport_context(struct android_log_logger_list* logger_list) {
struct android_log_transport_read* transport;
transport = (logger_list->mode & ANDROID_LOG_PSTORE) ? &pmsgLoggerRead : &logdLoggerRead;
struct android_log_logger* logger;
struct logger* logger;
unsigned logMask = 0;
logger_for_each(logger, logger_list) {
@ -96,28 +94,26 @@ static int init_transport_context(struct android_log_logger_list* logger_list) {
return 0;
}
#define LOGGER_FUNCTION(logger, def, func, args...) \
ssize_t ret = -EINVAL; \
android_log_logger* logger_internal = reinterpret_cast<android_log_logger*>(logger); \
\
if (!logger_internal) { \
return ret; \
} \
ret = init_transport_context(logger_internal->parent); \
if (ret < 0) { \
return ret; \
} \
\
ret = (def); \
android_log_transport_context* transport_context = &logger_internal->parent->transport_context; \
if (transport_context->logMask & (1 << logger_internal->logId) && \
transport_context->transport && transport_context->transport->func) { \
ssize_t retval = \
(transport_context->transport->func)(logger_internal, transport_context, ##args); \
if (ret >= 0 || ret == (def)) { \
ret = retval; \
} \
} \
#define LOGGER_FUNCTION(logger, def, func, args...) \
ssize_t ret = -EINVAL; \
\
if (!logger) { \
return ret; \
} \
ret = init_transport_context(logger->parent); \
if (ret < 0) { \
return ret; \
} \
\
ret = (def); \
android_log_transport_context* transport_context = &logger->parent->transport_context; \
if (transport_context->logMask & (1 << logger->logId) && transport_context->transport && \
transport_context->transport->func) { \
ssize_t retval = (transport_context->transport->func)(logger, transport_context, ##args); \
if (ret >= 0 || ret == (def)) { \
ret = retval; \
} \
} \
return ret
int android_logger_clear(struct logger* logger) {
@ -148,24 +144,20 @@ int android_logger_get_log_version(struct logger* logger) {
LOGGER_FUNCTION(logger, 4, version);
}
#define LOGGER_LIST_FUNCTION(logger_list, def, func, args...) \
android_log_logger_list* logger_list_internal = \
reinterpret_cast<android_log_logger_list*>(logger_list); \
\
ssize_t ret = init_transport_context(logger_list_internal); \
if (ret < 0) { \
return ret; \
} \
\
ret = (def); \
android_log_transport_context* transport_context = &logger_list_internal->transport_context; \
if (transport_context->transport && transport_context->transport->func) { \
ssize_t retval = \
(transport_context->transport->func)(logger_list_internal, transport_context, ##args); \
if (ret >= 0 || ret == (def)) { \
ret = retval; \
} \
} \
#define LOGGER_LIST_FUNCTION(logger_list, def, func, args...) \
ssize_t ret = init_transport_context(logger_list); \
if (ret < 0) { \
return ret; \
} \
\
ret = (def); \
android_log_transport_context* transport_context = &logger_list->transport_context; \
if (transport_context->transport && transport_context->transport->func) { \
ssize_t retval = (transport_context->transport->func)(logger_list, transport_context, ##args); \
if (ret >= 0 || ret == (def)) { \
ret = retval; \
} \
} \
return ret
/*
@ -184,9 +176,9 @@ int android_logger_set_prune_list(struct logger_list* logger_list, char* buf, si
}
struct logger_list* android_logger_list_alloc(int mode, unsigned int tail, pid_t pid) {
struct android_log_logger_list* logger_list;
struct logger_list* logger_list;
logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
logger_list = static_cast<struct logger_list*>(calloc(1, sizeof(*logger_list)));
if (!logger_list) {
return NULL;
}
@ -196,13 +188,13 @@ struct logger_list* android_logger_list_alloc(int mode, unsigned int tail, pid_t
logger_list->tail = tail;
logger_list->pid = pid;
return (struct logger_list*)logger_list;
return logger_list;
}
struct logger_list* android_logger_list_alloc_time(int mode, log_time start, pid_t pid) {
struct android_log_logger_list* logger_list;
struct logger_list* logger_list;
logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
logger_list = static_cast<struct logger_list*>(calloc(1, sizeof(*logger_list)));
if (!logger_list) {
return NULL;
}
@ -212,7 +204,7 @@ struct logger_list* android_logger_list_alloc_time(int mode, log_time start, pid
logger_list->start = start;
logger_list->pid = pid;
return (struct logger_list*)logger_list;
return logger_list;
}
/* android_logger_list_register unimplemented, no use case */
@ -220,33 +212,31 @@ struct logger_list* android_logger_list_alloc_time(int mode, log_time start, pid
/* Open the named log and add it to the logger list */
struct logger* android_logger_open(struct logger_list* logger_list, log_id_t logId) {
struct android_log_logger_list* logger_list_internal =
(struct android_log_logger_list*)logger_list;
struct android_log_logger* logger;
struct logger* logger;
if (!logger_list_internal || (logId >= LOG_ID_MAX)) {
if (!logger_list || (logId >= LOG_ID_MAX)) {
return nullptr;
}
logger_for_each(logger, logger_list_internal) {
logger_for_each(logger, logger_list) {
if (logger->logId == logId) {
return reinterpret_cast<struct logger*>(logger);
}
}
logger = static_cast<android_log_logger*>(calloc(1, sizeof(*logger)));
logger = static_cast<struct logger*>(calloc(1, sizeof(*logger)));
if (!logger) {
return nullptr;
}
logger->logId = logId;
list_add_tail(&logger_list_internal->logger, &logger->node);
logger->parent = logger_list_internal;
list_add_tail(&logger_list->logger, &logger->node);
logger->parent = logger_list;
// Reset known transport to re-evaluate, since we added a new logger.
logger_list_internal->transport_initialized = false;
logger_list->transport_initialized = false;
return (struct logger*)logger;
return logger;
}
/* Open the single named log and make it part of a new logger list */
@ -267,7 +257,7 @@ struct logger_list* android_logger_list_open(log_id_t logId, int mode, unsigned
}
/* Validate log_msg packet, read function has already been null checked */
static int android_transport_read(struct android_log_logger_list* logger_list,
static int android_transport_read(struct logger_list* logger_list,
struct android_log_transport_context* transp,
struct log_msg* log_msg) {
int ret = (*transp->transport->read)(logger_list, transp, log_msg);
@ -297,38 +287,32 @@ static int android_transport_read(struct android_log_logger_list* logger_list,
/* Read from the selected logs */
int android_logger_list_read(struct logger_list* logger_list, struct log_msg* log_msg) {
struct android_log_logger_list* logger_list_internal =
(struct android_log_logger_list*)logger_list;
int ret = init_transport_context(logger_list_internal);
int ret = init_transport_context(logger_list);
if (ret < 0) {
return ret;
}
android_log_transport_context* transport_context = &logger_list_internal->transport_context;
return android_transport_read(logger_list_internal, transport_context, log_msg);
android_log_transport_context* transport_context = &logger_list->transport_context;
return android_transport_read(logger_list, transport_context, log_msg);
}
/* Close all the logs */
void android_logger_list_free(struct logger_list* logger_list) {
struct android_log_logger_list* logger_list_internal =
(struct android_log_logger_list*)logger_list;
if (logger_list_internal == NULL) {
if (logger_list == NULL) {
return;
}
android_log_transport_context* transport_context = &logger_list_internal->transport_context;
android_log_transport_context* transport_context = &logger_list->transport_context;
if (transport_context->transport && transport_context->transport->close) {
(*transport_context->transport->close)(logger_list_internal, transport_context);
(*transport_context->transport->close)(logger_list, transport_context);
}
while (!list_empty(&logger_list_internal->logger)) {
struct listnode* node = list_head(&logger_list_internal->logger);
struct android_log_logger* logger = node_to_item(node, struct android_log_logger, node);
while (!list_empty(&logger_list->logger)) {
struct listnode* node = list_head(&logger_list->logger);
struct logger* logger = node_to_item(node, struct logger, node);
android_logger_free((struct logger*)logger);
}
free(logger_list_internal);
free(logger_list);
}

View file

@ -26,24 +26,22 @@
#include "logger.h"
static int pmsgAvailable(log_id_t logId);
static int pmsgVersion(struct android_log_logger* logger,
struct android_log_transport_context* transp);
static int pmsgRead(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, struct log_msg* log_msg);
static void pmsgClose(struct android_log_logger_list* logger_list,
static int PmsgAvailable(log_id_t logId);
static int PmsgVersion(struct logger* logger, struct android_log_transport_context* transp);
static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
struct log_msg* log_msg);
static void PmsgClose(struct logger_list* logger_list,
struct android_log_transport_context* transp);
static int pmsgClear(struct android_log_logger* logger,
struct android_log_transport_context* transp);
static int PmsgClear(struct logger* logger, struct android_log_transport_context* transp);
struct android_log_transport_read pmsgLoggerRead = {
.name = "pmsg",
.available = pmsgAvailable,
.version = pmsgVersion,
.close = pmsgClose,
.read = pmsgRead,
.available = PmsgAvailable,
.version = PmsgVersion,
.close = PmsgClose,
.read = PmsgRead,
.poll = NULL,
.clear = pmsgClear,
.clear = PmsgClear,
.setSize = NULL,
.getSize = NULL,
.getReadableSize = NULL,
@ -52,7 +50,7 @@ struct android_log_transport_read pmsgLoggerRead = {
.getStats = NULL,
};
static int pmsgAvailable(log_id_t logId) {
static int PmsgAvailable(log_id_t logId) {
if (logId > LOG_ID_SECURITY) {
return -EINVAL;
}
@ -62,21 +60,19 @@ static int pmsgAvailable(log_id_t logId) {
return -EBADF;
}
static int pmsgClear(struct android_log_logger* logger __unused,
struct android_log_transport_context* transp __unused) {
static int PmsgClear(struct logger*, struct android_log_transport_context*) {
return unlink("/sys/fs/pstore/pmsg-ramoops-0");
}
/*
* returns the logger version
*/
static int pmsgVersion(struct android_log_logger* logger __unused,
struct android_log_transport_context* transp __unused) {
static int PmsgVersion(struct logger*, struct android_log_transport_context*) {
return 4;
}
static int pmsgRead(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, struct log_msg* log_msg) {
static int PmsgRead(struct logger_list* logger_list, struct android_log_transport_context* transp,
struct log_msg* log_msg) {
ssize_t ret;
off_t current, next;
struct __attribute__((__packed__)) {
@ -192,8 +188,7 @@ static int pmsgRead(struct android_log_logger_list* logger_list,
}
}
static void pmsgClose(struct android_log_logger_list* logger_list __unused,
struct android_log_transport_context* transp) {
static void PmsgClose(struct logger_list*, struct android_log_transport_context* transp) {
int fd = atomic_exchange(&transp->context.fd, 0);
if (fd > 0) {
close(fd);
@ -211,7 +206,7 @@ static void* realloc_or_free(void* ptr, size_t new_size) {
ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* prefix,
__android_log_pmsg_file_read_fn fn, void* arg) {
ssize_t ret;
struct android_log_logger_list logger_list;
struct logger_list logger_list;
struct android_log_transport_context transp;
struct content {
struct listnode node;
@ -268,7 +263,7 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref
/* Read the file content */
log_msg log_msg;
while (pmsgRead(&logger_list, &transp, &log_msg) > 0) {
while (PmsgRead(&logger_list, &transp, &log_msg) > 0) {
const char* cp;
size_t hdr_size = log_msg.entry.hdr_size;
@ -426,7 +421,7 @@ ssize_t __android_log_pmsg_file_read(log_id_t logId, char prio, const char* pref
}
list_add_head(node, &content->node);
}
pmsgClose(&logger_list, &transp);
PmsgClose(&logger_list, &transp);
/* Progress through all the collected files */
list_for_each_safe(node, n, &name_list) {