Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AppHelper.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Linq;
5 using System.Text;
6 using System.Windows.Forms;
7 
8 namespace SiliconStudio.Core.Windows
9 {
10  public static class AppHelper
11  {
12  public static string[] GetCommandLineArgs()
13  {
14  return Environment.GetCommandLineArgs().Skip(1).ToArray();
15  }
16 
17  public static string BuildErrorToClipboard(Exception e, string header = null)
18  {
19  var body = new StringBuilder();
20 
21  if (header != null)
22  {
23  body.Append(header);
24  }
25  body.AppendFormat("User: {0}\n", Environment.UserName);
26  body.AppendFormat("Current Directory: {0}\n", Environment.CurrentDirectory);
27  body.AppendFormat("OS Version: {0}\n", Environment.OSVersion);
28  body.AppendFormat("Command Line Args: {0}\n", string.Join(" ", GetCommandLineArgs()));
29  PrintExceptionRecursively(body, e);
30  var errorMessage = body.ToString();
31  try
32  {
33  Clipboard.SetText(errorMessage);
34  }
35  catch (Exception)
36  {
37  }
38 
39  return errorMessage;
40  }
41 
42  private static void PrintExceptionRecursively(StringBuilder builder, Exception exception, int indent = 0)
43  {
44  PrintException(builder, exception, indent);
45  if (exception == null)
46  return;
47 
48  var aggregate = exception as AggregateException;
49  if (aggregate != null)
50  {
51  builder.AppendLine("The above exception is an aggregate exception. Printing inner exceptions:");
52  // The InnerException is normally the first of the InnerExceptions.
53  //if (aggregate.InnerException != null)
54  //{
55  // PrintExceptionRecursively(builder, aggregate.InnerException, indent + 2);
56  //}
57  foreach (var innerException in aggregate.InnerExceptions)
58  {
59  PrintExceptionRecursively(builder, innerException, indent + 2);
60  }
61  }
62  else if (exception.InnerException != null)
63  {
64  builder.AppendLine("The above exception has an inner exception:");
65  PrintExceptionRecursively(builder, exception.InnerException, indent + 2);
66  }
67  }
68 
69  private static void PrintException(StringBuilder builder, Exception exception, int indent)
70  {
71  if (exception == null)
72  {
73  builder.AppendFormat("{0}Exception type: (null)\n", Indent(indent));
74  }
75  else
76  {
77  builder.AppendFormat("{0}Exception type: {1}\n", Indent(indent), exception.GetType().Name);
78  builder.AppendFormat("{0}Exception message: {1}\n", Indent(indent), exception.Message);
79  builder.AppendFormat("{0}StackTrace: {1}\n", Indent(indent), exception.StackTrace);
80  }
81  }
82 
83  private static string Indent(int offset)
84  {
85  return "".PadLeft(offset);
86  }
87  }
88 }
static string[] GetCommandLineArgs()
Definition: AppHelper.cs:12
The windows desktop OS.
static string BuildErrorToClipboard(Exception e, string header=null)
Definition: AppHelper.cs:17