1 /*
2 * Copyright (c) 2022-2023 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
16 #include "b_error/b_error.h"
17
18 #include <algorithm>
19 #include <sstream>
20 #include <sys/syscall.h>
21
22 #include "dfx_dump_catcher.h"
23 #include "filemgmt_libhilog.h"
24
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27
WrapMessageWithExtraInfos(const char * fileName,int lineNo,const char * functionName,Codes code,const vector<string_view> & msgs) const28 string BError::WrapMessageWithExtraInfos(const char *fileName,
29 int lineNo,
30 const char *functionName,
31 Codes code,
32 const vector<string_view> &msgs) const
33 {
34 stringstream ss;
35 ss << '[' << fileName << ':' << lineNo << " -> " << functionName << ']' << ' ';
36 for (size_t i = 0; i < msgs.size(); ++i) {
37 ss << msgs[i];
38 if (i != msgs.size() - 1) {
39 ss << ". ";
40 }
41 }
42
43 if (code != Codes::OK) {
44 string msg;
45 HiviewDFX::DfxDumpCatcher().DumpCatch(getpid(), syscall(SYS_gettid), msg);
46 ss << endl << msg;
47 }
48
49 string res = ss.str();
50 HiviewDFX::HiLog::Error(FILEMGMT_LOG_LABEL, "%{public}s", res.c_str());
51 return res;
52 }
53
GetCode() const54 int BError::GetCode() const
55 {
56 int code = static_cast<int>(GetRawCode());
57 if (errCodeTable_.find(code) != errCodeTable_.end()) {
58 return errCodeTable_.at(code);
59 }
60 HILOGE("Unknown code : %{public}d", code);
61 return BackupErrorCode::E_UKERR;
62 }
63 } // namespace OHOS::FileManagement::Backup