File: mnesia_core_dump_viewer

package info (click to toggle)
erlang 1%3A25.2.3%2Bdfsg-1%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,972 kB
  • sloc: erlang: 1,440,803; xml: 473,412; ansic: 392,382; cpp: 164,287; makefile: 17,392; sh: 13,842; lisp: 9,675; java: 8,578; asm: 6,426; perl: 5,527; python: 5,469; javascript: 610; pascal: 126; sed: 72; php: 3
file content (54 lines) | stat: -rwxr-xr-x 1,343 bytes parent folder | download | duplicates (17)
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%
%% Pretty print a Mnesia Core Dump
%% 
%% core_dump_viewer CoreFile [OutputFile]
%%
%% Status codes:
%%
%%  0 -> Ok
%%  1 -> Bad usage
%%  2 -> Cannot read Mnesia Core file
%%  3 -> Cannot write to file

-mode(compile).

main([CoreFile | OptOutputFile]) when CoreFile =/= "--help" ->
    %% io:format("Args: ~p\n", [init:get_arguments()]),
    case file:read_file(CoreFile) of
	{ok, Bin} ->
	    Core = binary_to_term(Bin),
	    PrettyCore = io_lib:format("~p\n", [Core]),
	    PrettyBin = list_to_binary(PrettyCore),
	    case OptOutputFile of
		[OutputFile] ->
		    case file:write_file(OutputFile, PrettyBin) of
			ok ->
			    ok;
			{error, Reason} ->
			    io:format("<ERROR> Cannot write to file ~p ~p\n",
				      [OutputFile, Reason]),
			    halt(3)
		    end;
		[] ->
		    io:format("~p\n", [Core]),
		    io:format("ok\n", []),
		    timer:sleep(timer:seconds(2)), % Poor mans flush of io buffers
		    init:stop();
		_ ->
		    usage()
	    end;
	{error, Reason} ->
	    io:format("<ERROR> Cannot read Mnesia Core file ~p ~p\n",
		      [CoreFile, Reason]),
	    halt(2)
    end;
main(_BadArgs) ->
    usage().

usage() ->
    FullName = escript:script_name(),
    BaseName = filename:basename(FullName),
    io:format("usage: ~s CoreFile [OutputFile]\n", [BaseName]),
    halt(1).