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  * UnleinfoStream 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 expreinfoStream or implied.
12  * See the License for the specific language governing permiinfoStreamions and
13  * limitations under the License.
14  */
15 
16 #include "backup_log_utils.h"
17 
18 #include <sstream>
19 
20 #include "backup_file_utils.h"
21 #include "backup_log_const.h"
22 
23 namespace OHOS::Media {
24 template<typename Key, typename Value>
GetValueFromMap(const std::unordered_map<Key,Value> & map,const Key & key,const Value & defaultValue=Value ())25 Value GetValueFromMap(const std::unordered_map<Key, Value> &map, const Key &key, const Value &defaultValue = Value())
26 {
27     auto it = map.find(key);
28     if (it == map.end()) {
29         return defaultValue;
30     }
31     return it->second;
32 }
33 
FileInfoToString(int32_t sceneCode,const FileInfo & info,const std::vector<std::string> & extendList)34 std::string BackupLogUtils::FileInfoToString(int32_t sceneCode, const FileInfo &info,
35     const std::vector<std::string> &extendList)
36 {
37     // [id], size, media_type, display_name, package_name, path
38     std::stringstream infoStream;
39     infoStream << "FileInfo[";
40     infoStream << (sceneCode >= CLONE_RESTORE_ID ? info.fileIdOld : info.localMediaId);
41     infoStream << ";" << info.fileSize;
42     infoStream << ";" << info.fileType;
43     infoStream << ";" << BackupFileUtils::GarbleFileName(info.displayName);
44     infoStream << ";" << BackupFileUtils::GarbleFileName(info.packageName);
45     infoStream << ";" << BackupFileUtils::GarbleFilePath(info.oldPath, DEFAULT_RESTORE_ID);
46     for (const auto &extend : extendList) {
47         if (extend.empty()) {
48             continue;
49         }
50         infoStream << ";" << extend;
51     }
52     infoStream << "]";
53     return infoStream.str();
54 }
55 
ErrorInfoToString(const ErrorInfo & info)56 std::string BackupLogUtils::ErrorInfoToString(const ErrorInfo &info)
57 {
58     // error, count, status
59     std::stringstream infoStream;
60     infoStream << "ErrorInfo[";
61     infoStream << RestoreErrorToString(info.error);
62     infoStream << ";" << info.count;
63     infoStream << ";" << info.status;
64     infoStream << ";" << info.extend;
65     infoStream << "]";
66     return infoStream.str();
67 }
68 
RestoreErrorToString(int32_t error)69 std::string BackupLogUtils::RestoreErrorToString(int32_t error)
70 {
71     return GetValueFromMap(RESTORE_ERROR_MAP, error);
72 }
73 
Format(const std::string & infoString)74 std::string BackupLogUtils::Format(const std::string &infoString)
75 {
76     std::stringstream infoStream;
77     infoStream << "\"";
78     for (char infoChar : infoString) {
79         if (infoChar == '\"') {
80             infoStream << "\"\"";
81         } else {
82             infoStream << infoChar;
83         }
84     }
85     infoStream << "\"";
86     return infoStream.str();
87 }
88 
FileDbCheckInfoToString(const FileDbCheckInfo & info)89 std::string BackupLogUtils::FileDbCheckInfoToString(const FileDbCheckInfo &info)
90 {
91     // dbType, dbStatus, fileStatus
92     std::stringstream infoStream;
93     infoStream << "FileDbCheckInfo[";
94     infoStream << info.dbType;
95     infoStream << ";" << info.dbStatus;
96     infoStream << ";" << info.fileStatus;
97     infoStream << "]";
98     return infoStream.str();
99 }
100 } // namespace OHOS::Media