system/corennnnn
Revision | 2d149691552676968b7de337f543463b920578b0 (tree) |
---|---|
Zeit | 2016-09-05 23:50:58 |
Autor | Mark Salyzyn <salyzyn@goog...> |
Commiter | Narayan Kamath |
liblog: add android_log_close()
Bug: 30963384
(cherry picked from commit df7a4c6bae5f85532d79a93b7d9197a2aab17825)
Change-Id: I2255486e84dd55af0f4e7fbbfb616c2deb1765d0
@@ -89,6 +89,11 @@ typedef enum android_LogPriority { | ||
89 | 89 | } android_LogPriority; |
90 | 90 | |
91 | 91 | /* |
92 | + * Release any logger resources (a new log write will immediately re-acquire) | |
93 | + */ | |
94 | +void __android_log_close(); | |
95 | + | |
96 | +/* | |
92 | 97 | * Send a simple string to the log. |
93 | 98 | */ |
94 | 99 | int __android_log_write(int prio, const char *tag, const char *text); |
@@ -282,6 +282,45 @@ const char *android_log_id_to_name(log_id_t log_id) | ||
282 | 282 | } |
283 | 283 | #endif |
284 | 284 | |
285 | +/* | |
286 | + * Release any logger resources. A new log write will immediately re-acquire. | |
287 | + */ | |
288 | +void __android_log_close() | |
289 | +{ | |
290 | +#if FAKE_LOG_DEVICE | |
291 | + int i; | |
292 | +#endif | |
293 | + | |
294 | +#ifdef HAVE_PTHREADS | |
295 | + pthread_mutex_lock(&log_init_lock); | |
296 | +#endif | |
297 | + | |
298 | + write_to_log = __write_to_log_init; | |
299 | + | |
300 | + /* | |
301 | + * Threads that are actively writing at this point are not held back | |
302 | + * by a lock and are at risk of dropping the messages with a return code | |
303 | + * -EBADF. Prefer to return error code than add the overhead of a lock to | |
304 | + * each log writing call to guarantee delivery. In addition, anyone | |
305 | + * calling this is doing so to release the logging resources and shut down, | |
306 | + * for them to do so with outstanding log requests in other threads is a | |
307 | + * disengenuous use of this function. | |
308 | + */ | |
309 | +#if FAKE_LOG_DEVICE | |
310 | + for (i = 0; i < LOG_ID_MAX; i++) { | |
311 | + fakeLogClose(log_fds[i]); | |
312 | + log_fds[i] = -1; | |
313 | + } | |
314 | +#else | |
315 | + close(logd_fd); | |
316 | + logd_fd = -1; | |
317 | +#endif | |
318 | + | |
319 | +#ifdef HAVE_PTHREADS | |
320 | + pthread_mutex_unlock(&log_init_lock); | |
321 | +#endif | |
322 | +} | |
323 | + | |
285 | 324 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
286 | 325 | { |
287 | 326 | #ifdef HAVE_PTHREADS |
@@ -117,6 +117,41 @@ static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr) | ||
117 | 117 | return ret; |
118 | 118 | } |
119 | 119 | |
120 | +/* | |
121 | + * Release any logger resources. A new log write will immediately re-acquire. | |
122 | + */ | |
123 | +void __android_log_close() | |
124 | +{ | |
125 | +#ifdef HAVE_PTHREADS | |
126 | + pthread_mutex_lock(&log_init_lock); | |
127 | +#endif | |
128 | + | |
129 | + write_to_log = __write_to_log_init; | |
130 | + | |
131 | + /* | |
132 | + * Threads that are actively writing at this point are not held back | |
133 | + * by a lock and are at risk of dropping the messages with a return code | |
134 | + * -EBADF. Prefer to return error code than add the overhead of a lock to | |
135 | + * each log writing call to guarantee delivery. In addition, anyone | |
136 | + * calling this is doing so to release the logging resources and shut down, | |
137 | + * for them to do so with outstanding log requests in other threads is a | |
138 | + * disengenuous use of this function. | |
139 | + */ | |
140 | + | |
141 | + log_close(log_fds[LOG_ID_MAIN]); | |
142 | + log_fds[LOG_ID_MAIN] = -1; | |
143 | + log_close(log_fds[LOG_ID_RADIO]); | |
144 | + log_fds[LOG_ID_RADIO] = -1; | |
145 | + log_close(log_fds[LOG_ID_EVENTS]); | |
146 | + log_fds[LOG_ID_EVENTS] = -1; | |
147 | + log_close(log_fds[LOG_ID_SYSTEM]); | |
148 | + log_fds[LOG_ID_SYSTEM] = -1; | |
149 | + | |
150 | +#ifdef HAVE_PTHREADS | |
151 | + pthread_mutex_unlock(&log_init_lock); | |
152 | +#endif | |
153 | +} | |
154 | + | |
120 | 155 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
121 | 156 | { |
122 | 157 | #ifdef HAVE_PTHREADS |
@@ -124,12 +124,17 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { | ||
124 | 124 | ASSERT_TRUE(NULL != (logger_list = android_logger_list_open( |
125 | 125 | LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid))); |
126 | 126 | |
127 | + // Check that we can close and reopen the logger | |
127 | 128 | log_time ts(CLOCK_MONOTONIC); |
128 | - | |
129 | 129 | ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); |
130 | + __android_log_close(); | |
131 | + | |
132 | + log_time ts1(CLOCK_MONOTONIC); | |
133 | + ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts1, sizeof(ts1))); | |
130 | 134 | usleep(1000000); |
131 | 135 | |
132 | 136 | int count = 0; |
137 | + int second_count = 0; | |
133 | 138 | |
134 | 139 | for (;;) { |
135 | 140 | log_msg log_msg; |
@@ -153,10 +158,13 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) { | ||
153 | 158 | log_time tx(eventData + 4 + 1); |
154 | 159 | if (ts == tx) { |
155 | 160 | ++count; |
161 | + } else if (ts1 == tx) { | |
162 | + ++second_count; | |
156 | 163 | } |
157 | 164 | } |
158 | 165 | |
159 | 166 | EXPECT_EQ(1, count); |
167 | + EXPECT_EQ(1, second_count); | |
160 | 168 | |
161 | 169 | android_logger_list_close(logger_list); |
162 | 170 | } |