www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Automatilc e-mail crach reporting

reply BCS <BCS pathlink.com> writes:
/* * Hacked this together, though y'all might like it. <g>* */

module email;

private import std.date;
private import std.string;
private import std.socket;
private import std.socketstream;
//private import std.stdio;

/**
encapsulate an e-mail logging system.

usage:

declare at global scope:
	EmailMessage email;
	
as first lines in main:
	email = new EmailMessage(SMTPSrv, to, from, Sub);
	scope(failure) email.send();

as first line in every other function put:
	scope(failure)email.log(__FILE__,__LINE__,<some message>);

at other important points put in:
	email.log(__FILE__,__LINE__,<some message>);

*/
class EmailMessage
{
	char[] server, to, from, subject;

	char[][] message;
	int at;

	/**
	arg srv:	the outbound SMTP server
	arg t:		the to address
	arg f:		the from address
	arg sub:	the subject
	*/
	this(char[] srv, char[] t, char[] f, char[] sub)
	{
		server = srv.dup;
		to = t.dup;
		from = f.dup;
		subject = sub.dup;
		at=0;
	}

	/**
	arg file:	__FILE__
	arg line:	__LINE__
	agr msg:	the message to print
	*/
	void log(char[] file, int line, char[] msg)
	{
		if(message.length <= at)
			message.length = at+10;
		auto time = getUTCtime();

		message[at] = (
			file ~ ":" ~
			std.string.toString(line)~" "~
			toTimeString(time)~"("~
			std.string.toString(time%TicksPerSecond)~
			")  >"~msg).dup;
		at++;
	}

	/** sne off the message */
	void send()
	{
		Stream outs;
		auto sock = new Socket(AddressFamily.INET, SocketType.STREAM);
		sock.connect(new InternetAddress(server, 25));
		outs = new SocketStream(sock);

		outs.writeString("HELO none anon.org\r\n"
				"MAIL FROM:<"~from~">\r\n"
				"RCPT TO:<"~to~">\r\n"
				"DATA\r\n"
				"Subject: "~subject~\r\n
				"Content-Type: text/plain; charset=us-ascii\r\n\r\n");

		foreach(line; message[0..at])
		{
			if("." == line)
			{
				outs.writeString("..\r\n");
			}
			else
			{
				outs.writeString(line~\r\n);
			}
		}

		outs.writeString(".\r\nQUIT\r\n");
		outs.close;

		at = 0;
	}
}

/***EXAMPLE****/


private import std.compiler;

EmailMessage email;
void main()
{
	email = new EmailMessage("10.0.0.1", "sendto 10.0.0.1", 
"sendto 10.0.0.1", "a test of somthing");

	email.log(__FILE__,__LINE__,"Built "__DATE__" "__TIME__);
	email.log(__FILE__,__LINE__,std.compiler.name);
	scope(failure)
	{
		writef("sending error report\n");
		email.send();
	}

		email.log(__FILE__,__LINE__,"Global catch");
		writef("going\n");
		foo(30);
		writef("done\n");

}

void foo(int i)
{
	scope(failure)email.log(__FILE__,__LINE__,"foo catch");
	if(i) bar(i-1);
	throw new Error("foo");
}

void bar(int i)
{
	scope(failure)email.log(__FILE__,__LINE__,"foo catch");
	if(i) baz(i-1);
	throw new Error("foo");
}

void baz(int i)
{
	scope(failure)email.log(__FILE__,__LINE__,"foo catch");
	if(i) foo(i-1);
	throw new Error("foo");
}
Jun 13 2006
parent reply Georg Wrede <georg.wrede nospam.org> writes:
BCS wrote:
 /* * Hacked this together, though y'all might like it. <g>* */
...
 encapsulate an e-mail logging system.
Excellent! IMNSHO This ought to be in both Phobos and Ares.
Jun 15 2006
parent reply BCS <BCS pathlink.com> writes:
Georg Wrede wrote:
 BCS wrote:
 
 /* * Hacked this together, though y'all might like it. <g>* */
...
 encapsulate an e-mail logging system.
Excellent! IMNSHO This ought to be in both Phobos and Ares.
IMNSHO if it end up in there , it had better be *off* by default. version(EMAIL_DUMP) pragma(msg, "WARNING: E-mail crash reporting ON.") ... version(EMAIL_DUMP) scope(failure) email.log(... I can just see someone getting 100,000 emails from a one line bug, and of course someone will keep using the broken version until who only knowns when. Hmmm.. A timeout feature in send() might not be a bad idea ("don't send e-mails after..."). OTOH I think I might have posted a broken version. Could someone test it out and such?
Jun 15 2006
parent Georg Wrede <georg.wrede nospam.org> writes:
BCS wrote:
 Georg Wrede wrote:
 
 BCS wrote:

 /* * Hacked this together, though y'all might like it. <g>* */
...
 encapsulate an e-mail logging system.
Excellent! IMNSHO This ought to be in both Phobos and Ares.
IMNSHO if it end up in there , it had better be *off* by default.
True.
Jun 16 2006