Replace () with (void) in function prototypes
[platform/core/system/dlog.git] / src / libdlog / log_android.c
1 /*  MIT License
2  *
3  * Copyright (c) 2015-2020 Samsung Electronics Co., Ltd
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * THE SOFTWARE. */
22
23 // C
24 #include <stdio.h>
25
26 // POSIX
27 #include <fcntl.h>
28 #include <time.h>
29 #include <sys/socket.h>
30 #include <sys/uio.h>
31
32 // DLog
33 #include <backend_androidlogger.h>
34 #include <libdlog.h>
35 #include <logcommon.h>
36 #include <logconfig.h>
37 #ifdef USE_ANDROID_MONOTONIC
38 #include <queued_entry.h>
39 #endif /* USE_ANDROID_MONOTONIC */
40
41 /* TODO: initialize in for loop to be sure of initializing all items */
42 static int log_fds[LOG_ID_MAX];
43
44 /**
45  * @brief Write to log
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
52  */
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)
54 {
55         ssize_t ret;
56         int log_fd;
57
58         if (log_id > LOG_ID_INVALID && log_id < LOG_ID_MAX)
59                 log_fd = log_fds[log_id];
60         else
61                 return DLOG_ERROR_INVALID_PARAMETER;
62
63         if (prio < DLOG_VERBOSE || prio >= DLOG_PRIO_MAX)
64                 return DLOG_ERROR_INVALID_PARAMETER;
65
66         if (!tag)
67                 tag = "";
68
69         if (!msg)
70                 return DLOG_ERROR_INVALID_PARAMETER;
71
72 #ifdef USE_ANDROID_MONOTONIC
73         struct iovec vec[4];
74         struct android_logger_footer alf;
75         struct timespec ts;
76
77         if (!tp_mono) {
78                 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
79                 if (result >= 0)
80                         tp_mono = &ts;
81         }
82
83         alf.magic = ANDROID_LOGGER_FOOTER_MAGIC;
84         if (tp_mono) {
85                 alf.sec_sent_mono = tp_mono->tv_sec;
86                 alf.nsec_sent_mono = tp_mono->tv_nsec;
87         } else {
88                 alf.sec_sent_mono = 0;
89                 alf.nsec_sent_mono = -1;
90         }
91
92         vec[3].iov_base = (void *) &alf;
93         vec[3].iov_len = sizeof alf;
94 #else
95         struct iovec vec[3];
96 #endif /* USE_ANDROID_MONOTONIC */
97
98         vec[0].iov_base = (unsigned char *) &prio;
99         vec[0].iov_len  = 1;
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;
104
105         ret = writev(log_fd, vec, NELEMS(vec));
106         if (ret < 0)
107                 ret = -errno;
108
109         return ret;
110 }
111
112 static void __destroy_androidlogger(void)
113 {
114         /* No need for an explicit lock here; this should only be called
115          * by the library destructor which has its own lock */
116
117         for (size_t i = 0; i < NELEMS(log_fds); ++i)
118                 if (log_fds[i] >= 0)
119                         close(log_fds[i]);
120 }
121
122 /**
123  * @brief Initialize the backend
124  * @details Prepares the backend for proper and fruitful work
125  */
126 void __dlog_init_android(const struct log_config *conf)
127 {
128         for (size_t i = 0; i < NELEMS(log_fds); ++i)
129                 log_fds[i] = -1;
130
131         log_id_t buf_id;
132
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]);
135
136                 if (ret < 0)
137                         goto failure;
138                 else if (ret == 0) /* zero signifies no buffer */
139                         continue;
140         }
141         write_to_log = __write_to_log_android;
142         destroy_backend = __destroy_androidlogger;
143         return;
144
145 failure:
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]);
149 }