1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define MLOG_TAG "DfxWorker"
16
17 #include "dfx_worker.h"
18
19 #include <charconv>
20 #include "dfx_const.h"
21 #include "dfx_manager.h"
22 #include "parameters.h"
23 #include "preferences_helper.h"
24 #include "ringtone_file_utils.h"
25 #include "ringtone_log.h"
26 #include "ringtone_utils.h"
27
28 namespace OHOS {
29 namespace Media {
30 using namespace std;
31 shared_ptr<DfxWorker> DfxWorker::dfxWorkerInstance_{nullptr};
32
GetInstance()33 shared_ptr<DfxWorker> DfxWorker::GetInstance()
34 {
35 if (dfxWorkerInstance_ == nullptr) {
36 dfxWorkerInstance_ = make_shared<DfxWorker>();
37 }
38 return dfxWorkerInstance_;
39 }
40
DfxWorker()41 DfxWorker::DfxWorker()
42 {
43 string dfxLongTime = system::GetParameter("persist.multimedia.ringtonelibrary.dfx.longtime", ONE_WEEK);
44 int32_t value = 0;
45 if (RingtoneUtils::IsNumber(dfxLongTime)) {
46 auto [ptr, ec] = std::from_chars(dfxLongTime.data(), dfxLongTime.data() + dfxLongTime.size(), value);
47 if (ec == std::errc{} && ptr == dfxLongTime.data() + dfxLongTime.size()) {
48 longTimeSec_ = value * ONE_MINUTE * ONE_MINUTE;
49 } else {
50 RINGTONE_ERR_LOG("get logTimeSec failed");
51 }
52 } else {
53 RINGTONE_ERR_LOG("dfxLongTime is not number");
54 }
55 }
56
~DfxWorker()57 DfxWorker::~DfxWorker()
58 {
59 }
60
Init()61 void DfxWorker::Init()
62 {
63 RINGTONE_INFO_LOG("init");
64 thread(bind(&DfxWorker::InitCycleThread, this)).detach();
65 }
66
InitCycleThread()67 void DfxWorker::InitCycleThread()
68 {
69 RINGTONE_INFO_LOG("DFX start");
70 int32_t errCode;
71 shared_ptr<NativePreferences::Preferences> prefs =
72 NativePreferences::PreferencesHelper::GetPreferences(DFX_COMMON_XML, errCode);
73 if (!prefs) {
74 RINGTONE_ERR_LOG("get preferences error: %{public}d", errCode);
75 return;
76 }
77 lastReportTime_ = prefs->GetLong(LAST_REPORT_TIME, 0);
78 if (RingtoneFileUtils::UTCTimeSeconds() - lastReportTime_ > longTimeSec_) {
79 RINGTONE_INFO_LOG("Report Xml");
80 lastReportTime_ = DfxManager::GetInstance()->HandleReportXml();
81 prefs->PutLong(LAST_REPORT_TIME, lastReportTime_);
82 prefs->FlushSync();
83 }
84 RINGTONE_INFO_LOG("DFX end");
85 }
86 } // namespace Media
87 } // namespace OHOS
88