2012-07-28 13:58:09 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2017-07-23 12:47:54 +00:00
|
|
|
// Copyright (C) 2010 Hylke Bons <hi@planetpeanut.uk>
|
2012-07-28 13:58:09 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2013-10-11 15:13:46 +00:00
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
2012-07-28 13:58:09 +00:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2013-10-11 15:13:46 +00:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-28 13:58:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2016-03-31 08:35:26 +00:00
|
|
|
namespace Sparkles {
|
2012-07-28 13:58:09 +00:00
|
|
|
|
2016-03-30 23:36:31 +00:00
|
|
|
public static class Logger {
|
2012-07-28 13:58:09 +00:00
|
|
|
|
2018-03-14 21:34:06 +00:00
|
|
|
static StreamWriter log_writer = File.CreateText (Configuration.DefaultConfiguration.LogFilePath);
|
2018-01-22 12:03:08 +00:00
|
|
|
static object log_writer_lock = new object ();
|
2016-03-28 20:29:25 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
|
|
|
|
public static void LogInfo (string type, string message)
|
2012-11-22 12:31:48 +00:00
|
|
|
{
|
|
|
|
LogInfo (type, message, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void LogInfo (string type, string message, Exception exception)
|
2012-07-28 13:58:09 +00:00
|
|
|
{
|
|
|
|
string timestamp = DateTime.Now.ToString ("HH:mm:ss");
|
2013-07-06 19:00:27 +00:00
|
|
|
string line;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty (type))
|
2016-03-28 20:29:25 +00:00
|
|
|
line = timestamp + " " + message;
|
2013-07-06 19:00:27 +00:00
|
|
|
else
|
2016-03-28 20:29:25 +00:00
|
|
|
line = timestamp + " " + type + " | " + message;
|
2012-07-28 13:58:09 +00:00
|
|
|
|
2012-11-22 12:31:48 +00:00
|
|
|
if (exception != null)
|
|
|
|
line += ": " + exception.Message + " " + exception.StackTrace;
|
|
|
|
|
2016-03-30 23:36:31 +00:00
|
|
|
if (Configuration.DebugMode)
|
2012-07-28 13:58:09 +00:00
|
|
|
Console.WriteLine (line);
|
|
|
|
|
2018-01-22 12:03:08 +00:00
|
|
|
lock (log_writer_lock) {
|
2018-03-14 16:10:52 +00:00
|
|
|
try {
|
|
|
|
log_writer.WriteLine (line);
|
|
|
|
log_writer.Flush ();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Console.WriteLine (string.Format ("Could not write to log {0}: {1} {2}",
|
|
|
|
(log_writer.BaseStream as FileStream).Name, e.Message, e.StackTrace));
|
|
|
|
}
|
2012-09-23 11:58:33 +00:00
|
|
|
}
|
2012-07-28 13:58:09 +00:00
|
|
|
}
|
2012-08-01 13:04:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
public static void WriteCrashReport (Exception e)
|
|
|
|
{
|
2018-03-14 21:34:06 +00:00
|
|
|
if (log_writer != null)
|
|
|
|
log_writer.Close ();
|
|
|
|
|
2012-08-01 13:04:31 +00:00
|
|
|
string home_path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
|
|
|
|
|
2016-04-10 16:48:18 +00:00
|
|
|
if (InstallationInfo.OperatingSystem == OS.Windows)
|
2012-08-01 13:04:31 +00:00
|
|
|
home_path = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
|
|
|
|
|
2016-03-31 14:46:17 +00:00
|
|
|
string crash_report_file_path = Path.Combine (home_path, "SparkleShare", "crash_report.txt");
|
2012-08-01 13:04:31 +00:00
|
|
|
|
|
|
|
string n = Environment.NewLine;
|
2016-03-31 08:35:26 +00:00
|
|
|
string crash_report =
|
|
|
|
"Oops! SparkleShare has crashed... :(" + n +
|
|
|
|
n +
|
2012-08-01 13:04:31 +00:00
|
|
|
"If you want to help fix this crash, please report it at " + n +
|
2016-03-31 08:35:26 +00:00
|
|
|
"https://github.com/hbons/SparkleShare/issues and include the lines below." + n +
|
|
|
|
n +
|
|
|
|
"Remove any sensitive information like file names, IP addresses, domain names, etc. if needed." + n +
|
|
|
|
n +
|
|
|
|
"------" + n +
|
|
|
|
n;
|
2012-08-01 13:04:31 +00:00
|
|
|
|
2016-03-31 08:35:26 +00:00
|
|
|
crash_report += e.GetType () + ": " + e.Message + n + e.StackTrace + n + n;
|
2012-08-01 13:04:31 +00:00
|
|
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
crash_report += n + e.InnerException.Message + n + e.InnerException.StackTrace + n;
|
|
|
|
|
2016-04-08 15:57:11 +00:00
|
|
|
if (Configuration.DefaultConfiguration != null && File.Exists (Configuration.DefaultConfiguration.LogFilePath)) {
|
|
|
|
string debug_log = File.ReadAllText (Configuration.DefaultConfiguration.LogFilePath);
|
2012-08-01 13:04:31 +00:00
|
|
|
string [] debug_lines = debug_log.Split (Environment.NewLine.ToCharArray ());
|
|
|
|
int line_count = 50;
|
|
|
|
|
2016-03-31 08:35:26 +00:00
|
|
|
if (debug_lines.Length > line_count)
|
|
|
|
crash_report += string.Join (n, debug_lines, (debug_lines.Length - line_count), line_count) + n;
|
|
|
|
else
|
2012-08-01 13:04:31 +00:00
|
|
|
crash_report += debug_log + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
File.WriteAllText (crash_report_file_path, crash_report);
|
2013-10-10 14:04:55 +00:00
|
|
|
Console.WriteLine (DateTime.Now.ToString ("HH:mm:ss") + " | Wrote crash report to " + crash_report_file_path);
|
2012-08-01 13:04:31 +00:00
|
|
|
}
|
2012-07-28 13:58:09 +00:00
|
|
|
}
|
|
|
|
}
|