liblog_test.cpp revision 46abc522964f797fafb0a3af2cbbabee05597f47
1/* 2 * Copyright (C) 2013 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#include <fcntl.h> 18#include <gtest/gtest.h> 19#include <log/log.h> 20#include <log/logger.h> 21 22TEST(liblog, __android_log_buf_print) { 23 ASSERT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, 24 "TEST__android_log_buf_print", 25 "radio")); 26 usleep(1000); 27 ASSERT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, 28 "TEST__android_log_buf_print", 29 "system")); 30 usleep(1000); 31 ASSERT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, 32 "TEST__android_log_buf_print", 33 "main")); 34 usleep(1000); 35} 36 37TEST(liblog, __android_log_buf_write) { 38 ASSERT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO, 39 "TEST__android_log_buf_write", 40 "radio")); 41 usleep(1000); 42 ASSERT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, 43 "TEST__android_log_buf_write", 44 "system")); 45 usleep(1000); 46 ASSERT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO, 47 "TEST__android_log_buf_write", 48 "main")); 49 usleep(1000); 50} 51 52TEST(liblog, __android_log_btwrite) { 53 int intBuf = 0xDEADBEEF; 54 ASSERT_LT(0, __android_log_btwrite(0, 55 EVENT_TYPE_INT, 56 &intBuf, sizeof(intBuf))); 57 long long longBuf = 0xDEADBEEFA55A5AA5; 58 ASSERT_LT(0, __android_log_btwrite(0, 59 EVENT_TYPE_LONG, 60 &longBuf, sizeof(longBuf))); 61 usleep(1000); 62 char Buf[] = "\20\0\0\0DeAdBeEfA55a5aA5"; 63 ASSERT_LT(0, __android_log_btwrite(0, 64 EVENT_TYPE_STRING, 65 Buf, sizeof(Buf) - 1)); 66 usleep(1000); 67} 68 69static void* ConcurrentPrintFn(void *arg) { 70 int ret = __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, 71 "TEST__android_log_print", "Concurrent %d", 72 reinterpret_cast<int>(arg)); 73 return reinterpret_cast<void*>(ret); 74} 75 76#define NUM_CONCURRENT 64 77#define _concurrent_name(a,n) a##__concurrent##n 78#define concurrent_name(a,n) _concurrent_name(a,n) 79 80TEST(liblog, concurrent_name(__android_log_buf_print, NUM_CONCURRENT)) { 81 pthread_t t[NUM_CONCURRENT]; 82 int i; 83 for (i=0; i < NUM_CONCURRENT; i++) { 84 ASSERT_EQ(0, pthread_create(&t[i], NULL, 85 ConcurrentPrintFn, 86 reinterpret_cast<void *>(i))); 87 } 88 int ret = 0; 89 for (i=0; i < NUM_CONCURRENT; i++) { 90 void* result; 91 ASSERT_EQ(0, pthread_join(t[i], &result)); 92 if ((0 == ret) && (0 != reinterpret_cast<int>(result))) { 93 ret = reinterpret_cast<int>(result); 94 } 95 } 96 ASSERT_LT(0, ret); 97} 98 99TEST(liblog, __android_log_btwrite__android_logger_list_read) { 100 pid_t pid; 101 struct logger_list *logger_list; 102 log_time_t ts; 103 104 pid = getpid(); 105 106 ASSERT_EQ(0, NULL == (logger_list = android_logger_list_open( 107 LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid))); 108 109 clock_gettime(CLOCK_MONOTONIC, &ts); 110 111 ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); 112 usleep(1000000); 113 114 int count = 0; 115 116 for (;;) { 117 log_msg log_msg; 118 if (android_logger_list_read(logger_list, &log_msg) <= 0) { 119 break; 120 } 121 122 ASSERT_EQ(log_msg.entry.pid, pid); 123 124 if ((log_msg.entry.len != (4 + 1 + 8)) 125 || (log_msg.id() != LOG_ID_EVENTS)) { 126 continue; 127 } 128 129 char *eventData = log_msg.msg(); 130 131 if (eventData[4] != EVENT_TYPE_LONG) { 132 continue; 133 } 134 135 log_time_t tx(eventData + 4 + 1); 136 if (ts == tx) { 137 ++count; 138 } 139 } 140 141 ASSERT_EQ(1, count); 142 143 android_logger_list_close(logger_list); 144} 145 146TEST(liblog, android_logger_get_) { 147 struct logger_list * logger_list = android_logger_list_alloc(O_WRONLY, 0, 0); 148 149 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { 150 log_id_t id = static_cast<log_id_t>(i); 151 const char *name = android_log_id_to_name(id); 152 if (id != android_name_to_log_id(name)) { 153 continue; 154 } 155 struct logger * logger; 156 ASSERT_EQ(0, NULL == (logger = android_logger_open(logger_list, id))); 157 ASSERT_EQ(id, android_logger_get_id(logger)); 158 ASSERT_LT(0, android_logger_get_log_size(logger)); 159 ASSERT_LT(0, android_logger_get_log_readable_size(logger)); 160 ASSERT_LT(0, android_logger_get_log_version(logger)); 161 } 162 163 android_logger_list_close(logger_list); 164} 165