| 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 | f1e83cc | 2014-07-25 20:31:47 -0700 | [diff] [blame] | 17 | #include <errno.h> | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 18 | #include <stdlib.h> | 
| Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 19 | #include <string.h> | 
| Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame] | 20 | #include <syslog.h> | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 21 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 22 | #include <async_safe/log.h> | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 23 |  | 
 | 24 | static const char* syslog_log_tag = NULL; | 
 | 25 | static int syslog_priority_mask = 0xff; | 
 | 26 |  | 
 | 27 | void closelog() { | 
| Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame] | 28 |   syslog_log_tag = NULL; | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 29 | } | 
 | 30 |  | 
 | 31 | void openlog(const char* log_tag, int /*options*/, int /*facility*/) { | 
 | 32 |   syslog_log_tag = log_tag; | 
 | 33 | } | 
 | 34 |  | 
 | 35 | int 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 |  | 
 | 44 | void 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 |  | 
 | 51 | void vsyslog(int priority, const char* fmt, va_list args) { | 
| Elliott Hughes | f1e83cc | 2014-07-25 20:31:47 -0700 | [diff] [blame] | 52 |   int caller_errno = errno; | 
 | 53 |  | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 54 |   // Check whether we're supposed to be logging messages of this priority. | 
 | 55 |   if ((syslog_priority_mask & LOG_MASK(LOG_PRI(priority))) == 0) { | 
 | 56 |     return; | 
 | 57 |   } | 
 | 58 |  | 
 | 59 |   // What's our log tag? | 
 | 60 |   const char* log_tag = syslog_log_tag; | 
 | 61 |   if (log_tag == NULL) { | 
 | 62 |     log_tag = getprogname(); | 
 | 63 |   } | 
 | 64 |  | 
 | 65 |   // What's our Android log priority? | 
 | 66 |   priority &= LOG_PRIMASK; | 
 | 67 |   int android_log_priority; | 
| Elliott Hughes | afe6360 | 2014-07-23 11:38:38 -0700 | [diff] [blame] | 68 |   if (priority <= LOG_ERR) { | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 69 |     android_log_priority = ANDROID_LOG_ERROR; | 
 | 70 |   } else if (priority == LOG_WARNING) { | 
 | 71 |     android_log_priority = ANDROID_LOG_WARN; | 
 | 72 |   } else if (priority <= LOG_INFO) { | 
 | 73 |     android_log_priority = ANDROID_LOG_INFO; | 
 | 74 |   } else { | 
 | 75 |     android_log_priority = ANDROID_LOG_DEBUG; | 
 | 76 |   } | 
 | 77 |  | 
| Elliott Hughes | f1e83cc | 2014-07-25 20:31:47 -0700 | [diff] [blame] | 78 |   // glibc's printf family support %m directly, but our BSD-based one doesn't. | 
 | 79 |   // If the format string seems to contain "%m", rewrite it. | 
 | 80 |   const char* log_fmt = fmt; | 
 | 81 |   if (strstr(fmt, "%m") != NULL) { | 
 | 82 |     size_t dst_len = 1024; | 
 | 83 |     char* dst = reinterpret_cast<char*>(malloc(dst_len)); | 
 | 84 |     log_fmt = dst; | 
 | 85 |  | 
 | 86 |     const char* src = fmt; | 
 | 87 |     for (; dst_len > 0 && *src != '\0'; ++src) { | 
 | 88 |       if (*src == '%' && *(src + 1) == 'm') { | 
 | 89 |         // Expand %m. | 
 | 90 |         size_t n = strlcpy(dst, strerror(caller_errno), dst_len); | 
 | 91 |         if (n >= dst_len) { | 
 | 92 |           n = dst_len; | 
 | 93 |         } | 
 | 94 |         dst += n; | 
 | 95 |         dst_len -= n; | 
 | 96 |         ++src; | 
 | 97 |       } else if (*src == '%' && *(src + 1) == '%') { | 
 | 98 |         // We need to copy pairs of '%'s so the %m test works. | 
 | 99 |         if (dst_len <= 2) { | 
 | 100 |           break; | 
 | 101 |         } | 
 | 102 |         *dst++ = '%'; --dst_len; | 
 | 103 |         *dst++ = '%'; --dst_len; | 
 | 104 |         ++src; | 
 | 105 |       } else { | 
 | 106 |         *dst++ = *src; --dst_len; | 
 | 107 |       } | 
 | 108 |     } | 
 | 109 |     *dst = '\0'; | 
 | 110 |   } | 
 | 111 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 112 |   // We can't let async_safe_format_log do the formatting because it doesn't support | 
| Elliott Hughes | f1e83cc | 2014-07-25 20:31:47 -0700 | [diff] [blame] | 113 |   // all the printf functionality. | 
 | 114 |   char log_line[1024]; | 
 | 115 |   vsnprintf(log_line, sizeof(log_line), log_fmt, args); | 
 | 116 |  | 
 | 117 |   if (log_fmt != fmt) { | 
 | 118 |     free(const_cast<char*>(log_fmt)); | 
 | 119 |   } | 
 | 120 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 121 |   async_safe_format_log(android_log_priority, log_tag, "%s", log_line); | 
| Elliott Hughes | 3ad8ecb | 2014-07-21 16:35:24 -0700 | [diff] [blame] | 122 | } |