3 * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished
10 * to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include <sys/socket.h>
33 #include <backend_androidlogger.h>
35 #include <logcommon.h>
36 #include <logconfig.h>
37 #ifdef USE_ANDROID_MONOTONIC
38 #include <queued_entry.h>
39 #endif /* USE_ANDROID_MONOTONIC */
41 /* TODO: initialize in for loop to be sure of initializing all items */
42 static int log_fds[LOG_ID_MAX];
46 * @details Writes a log
47 * @param[in] log_id ID of the buffer to log to. Belongs to (LOG_ID_INVALID, LOG_ID_MAX) non-inclusive
48 * @param[in] prio Priority of the message.
49 * @param[in] tag The message tag, identifies the sender.
50 * @param[in] msg The contents of the message.
51 * @return Number of bytes written, or negative errno
53 static int __write_to_log_android(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono)
58 if (log_id > LOG_ID_INVALID && log_id < LOG_ID_MAX)
59 log_fd = log_fds[log_id];
61 return DLOG_ERROR_INVALID_PARAMETER;
63 if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
64 return DLOG_ERROR_INVALID_PARAMETER;
70 return DLOG_ERROR_INVALID_PARAMETER;
72 #ifdef USE_ANDROID_MONOTONIC
74 struct android_logger_footer alf;
78 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
83 alf.magic = ANDROID_LOGGER_FOOTER_MAGIC;
85 alf.sec_sent_mono = tp_mono->tv_sec;
86 alf.nsec_sent_mono = tp_mono->tv_nsec;
88 alf.sec_sent_mono = 0;
89 alf.nsec_sent_mono = -1;
92 vec[3].iov_base = (void *) &alf;
93 vec[3].iov_len = sizeof alf;
96 #endif /* USE_ANDROID_MONOTONIC */
98 vec[0].iov_base = (unsigned char *) &prio;
100 vec[1].iov_base = (void *) tag;
101 vec[1].iov_len = strlen(tag) + 1;
102 vec[2].iov_base = (void *) msg;
103 vec[2].iov_len = strlen(msg) + 1;
105 ret = writev(log_fd, vec, NELEMS(vec));
112 static void __destroy_androidlogger(void)
114 /* No need for an explicit lock here; this should only be called
115 * by the library destructor which has its own lock */
117 for (size_t i = 0; i < NELEMS(log_fds); ++i)
123 * @brief Initialize the backend
124 * @details Prepares the backend for proper and fruitful work
126 void __dlog_init_android(const struct log_config *conf)
128 for (size_t i = 0; i < NELEMS(log_fds); ++i)
133 for (buf_id = 0; buf_id < LOG_ID_MAX; ++buf_id) {
134 int ret = logger_open_buffer_from_config(buf_id, conf, O_WRONLY | O_CLOEXEC, &log_fds[buf_id]);
138 else if (ret == 0) /* zero signifies no buffer */
141 write_to_log = __write_to_log_android;
142 destroy_backend = __destroy_androidlogger;
146 for (buf_id = 0; buf_id < LOG_ID_MAX; buf_id++)
147 if (log_fds[buf_id] >= 0)
148 close(log_fds[buf_id]);