Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [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 | |
| 17 | #include <ctype.h> |
| 18 | #include <limits.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 21 | |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 22 | #include <private/android_logger.h> |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 23 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 24 | // Add %#q for fractional seconds to standard strptime function |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 25 | char* log_time::strptime(const char* s, const char* format) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 26 | time_t now; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 27 | #ifdef __linux__ |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 28 | *this = log_time(CLOCK_REALTIME); |
| 29 | now = tv_sec; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 30 | #else |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 31 | time(&now); |
| 32 | tv_sec = now; |
| 33 | tv_nsec = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 34 | #endif |
| 35 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 36 | struct tm* ptm; |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 37 | #if !defined(_WIN32) |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 38 | struct tm tmBuf; |
| 39 | ptm = localtime_r(&now, &tmBuf); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 40 | #else |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 41 | ptm = localtime(&now); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 42 | #endif |
| 43 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 44 | char fmt[strlen(format) + 1]; |
| 45 | strcpy(fmt, format); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 46 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 47 | char* ret = const_cast<char*>(s); |
| 48 | char* cp; |
| 49 | for (char* f = cp = fmt;; ++cp) { |
| 50 | if (!*cp) { |
| 51 | if (f != cp) { |
| 52 | ret = ::strptime(ret, f, ptm); |
| 53 | } |
| 54 | break; |
| 55 | } |
| 56 | if (*cp != '%') { |
| 57 | continue; |
| 58 | } |
| 59 | char* e = cp; |
| 60 | ++e; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 61 | #if (defined(__BIONIC__)) |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 62 | if (*e == 's') { |
| 63 | *cp = '\0'; |
| 64 | if (*f) { |
| 65 | ret = ::strptime(ret, f, ptm); |
| 66 | if (!ret) { |
| 67 | break; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 68 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 69 | } |
| 70 | tv_sec = 0; |
| 71 | while (isdigit(*ret)) { |
| 72 | tv_sec = tv_sec * 10 + *ret - '0'; |
| 73 | ++ret; |
| 74 | } |
| 75 | now = tv_sec; |
| 76 | #if !defined(_WIN32) |
| 77 | ptm = localtime_r(&now, &tmBuf); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 78 | #else |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 79 | ptm = localtime(&now); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 80 | #endif |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 81 | } else |
| 82 | #endif |
| 83 | { |
| 84 | unsigned num = 0; |
| 85 | while (isdigit(*e)) { |
| 86 | num = num * 10 + *e - '0'; |
| 87 | ++e; |
| 88 | } |
| 89 | if (*e != 'q') { |
| 90 | continue; |
| 91 | } |
| 92 | *cp = '\0'; |
| 93 | if (*f) { |
| 94 | ret = ::strptime(ret, f, ptm); |
| 95 | if (!ret) { |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | unsigned long mul = NS_PER_SEC; |
| 100 | if (num == 0) { |
| 101 | num = INT_MAX; |
| 102 | } |
| 103 | tv_nsec = 0; |
| 104 | while (isdigit(*ret) && num && (mul > 1)) { |
| 105 | --num; |
| 106 | mul /= 10; |
| 107 | tv_nsec = tv_nsec + (*ret - '0') * mul; |
| 108 | ++ret; |
| 109 | } |
| 110 | } |
| 111 | f = cp = e; |
| 112 | ++f; |
| 113 | } |
| 114 | |
| 115 | if (ret) { |
| 116 | tv_sec = mktime(ptm); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 117 | return ret; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Upon error, place a known value into the class, the current time. |
| 121 | #ifdef __linux__ |
| 122 | *this = log_time(CLOCK_REALTIME); |
| 123 | #else |
| 124 | time(&now); |
| 125 | tv_sec = now; |
| 126 | tv_nsec = 0; |
| 127 | #endif |
| 128 | return ret; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 129 | } |