File: programming/cocoa/

//  UKCrashReporter.m
//  NiftyFeatures
//  Created by Uli Kusterer on Sat Feb 04 2006.
//  Copyright (c) 2006 M. Uli Kusterer. All rights reserved.
// -----------------------------------------------------------------------------
//	Headers:
// -----------------------------------------------------------------------------
#import "UKCrashReporter.h"
#import "UKSystemInfo.h"
#import <AddressBook/AddressBook.h>
// -----------------------------------------------------------------------------
//	UKCrashReporterCheckForCrash:
//		This submits the crash report to a CGI form as a POST request by
//		passing it as the request variable "crashlog".
//		KNOWN LIMITATION:	If the app crashes several times in a row, only the
//							last crash report will be sent because this doesn't
//							walk through the log files to try and determine the
//							dates of all reports.
//		This is written so it works back to OS X 10.2, or at least gracefully
//		fails by just doing nothing on such older OSs. This also should never
//		throw exceptions or anything on failure. This is an additional service
//		for the developer and *mustn't* interfere with regular operation of the
//		application.
// -----------------------------------------------------------------------------
// Try whether the classes we need to talk to the CGI are present:
"NSMutableURLRequest""NSURLConnection"// Get the log file, its last change date and last report date:
"CFBundleExecutable""~/Library/Logs/CrashReporter/"".crash.log""UKCrashReporterLastCrashReportDate"// We have a crash log file and its mod date? Means we crashed sometime in the past.
			// If we never before reported a crash or the last report lies before the last crash:
// Fetch the newest report from the log:
"\n\n**********\n\n""*** Couldn't read Report ***";	// 1 since report 0 is empty (file has a delimiter at the top).
"""%dx ",numCores];
				// Create a string containing Mac and CPU info, crash log and prefs:
"Model: %@\nCPU Speed: %@%.2f GHz\n%@\n\nPreferences:\n%@"// Now show a crash reporter window so the user can edit the info to send:
"Error during check for crash: %@""%@_"".crash"// Find the newest of our crash log files:
// In super init the awakeFromNib method gets called, so we can not
	//	use ivars to transfer the log, and use a global instead:
// Insert the app name into the explanation message:
"FEEDBACK_EXPLANATION_TEXT",@"UKCrashReporter",@"""%%APPNAME"// Insert user name and e-mail address into the information field:
"FEEDBACK_MESSAGE_TEXT",@"UKCrashReporter",@"""%%LONGUSERNAME""MISSING_EMAIL_ADDRESS",@"UKCrashReporter",@"""%%EMAILADDRESS"// Show the crash log to the user:
"de.zathras.ukcrashreporter.crashlog-tab""""%dx ""SYSTEM_INFO_TAB_NAME",@"UKCrashReporter",@"""Application: %@ %@\nModel: %@\nCPU Speed: %@%.2f GHz\nSystem Version: %@\n\nPreferences:\n%@""CFBundleVersion"// Show the window:
"0xKhTmLbOuNdArY""\n==========\n""USED_TO_BE_KHTMLBOUNDARY"// Prepare a request:
"CRASH_REPORT_CGI_URL", @"UKCrashReporter", @"""multipart/form-data; boundary=%@""UKCrashReporter";
	// Add form trappings to crashReport:
"--%@\r\nContent-Disposition: form-data; name=\"crashlog\"\r\n\r\n""\r\n--%@--\r\n"// setting the headers:
	[postRequest setHTTPMethod: @"POST""Content-Type""User-Agent""%lu""Content-Length"// Go into progress mode and kick off the HTTP post:
// Remember we already did this crash, so we don't ask twice:
		NSRunAlertPanel( errTitle, @"%@""COULDNT_SEND_CRASH_REPORT_ERROR_OK",@"UKCrashReporter",@""), @"", @""// Now that we successfully sent this crash, don't report it again:

This code uses the PclZip Zip File reading code, which is subject to the GNU LGPL. It also uses the GeSHi syntax highlighter, subject to the GPL. Ask if you want this for your own web site, it's free.