• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "minddata/dataset/util/md_log_adapter.h"
18 
19 #include <sstream>
20 
21 namespace mindspore {
22 namespace dataset {
Apply(Status * rc)23 Status MDLogAdapter::Apply(Status *rc) {
24   std::string status_msg = ConstructMsg(rc->StatusCode(), rc->CodeAsString(rc->StatusCode()), "", rc->GetLineOfCode(),
25                                         rc->GetFileName(), rc->GetErrDescription());
26   rc->SetStatusMsg(status_msg);
27   return *rc;
28 }
29 
ConstructMsg(const enum StatusCode & status_code,const std::string & code_as_string,const std::string & status_msg,const int line_of_code,const std::string & file_name,const std::string & err_description)30 std::string MDLogAdapter::ConstructMsg(const enum StatusCode &status_code, const std::string &code_as_string,
31                                        const std::string &status_msg, const int line_of_code,
32                                        const std::string &file_name, const std::string &err_description) {
33   std::ostringstream ss;
34   std::string kSplitLine = std::string(66, '-') + "\n";
35   std::string err_ori = err_description;
36 
37   /// Python Runtime Error
38   ss << code_as_string << ". \n\n";
39 
40   /// Internal Error
41   if (err_description.find("[Internal ERROR]") != std::string::npos) {
42     ss << kSplitLine << "- Framework Unexpected Exception Raised: \n" << kSplitLine;
43     ss << "This exception is caused by framework's unexpected error. Please create an issue at "
44        << "https://gitee.com/mindspore/mindspore/issues to get help.\n\n";
45   }
46 
47   /// Python Stack
48   std::string user_err;
49   std::string user_stack;
50   if (status_code == StatusCode::kMDPyFuncException) {
51     std::string at_stack = "\n\nAt:\n";
52     if (err_ori.find(at_stack) != std::string::npos) {
53       user_stack = err_ori.substr(0, err_ori.find(at_stack));
54       user_err = "Execute user Python code failed, check 'Python Call Stack' above.";
55       ss << kSplitLine << "- Python Call Stack: \n" << kSplitLine;
56       ss << user_stack << "\n\n";
57     } else {
58       user_err = err_ori;
59     }
60   }
61 
62   /// Summary Message
63   ss << kSplitLine << "- Dataset Pipeline Error Message: \n" << kSplitLine;
64   if (!user_err.empty()) {
65     ss << "[ERROR] " + user_err + "\n\n";
66   } else {
67     user_err = err_description;
68     if (*user_err.rbegin() != '.') {
69       user_err += '.';
70     }
71     ss << "[ERROR] " + user_err + "\n\n";
72   }
73 
74   /// C++ Stack
75   if (!file_name.empty()) {
76     ss << kSplitLine << "- C++ Call Stack: (For framework developers) \n" << kSplitLine;
77     std::string cpp_trace = std::string(file_name) + "(" + std::to_string(line_of_code) + ").\n";
78     ss << cpp_trace << "\n\n";
79   }
80 
81   return ss.str();
82 }
83 }  //  namespace dataset
84 }  //  namespace mindspore
85