blob: a459c6b1ea84d50d3f71b59d92db55c721079d9e [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 Hughes3ad8ecb2014-07-21 16:35:24 -070026static int syslog_priority_mask = 0xff;
Elliott Hughes213d9432023-03-01 22:56:13 +000027static int syslog_options = 0;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070028
29void closelog() {
Yi Kong32bc0fc2018-08-02 17:31:13 -070030 syslog_log_tag = nullptr;
Elliott Hughes213d9432023-03-01 22:56:13 +000031 syslog_options = 0;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070032}
33
Elliott Hughes213d9432023-03-01 22:56:13 +000034void openlog(const char* log_tag, int options, int /*facility*/) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070035 syslog_log_tag = log_tag;
Elliott Hughes213d9432023-03-01 22:56:13 +000036 syslog_options = options;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070037}
38
39int setlogmask(int new_mask) {
40 int old_mask = syslog_priority_mask;
41 // 0 is used to query the current mask.
42 if (new_mask != 0) {
43 syslog_priority_mask = new_mask;
44 }
45 return old_mask;
46}
47
48void syslog(int priority, const char* fmt, ...) {
49 va_list args;
50 va_start(args, fmt);
51 vsyslog(priority, fmt, args);
52 va_end(args);
53}
54
55void vsyslog(int priority, const char* fmt, va_list args) {
56 // Check whether we're supposed to be logging messages of this priority.
57 if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) {
58 return;
59 }
60
61 // What's our log tag?
62 const char* log_tag = syslog_log_tag;
Yi Kong32bc0fc2018-08-02 17:31:13 -070063 if (log_tag == nullptr) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070064 log_tag = getprogname();
65 }
66
67 // What's our Android log priority?
68 priority &= LOG_PRIMASK;
69 int android_log_priority;
Elliott Hughesafe63602014-07-23 11:38:38 -070070 if (priority <= LOG_ERR) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070071 android_log_priority = ANDROID_LOG_ERROR;
72 } else if (priority == LOG_WARNING) {
73 android_log_priority = ANDROID_LOG_WARN;
74 } else if (priority <= LOG_INFO) {
75 android_log_priority = ANDROID_LOG_INFO;
76 } else {
77 android_log_priority = ANDROID_LOG_DEBUG;
78 }
79
Elliott Hughes213d9432023-03-01 22:56:13 +000080 // We can't let async_safe_format_log do the formatting because it doesn't
81 // support all the printf functionality.
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070082 char log_line[1024];
Elliott Hughes213d9432023-03-01 22:56:13 +000083 int n = vsnprintf(log_line, sizeof(log_line), fmt, args);
84 if (n < 0) return;
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070085
Christopher Ferris7a3681e2017-04-24 17:48:32 -070086 async_safe_format_log(android_log_priority, log_tag, "%s", log_line);
Elliott Hughes213d9432023-03-01 22:56:13 +000087 if ((syslog_options & LOG_PERROR) != 0) {
88 bool have_newline =
89 (n > 0 && n < static_cast<int>(sizeof(log_line)) && log_line[n - 1] == '\n');
90 dprintf(STDERR_FILENO, "%s: %s%s", log_tag, log_line, have_newline ? "" : "\n");
91 }
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070092}