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 longTimeSec_ = ONE_WEEK_TIME * ONE_MINUTE * ONE_MINUTE;
51 RINGTONE_ERR_LOG("get logTimeSec failed");
52 }
53 } else {
54 longTimeSec_ = ONE_WEEK_TIME * ONE_MINUTE * ONE_MINUTE;
55 RINGTONE_ERR_LOG("dfxLongTime is not number");
56 }
57 }
58
~DfxWorker()59 DfxWorker::~DfxWorker()
60 {
61 }
62
Init()63 void DfxWorker::Init()
64 {
65 RINGTONE_INFO_LOG("init");
66 thread(bind(&DfxWorker::InitCycleThread, this)).detach();
67 }
68
InitCycleThread()69 void DfxWorker::InitCycleThread()
70 {
71 RINGTONE_INFO_LOG("DFX start");
72 int32_t errCode;
73 shared_ptr<NativePreferences::Preferences> prefs =
74 NativePreferences::PreferencesHelper::GetPreferences(DFX_COMMON_XML, errCode);
75 if (!prefs) {
76 RINGTONE_ERR_LOG("get preferences error: %{public}d", errCode);
77 return;
78 }
79 lastReportTime_ = prefs->GetLong(LAST_REPORT_TIME, 0);
80 if (RingtoneFileUtils::UTCTimeSeconds() - lastReportTime_ > longTimeSec_) {
81 RINGTONE_INFO_LOG("Report Xml");
82 lastReportTime_ = DfxManager::GetInstance()->HandleReportXml();
83 prefs->PutLong(LAST_REPORT_TIME, lastReportTime_);
84 prefs->FlushSync();
85 }
86 RINGTONE_INFO_LOG("DFX end");
87 }
88 } // namespace Media
89 } // namespace OHOS
90