1 //Written in the D programming language
2 
3 /*
4  * Sample program to demonstrate Backtrace.
5  *
6  * Written by: Jason den Dulk.
7  */
8 
9 module sample;
10 
11 import std.conv;
12 import std.string;
13 import std.array;
14 import std.algorithm;
15 import std.stdio;
16 
17 import backtrace;
18 
19 void toStdout() {
20   writeln("#");
21   writeln("# Pretty trace direct to stdout.");
22   writeln("#");
23   printPrettyTrace();
24 }
25 
26 void toStderr() {
27   writeln("#");
28   writeln("# Pretty trace direct to stderr.");
29   writeln("#");
30   printPrettyTrace(stderr);
31 }
32 
33 void asString() {
34   writeln("#");
35   writeln("# Pretty trace as a string");
36   writeln("#");
37   write(prettyTrace());
38 }
39 
40 void onException() {
41   writeln("#");
42   writeln("# Pretty trace after an exception");
43   writeln("#");
44   throw new Exception("FAIL!");
45 }
46 
47 void onError() {
48   writeln("#");
49   writeln("# Pretty trace after an error");
50   writeln("#");
51   int[] x = [1,2,3];
52   int y = x[5]; // Range violation;
53 }
54 
55 void main() {
56   backtrace.install();
57   writeln("### Backtrace Sample Demo. ###");
58   toStdout();
59   toStderr();
60   asString();
61   try {
62     onException();
63   } catch (Exception e) {
64     writeln("# Line by line");
65     foreach (i,l;e.info)
66       writeln(i,": ",l);
67     writeln("# via Throwable.toString()");
68     write(e.toString());
69   }
70   onError();
71 }