1 /**
2 Copyright: Copyright (c) 2017-2021, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 
10 This file contains convenient functions for logging some meta data about a
11 cursor for debugging purpose.
12 */
13 module libclang_ast.cursor_logger;
14 
15 import clang.Cursor : Cursor;
16 import clang.Visitor : Visitor;
17 
18 /// Log information of a cursor.
19 void logNode(ref const Cursor c, const int indent = 0, const string func = __FUNCTION__,
20         const uint line = __LINE__) @trusted {
21     import std.array : array;
22     import std.range : repeat;
23     import logger = std.experimental.logger;
24     import clang.Cursor : dump;
25     import clang.info : abilities;
26 
27     // dfmt off
28     debug {
29         string indent_ = repeat(' ', indent).array();
30         auto loc = c.location;
31         logger.logf!(-1, "", "", "", "")
32             (logger.LogLevel.trace,
33              "%d %s%s|%s|%s|%s:%d:%d [%s:%d]",
34              indent,
35              indent_,
36              dump(c),
37              c.displayName,
38              abilities(c),
39              loc.file,
40              loc.spelling.line,
41              loc.spelling.column,
42              func,
43              line);
44     }
45     // dfmt on
46 }
47 
48 /// logNode can't take a rvalue so creating a temporary and then logging.
49 /// -3 because there are 3 lines until the call to logNode. By subtracting it is
50 /// kept semantic equivalent to the mixin line.
51 template mixinNodeLog() {
52     enum mixinNodeLog = q{debug {
53             {
54                 auto c = v.cursor;
55                 logNode(c, indent, __FUNCTION__, __LINE__-3);
56             }
57         }
58     };
59 }