1 /** 2 * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_DEBUG_RDR_BASE_RECORDER_H_ 17 #define MINDSPORE_CCSRC_DEBUG_RDR_BASE_RECORDER_H_ 18 #include <memory> 19 #include <string> 20 #include <sstream> 21 #include <chrono> 22 #include <iomanip> 23 #include "debug/common.h" 24 #include "debug/env_config_parser.h" 25 #include "mindspore/core/utils/log_adapter.h" 26 27 const int maxNameLength = 32; 28 namespace mindspore { 29 class BaseRecorder { 30 public: BaseRecorder()31 BaseRecorder() : module_(""), name_(""), directory_(""), filename_(""), timestamp_("") {} BaseRecorder(const std::string & module,const std::string & name)32 BaseRecorder(const std::string &module, const std::string &name) : module_(module), name_(name), filename_("") { 33 directory_ = mindspore::EnvConfigParser::GetInstance().RdrPath(); 34 35 if (name.length() > maxNameLength) { 36 name_ = name.substr(0, maxNameLength); 37 MS_LOG(WARNING) << "The name length is " << name.length() << ", exceeding the limit " << maxNameLength 38 << ". It will be intercepted as '" << name_ << "'."; 39 } 40 41 std::string err_msg = module_ + ":" + name_ + " set filename failed."; 42 if (!filename_.empty() && !Common::IsFilenameValid(filename_, MAX_FILENAME_LENGTH, err_msg)) { 43 filename_ = ""; 44 } 45 auto sys_time = GetTimeString(); 46 for (auto ch : sys_time) { 47 if (ch == '.') { 48 break; 49 } 50 if (ch != '-' && ch != ':') { 51 timestamp_.push_back(ch); 52 } 53 } 54 } ~BaseRecorder()55 virtual ~BaseRecorder() {} 56 GetModule()57 std::string GetModule() const { return module_; } GetName()58 std::string GetName() const { return name_; } GetTimeStamp()59 std::string GetTimeStamp() const { return timestamp_; } 60 std::optional<std::string> GetFileRealPath(const std::string &suffix = "") const; 61 Export()62 virtual void Export() {} UpdateInfo(const BaseRecorder & recorder)63 virtual void UpdateInfo(const BaseRecorder &recorder) {} 64 65 protected: 66 std::string module_; 67 std::string name_; 68 std::string directory_; 69 std::string filename_; 70 std::string timestamp_; // year,month,day,hour,minute,second 71 std::string delimiter_{"."}; 72 }; 73 using BaseRecorderPtr = std::shared_ptr<BaseRecorder>; 74 } // namespace mindspore 75 #endif // MINDSPORE_CCSRC_DEBUG_RDR_BASE_RECORDER_H_ 76