1 /**
2 Module with some usefull functions
3 */
4 module mutils.job_manager.utils;
5 
6 import std.traits;
7 import std.stdio:writefln,writeln;
8 
9 // Casts @nogc out of a function or delegate type.
10 auto assumeNoGC(T) (T t) if (isFunctionPointer!T || isDelegate!T)
11 {
12 	enum attrs = functionAttributes!T | FunctionAttribute.nogc;
13 	return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
14 }
15 
16 void writelnng(T...)(T args){
17 	assumeNoGC( (T arg){writeln(arg);})(args);
18 }
19 
20 void assertM(A,B,string file=__FILE__,uint line=__LINE__)(A a,B b){
21 	if(a!=b){
22 		writefln("File: %s:%s  A: %s, B: %s",file,line,a,b);
23 		assert(a==b);
24 	}
25 }
26 
27 
28 
29 version(linux){
30 	import std.conv;
31 	import std.demangle;
32 	private static struct  Dl_info {
33 		const char *dli_fname; 
34 		void       *dli_fbase;  
35 		const char *dli_sname;  
36 		void       *dli_saddr; 
37 	}
38 	private extern(C) int dladdr(void *addr, Dl_info *info);
39 	
40 	string functionName(void* addr){
41 		Dl_info info;
42 		int ret=dladdr(addr,&info);
43 		return info.dli_sname.to!(string).demangle;
44 	}
45 }else{
46 	string functionName(void* addr){
47 		return "???";
48 	}
49 }
50 
51 void printException(Exception e, int maxStack = 40) {
52 	writeln("Exception message: %s", e.msg);
53 	writefln("File: %s Line Number: %s", e.file, e.line);
54 	writeln("Call stack:");
55 	foreach (i, b; e.info) {
56 		writeln(b);
57 		if (i >= maxStack)
58 			break;
59 	}
60 	writeln("--------------");
61 }
62 void printStack(){
63 	static immutable Exception exc=new Exception("Dummy");
64 	try{
65 		throw exc;
66 	}catch(Exception e ){
67 		printException(e);
68 	}
69 }