1 /* 2 * Copyright (C) 2006 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 #pragma once 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <sys/types.h> 22 23 #include <android/log.h> 24 #include <log/event_tag_map.h> 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 typedef enum { 31 /* Verbs */ 32 FORMAT_OFF = 0, 33 FORMAT_BRIEF, 34 FORMAT_PROCESS, 35 FORMAT_TAG, 36 FORMAT_THREAD, 37 FORMAT_RAW, 38 FORMAT_TIME, 39 FORMAT_THREADTIME, 40 FORMAT_LONG, 41 /* Adverbs. The following are modifiers to above format verbs */ 42 FORMAT_MODIFIER_COLOR, /* converts priority to color */ 43 FORMAT_MODIFIER_TIME_USEC, /* switches from msec to usec time precision */ 44 FORMAT_MODIFIER_PRINTABLE, /* converts non-printable to printable escapes */ 45 FORMAT_MODIFIER_YEAR, /* Adds year to date */ 46 FORMAT_MODIFIER_ZONE, /* Adds zone to date, + UTC */ 47 FORMAT_MODIFIER_EPOCH, /* Print time as seconds since Jan 1 1970 */ 48 FORMAT_MODIFIER_MONOTONIC, /* Print cpu time as seconds since start */ 49 FORMAT_MODIFIER_UID, /* Adds uid */ 50 FORMAT_MODIFIER_DESCRIPT, /* Adds descriptive */ 51 /* private, undocumented */ 52 FORMAT_MODIFIER_TIME_NSEC, /* switches from msec to nsec time precision */ 53 } AndroidLogPrintFormat; 54 55 typedef struct AndroidLogFormat_t AndroidLogFormat; 56 57 typedef struct AndroidLogEntry_t { 58 time_t tv_sec; 59 long tv_nsec; 60 android_LogPriority priority; 61 int32_t uid; 62 int32_t pid; 63 int32_t tid; 64 const char* tag; 65 size_t tagLen; 66 size_t messageLen; 67 const char* message; 68 } AndroidLogEntry; 69 70 AndroidLogFormat* android_log_format_new(); 71 72 void android_log_format_free(AndroidLogFormat* p_format); 73 74 /* currently returns 0 if format is a modifier, 1 if not */ 75 int android_log_setPrintFormat(AndroidLogFormat* p_format, 76 AndroidLogPrintFormat format); 77 78 /** 79 * Returns FORMAT_OFF on invalid string 80 */ 81 AndroidLogPrintFormat android_log_formatFromString(const char* s); 82 83 /** 84 * filterExpression: a single filter expression 85 * eg "AT:d" 86 * 87 * returns 0 on success and -1 on invalid expression 88 * 89 * Assumes single threaded execution 90 * 91 */ 92 93 int android_log_addFilterRule(AndroidLogFormat* p_format, 94 const char* filterExpression); 95 96 /** 97 * filterString: a whitespace-separated set of filter expressions 98 * eg "AT:d *:i" 99 * 100 * returns 0 on success and -1 on invalid expression 101 * 102 * Assumes single threaded execution 103 * 104 */ 105 106 int android_log_addFilterString(AndroidLogFormat* p_format, 107 const char* filterString); 108 109 /** 110 * returns 1 if this log line should be printed based on its priority 111 * and tag, and 0 if it should not 112 */ 113 int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag, 114 android_LogPriority pri); 115 116 /** 117 * Splits a wire-format buffer into an AndroidLogEntry 118 * entry allocated by caller. Pointers will point directly into buf 119 * 120 * Returns 0 on success and -1 on invalid wire format (entry will be 121 * in unspecified state) 122 */ 123 int android_log_processLogBuffer(struct logger_entry* buf, 124 AndroidLogEntry* entry); 125 126 /** 127 * Like android_log_processLogBuffer, but for binary logs. 128 * 129 * If "map" is non-NULL, it will be used to convert the log tag number 130 * into a string. 131 */ 132 int android_log_processBinaryLogBuffer(struct logger_entry* buf, 133 AndroidLogEntry* entry, 134 const EventTagMap* map, char* messageBuf, 135 int messageBufLen); 136 137 /** 138 * Formats a log message into a buffer 139 * 140 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer 141 * If return value != defaultBuffer, caller must call free() 142 * Returns NULL on malloc error 143 */ 144 145 char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer, 146 size_t defaultBufferSize, 147 const AndroidLogEntry* p_line, 148 size_t* p_outLength); 149 150 /** 151 * Formats a log message into a FILE*. 152 */ 153 size_t android_log_printLogLine(AndroidLogFormat* p_format, FILE* fp, const AndroidLogEntry* entry); 154 155 #ifdef __cplusplus 156 } 157 #endif 158