1 /*
2 * Copyright (c) 2025 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 #include "event_param_watcher.h"
16
17 #include "event_validator.h"
18 #include "hiview_logger.h"
19 #include "parameter_ex.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_TAG("EventParamWatcher");
24 namespace {
ParameterWatchCallback(const char * key,const char * value,void * context)25 void ParameterWatchCallback(const char* key, const char* value, void* context)
26 {
27 if (context == nullptr) {
28 HIVIEW_LOGE("context is null");
29 return;
30 }
31 auto watcher = reinterpret_cast<EventParamWatcher*>(context);
32 if (watcher == nullptr) {
33 HIVIEW_LOGE("watcher is null");
34 return;
35 }
36 constexpr size_t maxLen = 256;
37 std::string testTypeStr(value);
38 if (testTypeStr.size() > maxLen) {
39 HIVIEW_LOGE("invalid length, strLen=%{public}zu, maxLen=%{public}zu", testTypeStr.size(), maxLen);
40 return;
41 }
42 watcher->UpdateTestType(testTypeStr);
43 HIVIEW_LOGI("test_type is set to be \"%{public}s\"", testTypeStr.c_str());
44 }
45 }
46
Init()47 void EventParamWatcher::Init()
48 {
49 constexpr char testTypeParamKey[] = "hiviewdfx.hiview.testtype";
50 if (Parameter::WatchParamChange(testTypeParamKey, ParameterWatchCallback, this) != 0) {
51 HIVIEW_LOGW("failed to watch the change of parameter %{public}s", testTypeParamKey);
52 }
53 }
54
GetTestType()55 std::string EventParamWatcher::GetTestType()
56 {
57 std::shared_lock<std::shared_mutex> lock(testTypeMutex_);
58 return testType_;
59 }
60
UpdateTestType(const std::string & testType)61 void EventParamWatcher::UpdateTestType(const std::string& testType)
62 {
63 std::unique_lock<std::shared_mutex> lock(testTypeMutex_);
64 testType_ = testType;
65 }
66 } // namespace HiviewDFX
67 } // namespace OHOS
68