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 import colorlog;
19 
20 /// Log information of a cursor.
21 void logNode(scope const Cursor c, const int indent = 0,
22         const string func = __FUNCTION__, const uint line = __LINE__) @trusted {
23     import std.array : array;
24     import std.range : repeat;
25     import logger = std.experimental.logger;
26     import clang.Cursor : dump;
27     import clang.info : abilities;
28 
29     // dfmt off
30     debug {
31         string indent_ = repeat(' ', indent).array();
32         auto loc = c.location;
33         log!"libclang_ast".logf!(-1, "", "", "", "")
34             (logger.LogLevel.trace,
35              "%d %s%s|%s|%s|%s:%d:%d [%s:%d]",
36              indent,
37              indent_,
38              dump(c),
39              c.displayName,
40              abilities(c),
41              loc.file,
42              loc.spelling.line,
43              loc.spelling.column,
44              func,
45              line);
46     }
47     // dfmt on
48 }
49 
50 /// logNode can't take a rvalue so creating a temporary and then logging.
51 /// -3 because there are 3 lines until the call to logNode. By subtracting it is
52 /// kept semantic equivalent to the mixin line.
53 template mixinNodeLog() {
54     enum mixinNodeLog = q{debug {
55             {
56                 auto c = v.cursor;
57                 logNode(c, indent, __FUNCTION__, __LINE__-3);
58             }
59         }
60     };
61 }