Merge changes I9494ce71,Idc14e4ad,I30de692e,Ieed3223b,I63d0667a am: ee57bbd9f8 am: 2a03cd7158

am: b69d774a0e

Change-Id: Ie506dc28fb1acd56db8d7c15f2862993a65f1b6c
This commit is contained in:
Mark Salyzyn 2017-02-21 18:20:32 +00:00 committed by android-build-merger
commit ba9ef91f8f
9 changed files with 1589 additions and 734 deletions

11
logcat/.clang-format Normal file
View File

@ -0,0 +1,11 @@
BasedOnStyle: Google
AllowShortFunctionsOnASingleLine: false
CommentPragmas: NOLINT:.*
DerivePointerAlignment: false
IndentWidth: 4
PointerAlignment: Left
TabWidth: 4
PenaltyExcessCharacter: 32
Cpp11BracedListStyle: false

View File

@ -1,20 +1,32 @@
# Copyright 2006-2014 The Android Open Source Project
LOCAL_PATH:= $(call my-dir)
LOCAL_PATH := $(call my-dir)
logcatLibs := liblog libbase libcutils libpcrecpp
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= logcat.cpp event.logtags
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils libpcrecpp
LOCAL_MODULE := logcat
LOCAL_SRC_FILES := logcat_main.cpp event.logtags
LOCAL_SHARED_LIBRARIES := liblogcat $(logcatLibs)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE := liblogcat
LOCAL_SRC_FILES := logcat.cpp logcat_system.cpp
LOCAL_SHARED_LIBRARIES := $(logcatLibs)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDES_DIR := $(LOCAL_PATH)/include
LOCAL_CFLAGS := -Werror
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := logpersist.start
LOCAL_MODULE_TAGS := debug
LOCAL_MODULE_CLASS := EXECUTABLES

122
logcat/include/log/logcat.h Normal file
View File

@ -0,0 +1,122 @@
/*
* Copyright (C) 2005-2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBS_LOGCAT_H /* header boilerplate */
#define _LIBS_LOGCAT_H
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
#ifndef __ANDROID_API__
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
#elif __ANDROID_API__ > 24 /* > Nougat */
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
#else
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 0
#endif
#endif
#if __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
/* For managing an in-process logcat function, rather than forking/execing
*
* It also serves as the basis for the logcat command.
*
* The following C API allows a logcat instance to be created, run
* to completion, and then release all the associated resources.
*/
/*
* The opaque context
*/
#ifndef __android_logcat_context_defined /* typedef boilerplate */
#define __android_logcat_context_defined
typedef struct android_logcat_context_internal* android_logcat_context;
#endif
/* Creates a context associated with this logcat instance
*
* Returns a pointer to the context, or a NULL on error.
*/
android_logcat_context create_android_logcat();
/* Collects and outputs the logcat data to output and error file descriptors
*
* Will block, performed in-thread and in-process
*
* The output file descriptor variable, if greater than or equal to 0, is
* where the output (ie: stdout) will be sent. The file descriptor is closed
* on android_logcat_destroy which terminates the instance, or when an -f flag
* (output redirect to a file) is present in the command. The error file
* descriptor variable, if greater than or equal to 0, is where the error
* stream (ie: stderr) will be sent, also closed on android_logcat_destroy.
* The error file descriptor can be set to equal to the output file descriptor,
* which will mix output and error stream content, and will defer closure of
* the file descriptor on -f flag redirection. Negative values for the file
* descriptors will use stdout and stderr FILE references respectively
* internally, and will not close the references as noted above.
*
* Return value is 0 for success, non-zero for errors.
*/
int android_logcat_run_command(android_logcat_context ctx, int output, int error,
int argc, char* const* argv, char* const* envp);
/* Will not block, performed in-process
*
* Starts a thread, opens a pipe, returns reading end fd, saves off argv.
* The command supports 2>&1 (mix content) and 2>/dev/null (drop content) for
* scripted error (stderr) redirection.
*/
int android_logcat_run_command_thread(android_logcat_context ctx, int argc,
char* const* argv, char* const* envp);
int android_logcat_run_command_thread_running(android_logcat_context ctx);
/* Finished with context
*
* Kill the command thread ASAP (if any), and free up all associated resources.
*
* Return value is the result of the android_logcat_run_command, or
* non-zero for any errors.
*/
int android_logcat_destroy(android_logcat_context* ctx);
/* derived helpers */
/*
* In-process thread that acts like somewhat like libc-like system and popen
* respectively. Can not handle shell scripting, only pure calls to the
* logcat operations. The android_logcat_system is a wrapper for the
* create_android_logcat, android_logcat_run_command and android_logcat_destroy
* API above. The android_logcat_popen is a wrapper for the
* android_logcat_run_command_thread API above. The android_logcat_pclose is
* a wrapper for a reasonable wait until output has subsided for command
* completion, fclose on the FILE pointer and the android_logcat_destroy API.
*/
int android_logcat_system(const char* command);
FILE* android_logcat_popen(android_logcat_context* ctx, const char* command);
int android_logcat_pclose(android_logcat_context* ctx, FILE* output);
#endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */
#ifdef __cplusplus
}
#endif
#endif /* _LIBS_LOGCAT_H */

File diff suppressed because it is too large Load Diff

30
logcat/logcat_main.cpp Normal file
View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <signal.h>
#include <stdlib.h>
#include <log/logcat.h>
int main(int argc, char** argv, char** envp) {
android_logcat_context ctx = create_android_logcat();
if (!ctx) return -1;
signal(SIGPIPE, exit);
int retval = android_logcat_run_command(ctx, -1, -1, argc, argv, envp);
int ret = android_logcat_destroy(&ctx);
if (!ret) ret = retval;
return ret;
}

148
logcat/logcat_system.cpp Normal file
View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <log/logcat.h>
static std::string unquote(const char*& cp, const char*& delim) {
if ((*cp == '\'') || (*cp == '"')) {
// KISS: Simple quotes. Do not handle the case
// of concatenation like "blah"foo'bar'
char quote = *cp++;
delim = strchr(cp, quote);
if (!delim) delim = cp + strlen(cp);
std::string str(cp, delim);
if (*delim) ++delim;
return str;
}
delim = strpbrk(cp, " \t\f\r\n");
if (!delim) delim = cp + strlen(cp);
return std::string(cp, delim);
}
static bool __android_logcat_parse(const char* command,
std::vector<std::string>& args,
std::vector<std::string>& envs) {
for (const char *delim, *cp = command; cp && *cp; cp = delim) {
while (isspace(*cp)) ++cp;
if ((args.size() == 0) && (*cp != '=') && !isdigit(*cp)) {
const char* env = cp;
while (isalnum(*cp) || (*cp == '_')) ++cp;
if (cp && (*cp == '=')) {
std::string str(env, ++cp);
str += unquote(cp, delim);
envs.push_back(str);
continue;
}
cp = env;
}
args.push_back(unquote(cp, delim));
if ((args.size() == 1) && (args[0] != "logcat") &&
(args[0] != "/system/bin/logcat")) {
return false;
}
}
return args.size() != 0;
}
FILE* android_logcat_popen(android_logcat_context* ctx, const char* command) {
*ctx = NULL;
std::vector<std::string> args;
std::vector<std::string> envs;
if (!__android_logcat_parse(command, args, envs)) return NULL;
std::vector<const char*> argv;
for (auto& str : args) {
argv.push_back(str.c_str());
}
argv.push_back(NULL);
std::vector<const char*> envp;
for (auto& str : envs) {
envp.push_back(str.c_str());
}
envp.push_back(NULL);
*ctx = create_android_logcat();
if (!*ctx) return NULL;
int fd = android_logcat_run_command_thread(
*ctx, argv.size() - 1, (char* const*)&argv[0], (char* const*)&envp[0]);
argv.clear();
args.clear();
envp.clear();
envs.clear();
if (fd < 0) {
android_logcat_destroy(ctx);
return NULL;
}
FILE* retval = fdopen(fd, "reb");
if (!retval) android_logcat_destroy(ctx);
return retval;
}
int android_logcat_pclose(android_logcat_context* ctx, FILE* output) {
if (*ctx) {
static const useconds_t wait_sample = 20000;
// Wait two seconds maximum
for (size_t retry = ((2 * 1000000) + wait_sample - 1) / wait_sample;
android_logcat_run_command_thread_running(*ctx) && retry; --retry) {
usleep(wait_sample);
}
}
if (output) fclose(output);
return android_logcat_destroy(ctx);
}
int android_logcat_system(const char* command) {
std::vector<std::string> args;
std::vector<std::string> envs;
if (!__android_logcat_parse(command, args, envs)) return -1;
std::vector<const char*> argv;
for (auto& str : args) {
argv.push_back(str.c_str());
}
argv.push_back(NULL);
std::vector<const char*> envp;
for (auto& str : envs) {
envp.push_back(str.c_str());
}
envp.push_back(NULL);
android_logcat_context ctx = create_android_logcat();
if (!ctx) return -1;
/* Command return value */
int retval = android_logcat_run_command(ctx, -1, -1, argv.size() - 1,
(char* const*)&argv[0],
(char* const*)&envp[0]);
/* destroy return value */
int ret = android_logcat_destroy(&ctx);
/* Paranoia merging any discrepancies between the two return values */
if (!ret) ret = retval;
return ret;
}

View File

@ -48,6 +48,7 @@ include $(BUILD_NATIVE_TEST)
test_src_files := \
logcat_test.cpp \
liblogcat_test.cpp \
# Build tests for the device (with .so). Run with:
# adb shell /data/nativetest/logcat-unit-tests/logcat-unit-tests
@ -55,6 +56,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := $(test_module_prefix)unit-tests
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_CFLAGS += $(test_c_flags)
LOCAL_SHARED_LIBRARIES := liblog libbase
LOCAL_SHARED_LIBRARIES := liblog libbase liblogcat
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log/logcat.h>
#define logcat_define(context) android_logcat_context context
#define logcat_popen(context, command) android_logcat_popen(&context, command)
#define logcat_pclose(context, fp) android_logcat_pclose(&context, fp)
#define logcat_system(command) android_logcat_system(command)
#define logcat liblogcat
#include "logcat_test.cpp"

File diff suppressed because it is too large Load Diff