blob: 6b17d268a09d062328c919329f1778c28818f2d8 [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 Hughes3ad8ecb2014-07-21 16:35:24 -070021
Christopher Ferris7a3681e2017-04-24 17:48:32 -070022#include <async_safe/log.h>
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070023
Yi Kong32bc0fc2018-08-02 17:31:13 -070024static const char* syslog_log_tag = nullptr;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070025static int syslog_priority_mask = 0xff;
26
27void closelog() {
Yi Kong32bc0fc2018-08-02 17:31:13 -070028 syslog_log_tag = nullptr;
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070029}
30
31void openlog(const char* log_tag, int /*options*/, int /*facility*/) {
32 syslog_log_tag = log_tag;
33}
34
35int setlogmask(int new_mask) {
36 int old_mask = syslog_priority_mask;
37 // 0 is used to query the current mask.
38 if (new_mask != 0) {
39 syslog_priority_mask = new_mask;
40 }
41 return old_mask;
42}
43
44void syslog(int priority, const char* fmt, ...) {
45 va_list args;
46 va_start(args, fmt);
47 vsyslog(priority, fmt, args);
48 va_end(args);
49}
50
51void vsyslog(int priority, const char* fmt, va_list args) {
52 // Check whether we're supposed to be logging messages of this priority.
53 if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) {
54 return;
55 }
56
57 // What's our log tag?
58 const char* log_tag = syslog_log_tag;
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 if (log_tag == nullptr) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070060 log_tag = getprogname();
61 }
62
63 // What's our Android log priority?
64 priority &= LOG_PRIMASK;
65 int android_log_priority;
Elliott Hughesafe63602014-07-23 11:38:38 -070066 if (priority <= LOG_ERR) {
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070067 android_log_priority = ANDROID_LOG_ERROR;
68 } else if (priority == LOG_WARNING) {
69 android_log_priority = ANDROID_LOG_WARN;
70 } else if (priority <= LOG_INFO) {
71 android_log_priority = ANDROID_LOG_INFO;
72 } else {
73 android_log_priority = ANDROID_LOG_DEBUG;
74 }
75
Christopher Ferris7a3681e2017-04-24 17:48:32 -070076 // We can't let async_safe_format_log do the formatting because it doesn't support
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070077 // all the printf functionality.
78 char log_line[1024];
Elliott Hughes654cd832018-08-30 16:00:42 -070079 vsnprintf(log_line, sizeof(log_line), fmt, args);
Elliott Hughesf1e83cc2014-07-25 20:31:47 -070080
Christopher Ferris7a3681e2017-04-24 17:48:32 -070081 async_safe_format_log(android_log_priority, log_tag, "%s", log_line);
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -070082}