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