1 /* 2 * Copyright 2018 Rockchip Electronics Co. LTD 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 * author: martin.cheng@rock-chips.com 17 * date: 2018/07/05 18 */ 19 20 #ifndef INCLUDE_RT_BASE_RT_TIME_H_ 21 #define INCLUDE_RT_BASE_RT_TIME_H_ 22 23 #include "rt_header.h" // NOLINT 24 #include "rt_log.h" // NOLINT 25 26 class RtTime { 27 public: 28 struct DateTime { 29 UINT16 mYear; //!< e.g. 2005 30 UINT8 mMonth; //!< 1..12 31 UINT8 mDayOfWeek; //!< 0..6, 0==Sunday 32 UINT8 mDay; //!< 1..31 33 UINT8 mHour; //!< 0..23 34 UINT8 mMinute; //!< 0..59 35 UINT8 mSecond; //!< 0..59 36 }; 37 38 static void getDateTime(DateTime*); 39 static UINT64 getNowTimeMs(); 40 static UINT64 getNowTimeUs(); 41 static UINT64 getRelativeTimeMs(); 42 static UINT64 getRelativeTimeUs(); 43 static void sleepMs(UINT64 time); 44 static void sleepUs(UINT64 time); 45 static INT32 randInt(); 46 }; 47 48 class RtAutoTimeoutLog { 49 public: 50 // The label is not deep-copied, so its address must remain valid for the 51 // lifetime of this object 52 inline RtAutoTimeoutLog(const char* label = RT_NULL, 53 UINT64 timeout_ms = 0) mLabel(label)54 : mLabel(label) { 55 mNow = RtTime::getNowTimeMs(); 56 mTimeOut = timeout_ms; 57 } ~RtAutoTimeoutLog()58 inline ~RtAutoTimeoutLog() { 59 UINT64 duration = RtTime::getNowTimeMs() - mNow; 60 if (duration >= mTimeOut) { 61 (void)mLabel; 62 // RT_LOGE("%s [perf:%lld ms]\n", mLabel ? mLabel : "", duration); 63 } 64 } 65 private: 66 const char* mLabel; 67 UINT64 mNow; 68 UINT64 mTimeOut; 69 }; 70 71 #endif // INCLUDE_RT_BASE_RT_TIME_H_ 72