2013-07-06 10:10:33 -04:00
|
|
|
|
/*
|
|
|
|
|
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll.
|
|
|
|
|
|
|
|
|
|
Keepass2Android is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
Keepass2Android 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
|
|
|
|
|
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2016-01-11 14:58:42 -05:00
|
|
|
|
using System.Collections.Generic;
|
2013-07-06 10:10:33 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using Android.Preferences;
|
|
|
|
|
using KeePassLib.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
|
{
|
|
|
|
|
public static class Kp2aLog
|
|
|
|
|
{
|
|
|
|
|
private static bool? _logToFile;
|
|
|
|
|
|
2013-08-03 06:31:40 -04:00
|
|
|
|
private static object _fileLocker = new object();
|
|
|
|
|
|
2013-07-06 10:10:33 -04:00
|
|
|
|
public static void Log(string message)
|
|
|
|
|
{
|
2013-09-17 00:53:18 -04:00
|
|
|
|
if (message != null)
|
|
|
|
|
Android.Util.Log.Debug("KP2A", message);
|
2013-07-06 10:10:33 -04:00
|
|
|
|
if (LogToFile)
|
|
|
|
|
{
|
2013-08-03 06:31:40 -04:00
|
|
|
|
lock (_fileLocker)
|
2013-07-06 10:10:33 -04:00
|
|
|
|
{
|
2013-08-03 06:31:40 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var streamWriter = File.AppendText(LogFilename))
|
|
|
|
|
{
|
|
|
|
|
string stringToLog = DateTime.Now + ":" + DateTime.Now.Millisecond + " -- " + message;
|
|
|
|
|
streamWriter.WriteLine(stringToLog);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Android.Util.Log.Debug("KP2A", "Couldn't write to log file. " + e);
|
|
|
|
|
}
|
2013-07-06 10:10:33 -04:00
|
|
|
|
}
|
2013-08-03 06:31:40 -04:00
|
|
|
|
|
2013-07-06 10:10:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string LogFilename
|
|
|
|
|
{
|
|
|
|
|
get { return "/mnt/sdcard/keepass2android.log"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool LogToFile
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_logToFile == null)
|
|
|
|
|
_logToFile = File.Exists(LogFilename);
|
|
|
|
|
return (bool) _logToFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-11 14:58:42 -05:00
|
|
|
|
public static event EventHandler<Exception> OnUnexpectedError;
|
|
|
|
|
|
|
|
|
|
public static void LogUnexpectedError(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
Log(exception.ToString());
|
|
|
|
|
if (OnUnexpectedError != null)
|
|
|
|
|
OnUnexpectedError(null, exception);
|
|
|
|
|
}
|
2013-07-06 10:10:33 -04:00
|
|
|
|
}
|
|
|
|
|
}
|