syslog.cpp revision b76613627d045acd3bdb7294f424f14c21584872
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 17#include <syslog.h> 18 19#include <stdlib.h> 20 21#include "private/libc_logging.h" 22 23static const char* syslog_log_tag = NULL; 24static int syslog_priority_mask = 0xff; 25 26void closelog() { 27} 28 29void openlog(const char* log_tag, int /*options*/, int /*facility*/) { 30 syslog_log_tag = log_tag; 31} 32 33int 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 42void 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 49void 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; 64 if (priority < LOG_ERR) { 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} 76