Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LIBS_LOGCAT_H /* header boilerplate */ |
| 18 | #define _LIBS_LOGCAT_H |
| 19 | |
Mark Salyzyn | 6dabc81 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | #ifndef __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE |
| 27 | #ifndef __ANDROID_API__ |
| 28 | #define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1 |
| 29 | #elif __ANDROID_API__ > 24 /* > Nougat */ |
| 30 | #define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1 |
| 31 | #else |
| 32 | #define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 0 |
| 33 | #endif |
| 34 | #endif |
| 35 | |
| 36 | #if __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE |
| 37 | |
| 38 | /* For managing an in-process logcat function, rather than forking/execing |
| 39 | * |
| 40 | * It also serves as the basis for the logcat command. |
| 41 | * |
| 42 | * The following C API allows a logcat instance to be created, run |
| 43 | * to completion, and then release all the associated resources. |
| 44 | */ |
| 45 | |
| 46 | /* |
| 47 | * The opaque context |
| 48 | */ |
| 49 | #ifndef __android_logcat_context_defined /* typedef boilerplate */ |
| 50 | #define __android_logcat_context_defined |
| 51 | typedef struct android_logcat_context_internal* android_logcat_context; |
| 52 | #endif |
| 53 | |
| 54 | /* Creates a context associated with this logcat instance |
| 55 | * |
| 56 | * Returns a pointer to the context, or a NULL on error. |
| 57 | */ |
| 58 | android_logcat_context create_android_logcat(); |
| 59 | |
| 60 | /* Collects and outputs the logcat data to output and error file descriptors |
| 61 | * |
| 62 | * Will block, performed in-thread and in-process |
| 63 | * |
| 64 | * The output file descriptor variable, if greater than or equal to 0, is |
| 65 | * where the output (ie: stdout) will be sent. The file descriptor is closed |
| 66 | * on android_logcat_destroy which terminates the instance, or when an -f flag |
| 67 | * (output redirect to a file) is present in the command. The error file |
| 68 | * descriptor variable, if greater than or equal to 0, is where the error |
| 69 | * stream (ie: stderr) will be sent, also closed on android_logcat_destroy. |
| 70 | * The error file descriptor can be set to equal to the output file descriptor, |
| 71 | * which will mix output and error stream content, and will defer closure of |
| 72 | * the file descriptor on -f flag redirection. Negative values for the file |
| 73 | * descriptors will use stdout and stderr FILE references respectively |
| 74 | * internally, and will not close the references as noted above. |
| 75 | * |
| 76 | * Return value is 0 for success, non-zero for errors. |
| 77 | */ |
Elliott Hughes | 61b580e | 2018-06-15 15:16:20 -0700 | [diff] [blame] | 78 | int android_logcat_run_command(android_logcat_context ctx, int output, int error, int argc, |
| 79 | char* const* argv, char* const* envp); |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 80 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 81 | /* Finished with context |
| 82 | * |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 83 | * Kill the command thread ASAP (if any), and free up all associated resources. |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 84 | * |
| 85 | * Return value is the result of the android_logcat_run_command, or |
| 86 | * non-zero for any errors. |
| 87 | */ |
| 88 | int android_logcat_destroy(android_logcat_context* ctx); |
| 89 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 90 | #endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */ |
| 91 | |
| 92 | #ifdef __cplusplus |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #endif /* _LIBS_LOGCAT_H */ |