• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "printing.h"
17 #include "mir_module.h"
18 #include "types_def.h"
19 
20 namespace maple {
21 const std::string kBlankString = "                                                                                ";
22 constexpr int kIndentunit = 2;  // number of blank chars of each indentation
23 
PrintIndentation(int32 indent)24 void PrintIndentation(int32 indent)
25 {
26     int64 indentAmount = static_cast<int64>(indent) * kIndentunit;
27     do {
28         LogInfo::MapleLogger() << kBlankString.substr(0, indentAmount);
29         indentAmount -= static_cast<int64>(kBlankString.length());
30     } while (indentAmount > 0);
31 }
32 
PrintString(const std::string & str)33 void PrintString(const std::string &str)
34 {
35     size_t i = 0;
36     LogInfo::MapleLogger() << " \"";
37     while (i < str.length()) {
38         unsigned char c = str[i++];
39         // differentiate printable and non-printable charactors
40         if (c >= 0x20 && c <= 0x7e) {
41             // escape "
42             switch (c) {
43                 case '"':
44                     LogInfo::MapleLogger() << "\\\"";
45                     break;
46                 case '\\':
47                     LogInfo::MapleLogger() << "\\\\";
48                     break;
49                 default:
50                     LogInfo::MapleLogger() << c;
51                     break;
52             }
53         } else {
54             constexpr int kFieldWidth = 2;
55             LogInfo::MapleLogger() << "\\x" << std::hex << std::setfill('0') << std::setw(kFieldWidth)
56                                    << static_cast<unsigned int>(c) << std::dec;
57         }
58     }
59     LogInfo::MapleLogger() << "\"";
60 }
61 }  // namespace maple
62