Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame^] | 18 | #include <syslog.h> |
Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 19 | |
| 20 | #include "private/libc_logging.h" |
| 21 | |
| 22 | static const char* syslog_log_tag = NULL; |
| 23 | static int syslog_priority_mask = 0xff; |
| 24 | |
| 25 | void closelog() { |
Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame^] | 26 | syslog_log_tag = NULL; |
Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void openlog(const char* log_tag, int /*options*/, int /*facility*/) { |
| 30 | syslog_log_tag = log_tag; |
| 31 | } |
| 32 | |
| 33 | int setlogmask(int new_mask) { |
| 34 | int old_mask = syslog_priority_mask; |
| 35 | // 0 is used to query the current mask. |
| 36 | if (new_mask != 0) { |
| 37 | syslog_priority_mask = new_mask; |
| 38 | } |
| 39 | return old_mask; |
| 40 | } |
| 41 | |
| 42 | void syslog(int priority, const char* fmt, ...) { |
| 43 | va_list args; |
| 44 | va_start(args, fmt); |
| 45 | vsyslog(priority, fmt, args); |
| 46 | va_end(args); |
| 47 | } |
| 48 | |
| 49 | void vsyslog(int priority, const char* fmt, va_list args) { |
| 50 | // Check whether we're supposed to be logging messages of this priority. |
| 51 | if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // What's our log tag? |
| 56 | const char* log_tag = syslog_log_tag; |
| 57 | if (log_tag == NULL) { |
| 58 | log_tag = getprogname(); |
| 59 | } |
| 60 | |
| 61 | // What's our Android log priority? |
| 62 | priority &= LOG_PRIMASK; |
| 63 | int android_log_priority; |
Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame^] | 64 | if (priority <= LOG_ERR) { |
Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 65 | android_log_priority = ANDROID_LOG_ERROR; |
| 66 | } else if (priority == LOG_WARNING) { |
| 67 | android_log_priority = ANDROID_LOG_WARN; |
| 68 | } else if (priority <= LOG_INFO) { |
| 69 | android_log_priority = ANDROID_LOG_INFO; |
| 70 | } else { |
| 71 | android_log_priority = ANDROID_LOG_DEBUG; |
| 72 | } |
| 73 | |
| 74 | __libc_format_log_va_list(android_log_priority, log_tag, fmt, args); |
| 75 | } |