Skip to content

Instantly share code, notes, and snippets.

@benui-dev
Created May 12, 2014 15:18
Show Gist options
  • Save benui-dev/4ef118716880686c92e0 to your computer and use it in GitHub Desktop.
Save benui-dev/4ef118716880686c92e0 to your computer and use it in GitHub Desktop.
Hacky error reporter for Unity. Takes a screenshot and uploads it with system information to your server. Almost certainly super insecure.
using UnityEngine;
using System.Collections;
using System.Text;
public class ErrorReporter
{
static string m_postUrl = "http://www.yoursite.com/reports/report.cgi";
static string m_reportUrl = "http://www.yoursite.com/reports/";
// Take screenshot
// Send message, system info and screenshot to server
// Open page on uploaded report
//
// Call with StartCoroutine(DoReport("something"))
public static IEnumerator DoReport(string message)
{
Debug.Log("Reporting error");
string[] info = new string[] {
"ErrorMessage", message,
"Application.platform", Application.platform.ToString(),
"SystemInfo.deviceModel", SystemInfo.deviceModel,
"SystemInfo.deviceName", SystemInfo.deviceName,
"SystemInfo.deviceType", SystemInfo.deviceType.ToString(),
"SystemInfo.deviceUniqueIdentifier", SystemInfo.deviceUniqueIdentifier,
"SystemInfo.graphicsDeviceID", SystemInfo.graphicsDeviceID.ToString(),
"SystemInfo.graphicsDeviceName", SystemInfo.graphicsDeviceName,
"SystemInfo.graphicsDeviceVendor", SystemInfo.graphicsDeviceVendor,
"SystemInfo.graphicsDeviceVendorID", SystemInfo.graphicsDeviceVendorID.ToString(),
"SystemInfo.graphicsDeviceVersio", SystemInfo.graphicsDeviceVersion.ToString(),
"SystemInfo.graphicsMemorySize", SystemInfo.graphicsMemorySize.ToString(),
"SystemInfo.graphicsPixelFillrate", SystemInfo.graphicsPixelFillrate.ToString(),
"SystemInfo.graphicsShaderLevel", SystemInfo.graphicsShaderLevel.ToString(),
"SystemInfo.npotSupport", SystemInfo.npotSupport.ToString(),
"SystemInfo.operatingSystem", SystemInfo.operatingSystem,
"SystemInfo.processorCount", SystemInfo.processorCount.ToString(),
"SystemInfo.processorType", SystemInfo.processorType,
"SystemInfo.supportedRenderTargetCount", SystemInfo.supportedRenderTargetCount.ToString(),
"SystemInfo.supports3DTextures", SystemInfo.supports3DTextures.ToString(),
"SystemInfo.supportsAccelerometer", SystemInfo.supportsAccelerometer.ToString(),
"SystemInfo.supportsComputeShaders", SystemInfo.supportsComputeShaders.ToString(),
"SystemInfo.supportsGyroscope", SystemInfo.supportsGyroscope.ToString(),
"SystemInfo.supportsImageEffects", SystemInfo.supportsImageEffects.ToString(),
"SystemInfo.supportsInstancing", SystemInfo.supportsInstancing.ToString(),
"SystemInfo.supportsLocationService", SystemInfo.supportsLocationService.ToString(),
"SystemInfo.supportsRenderTextures", SystemInfo.supportsRenderTextures.ToString(),
"SystemInfo.supportsRenderToCubemap", SystemInfo.supportsRenderToCubemap.ToString(),
"SystemInfo.supportsShadows", SystemInfo.supportsShadows.ToString(),
"SystemInfo.supportsStencil", SystemInfo.supportsStencil.ToString(),
"SystemInfo.supportsVibration", SystemInfo.supportsVibration.ToString(),
"SystemInfo.systemMemorySize", SystemInfo.systemMemorySize.ToString(),
};
var form = new WWWForm();
//var builder = new StringBuilder(url);
//builder.Append("?").Append(info[0]).Append("=").Append(info[1]);
for (int i = 0; i < info.Length; i+=2)
{
//builder.Append("&").Append(info[i]).Append("=").Append(info[i + 1]);
form.AddField(info[i], info[i+1]);
}
//form.AddBinaryData("screenshot", TakeScreenshot());
yield return new WaitForEndOfFrame();
form.AddBinaryData("screenshot", TakeScreenshot(), "screenshot.png", "image/png");
var w = new WWW(m_postUrl, form);
yield return w;
if (!string.IsNullOrEmpty(w.error))
{
D.Error(w.error);
}
else
{
// Open URL for viewing
Application.OpenURL(m_reportUrl + w.text + ".html");
}
}
static byte[] TakeScreenshot()
{
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
return bytes;
}
}
#!/usr/bin/perl
use strict;
use CGI;
my $query = CGI->new;
print $query->header();
#print "Hello, world!";
my $id = 0;
my $id_filename = "id.txt";
open(my $id_fh, "<", $id_filename) or print($!);
$id = <$id_fh>;
print $id;
my $dir = 'shots';
my $screenshot_file = "$dir/$id.png";
#my $file = $query->param('screenshot');
open(my $out_shot_fh, ">$screenshot_file") or print "error $!";
my $file_handle = $query->upload('screenshot');
while(<$file_handle>) {
print $out_shot_fh $_;
}
close($file_handle);
close($out_shot_fh);
#my $data = $query->param('PUTDATA');
#open(my $screen_fh, ">", $id . ".png") or print($!);
#print $screen_fh $data;
#close($screen_fh);
my $out_filename = $id . ".html";
open(my $out_fh, ">", $out_filename) or print($!);
print $out_fh "<html><body><h1>$id</h1><h3>".localtime()."</h3>";
print $out_fh "<pre>" . $query->param("ErrorMessage") . "</pre>";
print $out_fh "<table>";
print $out_fh "<tr><th>Remote host</th><td>" . $query->remote_host() . "</td></tr>\n";
my @names = $query->param;
for my $name (@names)
{
if ($name eq "screenshot")
{
next;
}
print $out_fh "<tr><th>$name</th><td>" . $query->param($name) . "</td></tr>\n";
}
print $out_fh "</table>\n";
print $out_fh '<img src="' . $screenshot_file . '" />' . "\n";
print $out_fh "</body></html>";
close $out_fh;
# Show the ID for ErrorReporter to use
open(my $id_out_fh, ">", $id_filename);
$id += 1;
print $id_out_fh $id;
close($id_out_fh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment