summary refs log tree commit diff stats
path: root/compiler/debugutils.nim
blob: d109d2121ba69c64e19081774daf4611ab7ccd00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
##[
Utilities to help with debugging nim compiler.

Experimental API, subject to change.
]##

#[
## example
useful debugging flags:
--stacktrace -d:debug -d:nimDebugUtils
 nim c -o:bin/nim_temp --stacktrace -d:debug -d:nimDebugUtils compiler/nim

## future work
* expose and improve astalgo.debug, replacing it by std/prettyprints,
  refs https://github.com/nim-lang/RFCs/issues/385
]#

import options
import std/wrapnils
export wrapnils
  # allows using things like: `?.n.sym.typ.len`

import std/stackframes
export stackframes
  # allows using things like: `setFrameMsg c.config$n.info & " " & $n.kind`
  # which doesn't log, but augments stacktrace with side channel information

var conf0: ConfigRef

proc onNewConfigRef*(conf: ConfigRef) {.inline.} =
  ## Caches `conf`, which can be retrieved with `getConfigRef`.
  ## This avoids having to forward `conf` all the way down the call chain to
  ## procs that need it during a debugging session.
  conf0 = conf

proc getConfigRef*(): ConfigRef =
  ## nil, if -d:nimDebugUtils wasn't specified
  result = conf0

proc isCompilerDebug*(): bool =
  ##[
  Provides a simple way for user code to enable/disable logging in the compiler
  in a granular way. This can then be used in the compiler as follows:
  ```nim
  if isCompilerDebug():
    echo ?.n.sym.typ.len
  ```
  ]##
  runnableExamples:
    proc main =
      echo 2
      {.define(nimCompilerDebug).}
      echo 3.5 # code section in which `isCompilerDebug` will be true
      {.undef(nimCompilerDebug).}
      echo 'x'
  conf0.isDefined("nimCompilerDebug")
pan class="nv">$post = $json_data->[0]->{data}->{children}->[0]->{data}; # Start the Org document. print "#+", "STARTUP:content\n"; # Print the post title. print "* ", "$post->{title}\n"; # Add various details to :PROPERTIES:. print ":PROPERTIES:\n"; foreach my $detail (qw( subreddit created_utc author permalink upvote_ratio ups downs score )) { print ":${detail}: =$post->{$detail}=\n" if scalar $post->{$detail}; } print ":END:\n"; # Add selftext/url if present. print "\n#+BEGIN_SRC markdown\n", " ", $wrapper->wrap($post->{selftext}) =~ s/\n/\n\ /gr, "#+END_SRC\n" if scalar $post->{selftext}; print "$post->{url}\n" if scalar $post->{selftext}; # $comments contains comment data. We are interested in: replies, # author, body, created_utc & permalink. my $comments = $json_data->[1]->{data}->{children}; # Iterate over top-level comments. foreach my $comment ($comments->@*) { print_comment_chain($comment->{data}, 0); } # print_comment_chain will print the whole chain of comment while # accounting for level. sub print_comment_chain { my $comment = shift @_; my $level = shift @_; print "*" x ($level + 2), " ", "$comment->{author}\n"; # Print comment details. print ":PROPERTIES:\n"; foreach my $detail (qw( created_utc author permalink upvote_ratio ups downs score edited is_submitter stickied controversiality )) { print ":${detail}: =$comment->{$detail}=\n" if scalar $comment->{$detail}; } print ":END:\n"; print "\n#+BEGIN_SRC markdown\n", " ", $wrapper->wrap($comment->{body}) =~ s/\n/\n\ /gr, "#+END_SRC\n"; # If the comment has replies then iterate over those too. if (scalar $comment->{replies}) { foreach my $reply ($comment->{replies}->{data}->{children}->@*) { print_comment_chain($reply->{data}, $level + 1); } } }