1 /*
2 * Copyright (C) 2016 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 <log/log_transport.h>
18
19 #include "config_write.h"
20 #include "logger.h"
21
22 struct listnode __android_log_transport_write = {&__android_log_transport_write,
23 &__android_log_transport_write};
24 struct listnode __android_log_persist_write = {&__android_log_persist_write,
25 &__android_log_persist_write};
26
__android_log_add_transport(struct listnode * list,struct android_log_transport_write * transport)27 static void __android_log_add_transport(struct listnode* list,
28 struct android_log_transport_write* transport) {
29 uint32_t i;
30
31 /* Try to keep one functioning transport for each log buffer id */
32 for (i = LOG_ID_MIN; i < LOG_ID_MAX; i++) {
33 struct android_log_transport_write* transp;
34
35 if (list_empty(list)) {
36 if (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0)) {
37 list_add_tail(list, &transport->node);
38 return;
39 }
40 } else {
41 write_transport_for_each(transp, list) {
42 if (!transp->available) {
43 return;
44 }
45 if (((*transp->available)(static_cast<log_id_t>(i)) < 0) &&
46 (!transport->available || ((*transport->available)(static_cast<log_id_t>(i)) >= 0))) {
47 list_add_tail(list, &transport->node);
48 return;
49 }
50 }
51 }
52 }
53 }
54
__android_log_config_write()55 void __android_log_config_write() {
56 if ((__android_log_transport == LOGGER_DEFAULT) || (__android_log_transport & LOGGER_LOGD)) {
57 #if (FAKE_LOG_DEVICE == 0)
58 extern struct android_log_transport_write logdLoggerWrite;
59 extern struct android_log_transport_write pmsgLoggerWrite;
60
61 __android_log_add_transport(&__android_log_transport_write, &logdLoggerWrite);
62 __android_log_add_transport(&__android_log_persist_write, &pmsgLoggerWrite);
63 #else
64 extern struct android_log_transport_write fakeLoggerWrite;
65
66 __android_log_add_transport(&__android_log_transport_write, &fakeLoggerWrite);
67 #endif
68 }
69
70 if (__android_log_transport & LOGGER_STDERR) {
71 extern struct android_log_transport_write stderrLoggerWrite;
72
73 /*
74 * stderr logger should be primary if we can be the only one, or if
75 * already in the primary list. Otherwise land in the persist list.
76 * Remember we can be called here if we are already initialized.
77 */
78 if (list_empty(&__android_log_transport_write)) {
79 __android_log_add_transport(&__android_log_transport_write, &stderrLoggerWrite);
80 } else {
81 struct android_log_transport_write* transp;
82 write_transport_for_each(transp, &__android_log_transport_write) {
83 if (transp == &stderrLoggerWrite) {
84 return;
85 }
86 }
87 __android_log_add_transport(&__android_log_persist_write, &stderrLoggerWrite);
88 }
89 }
90 }
91
__android_log_config_write_close()92 void __android_log_config_write_close() {
93 struct android_log_transport_write* transport;
94 struct listnode* n;
95
96 write_transport_for_each_safe(transport, n, &__android_log_transport_write) {
97 transport->logMask = 0;
98 list_remove(&transport->node);
99 }
100 write_transport_for_each_safe(transport, n, &__android_log_persist_write) {
101 transport->logMask = 0;
102 list_remove(&transport->node);
103 }
104 }
105