blob: 9ac55b39bea894af6065cdbd79b2687acd1bcc91 [file] [log] [blame]
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -07001/*
2 * Copyright (C) 2014 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
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070017#include <errno.h>
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070018#include <stdlib.h>
Elliott Hughes76f89162015-01-26 13:34:58 -080019#include <string.h>
Elliott Hughesafe63602014-07-23 11:38:38 -070020#include <syslog.h>
Elliott Hughes213d9432023-03-01 22:56:13 +000021#include <unistd.h>
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070022
Christopher Ferris7a3681e2017-04-24 17:48:32 -070023#include <async_safe/log.h>
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070024
Yi Kong32bc0fc2018-08-02 17:31:13 -070025static const char* syslog_log_tag = nullptr;
Elliott Hughes5cfe7dc2025-07-27 19:26:46 -070026// The "mask" is of _enabled_ log priorities, and defaults to all.
27static int syslog_priority_mask = LOG_UPTO(LOG_DEBUG);
Elliott Hughes213d9432023-03-01 22:56:13 +000028static int syslog_options = 0;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070029
30void closelog() {
Yi Kong32bc0fc2018-08-02 17:31:13 -070031 syslog_log_tag = nullptr;
Elliott Hughes213d9432023-03-01 22:56:13 +000032 syslog_options = 0;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070033}
34
Elliott Hughes213d9432023-03-01 22:56:13 +000035void openlog(const char* log_tag, int options, int /*facility*/) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070036 syslog_log_tag = log_tag;
Elliott Hughes213d9432023-03-01 22:56:13 +000037 syslog_options = options;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070038}
39
40int setlogmask(int new_mask) {
41 int old_mask = syslog_priority_mask;
42 // 0 is used to query the current mask.
43 if (new_mask != 0) {
44 syslog_priority_mask = new_mask;
45 }
46 return old_mask;
47}
48
49void syslog(int priority, const char* fmt, ...) {
50 va_list args;
51 va_start(args, fmt);
52 vsyslog(priority, fmt, args);
53 va_end(args);
54}
55
56void vsyslog(int priority, const char* fmt, va_list args) {
57 // Check whether we're supposed to be logging messages of this priority.
58 if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) {
59 return;
60 }
61
62 // What's our log tag?
63 const char* log_tag = syslog_log_tag;
Yi Kong32bc0fc2018-08-02 17:31:13 -070064 if (log_tag == nullptr) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070065 log_tag = getprogname();
66 }
67
68 // What's our Android log priority?
69 priority &= LOG_PRIMASK;
70 int android_log_priority;
Elliott Hughesafe63602014-07-23 11:38:38 -070071 if (priority <= LOG_ERR) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070072 android_log_priority = ANDROID_LOG_ERROR;
73 } else if (priority == LOG_WARNING) {
74 android_log_priority = ANDROID_LOG_WARN;
75 } else if (priority <= LOG_INFO) {
76 android_log_priority = ANDROID_LOG_INFO;
77 } else {
78 android_log_priority = ANDROID_LOG_DEBUG;
79 }
80
Elliott Hughes213d9432023-03-01 22:56:13 +000081 // We can't let async_safe_format_log do the formatting because it doesn't
82 // support all the printf functionality.
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070083 char log_line[1024];
Elliott Hughes213d9432023-03-01 22:56:13 +000084 int n = vsnprintf(log_line, sizeof(log_line), fmt, args);
85 if (n < 0) return;
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070086
Frederick Mayle2fb259e2025-07-14 17:51:21 -070087 async_safe_write_log(android_log_priority, log_tag, log_line);
Elliott Hughes213d9432023-03-01 22:56:13 +000088 if ((syslog_options & LOG_PERROR) != 0) {
89 bool have_newline =
90 (n > 0 && n < static_cast<int>(sizeof(log_line)) && log_line[n - 1] == '\n');
91 dprintf(STDERR_FILENO, "%s: %s%s", log_tag, log_line, have_newline ? "" : "\n");
92 }
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070093}