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