Add logging support and bump version

This commit is contained in:
Matt Nadareski
2016-03-28 17:54:24 -07:00
parent 04b2eacbdd
commit 9c8075dc05
5 changed files with 152 additions and 26 deletions

View File

@@ -7,6 +7,8 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using WoD.Helper;
namespace WoD
{
class Generate
@@ -20,14 +22,16 @@ namespace WoD
// Private required variables
private Dictionary<int, string> _headers;
private Logging _logger;
public Generate(string systems, string sources, string connectionString, bool norename = false, bool old = false)
public Generate(string systems, string sources, string connectionString, Logging logger, bool norename = false, bool old = false)
{
_systems = systems;
_sources = sources;
_connectionString = connectionString;
_norename = norename;
_old = old;
_logger = logger;
_headers = new Dictionary<int, string>();
_headers.Add(25, "a7800.xml");
@@ -45,7 +49,7 @@ namespace WoD
int id = 0;
if (_sources != "" && Int32.TryParse(_sources, out id) && id <= 14)
{
Console.WriteLine("This source is import-only so a DAT cannot be created. We apologize for the inconvenience.");
_logger.Log("This source is import-only so a DAT cannot be created. We apologize for the inconvenience.");
return false;
}
@@ -64,7 +68,7 @@ namespace WoD
// If there are no games for this combination, return nothing
if (!sldr.HasRows)
{
Console.WriteLine("No system could be found with id in \"" + _systems + "\". Please check and try again.");
_logger.Log("No system could be found with id in \"" + _systems + "\". Please check and try again.");
return false;
}
@@ -106,7 +110,7 @@ namespace WoD
// If there are no games for this combination, return nothing
if (!sldr.HasRows)
{
Console.WriteLine("No source could be found with id in \"" + _sources + "\". Please check and try again.");
_logger.Log("No source could be found with id in \"" + _sources + "\". Please check and try again.");
return false;
}
@@ -146,7 +150,7 @@ namespace WoD
string datname = systemname + " (" + sourcename + " " + version + ")";
// Create and open an output file for writing (currently uses current time, change to "last updated time"
Console.WriteLine("Opening file for writing: " + datname + (_old ? ".dat" : ".xml"));
_logger.Log("Opening file for writing: " + datname + (_old ? ".dat" : ".xml"));
try
{
@@ -234,13 +238,13 @@ namespace WoD
}
sw.Write((_old ? ")" : "\t</machine>\n</datafile>"));
Console.WriteLine("File written!");
_logger.Log("File written!");
sw.Close();
fs.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
_logger.Log(ex.ToString());
return false;
}
@@ -288,7 +292,7 @@ JOIN checksums
// If there are no games for this combination, return nothing
if (!sldr.HasRows)
{
Console.WriteLine("No games could be found with those inputs. Please check and try again.");
_logger.Log("No games could be found with those inputs. Please check and try again.");
return null;
}

View File

@@ -13,6 +13,7 @@ namespace WoD
// Private instance variables
private string _filepath;
private string _connectionString;
private Logging _logger;
// Regex File Name Patterns
private static string _defaultPattern = @"^(.+?) - (.+?) \((.*) (.*)\)\.dat$";
@@ -49,7 +50,7 @@ namespace WoD
}
// Constructor
public Import(string filepath, string connectionString)
public Import(string filepath, string connectionString, Logging logger)
{
if (File.Exists(filepath))
{
@@ -61,6 +62,7 @@ namespace WoD
}
_connectionString = connectionString;
_logger = logger;
}
// Import the data from file into the database
@@ -104,7 +106,7 @@ namespace WoD
// If the type is still unmatched, the data can't be imported yet
else
{
Console.WriteLine("File " + filename + " cannot be imported at this time because it is not a known pattern.\nPlease try again with an unrenamed version.");
_logger.Log("File " + filename + " cannot be imported at this time because it is not a known pattern.\nPlease try again with an unrenamed version.");
return false;
}
@@ -120,7 +122,7 @@ namespace WoD
case DatType.mame:
if (!Remapping.MAME.ContainsKey(fileinfo[1].Value))
{
Console.WriteLine("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
_logger.Log("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
return false;
}
GroupCollection mameInfo = Regex.Match(Remapping.MAME[fileinfo[1].Value], _remappedPattern).Groups;
@@ -133,7 +135,7 @@ namespace WoD
case DatType.nointro:
if (!Remapping.NoIntro.ContainsKey(fileinfo[1].Value))
{
Console.WriteLine("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
_logger.Log("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
return false;
}
GroupCollection nointroInfo = Regex.Match(Remapping.NoIntro[fileinfo[1].Value], _remappedPattern).Groups;
@@ -149,7 +151,7 @@ namespace WoD
case DatType.redump:
if (!Remapping.Redump.ContainsKey(fileinfo[1].Value))
{
Console.WriteLine("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
_logger.Log("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
return false;
}
GroupCollection redumpInfo = Regex.Match(Remapping.Redump[fileinfo[1].Value], _remappedPattern).Groups;
@@ -165,7 +167,7 @@ namespace WoD
case DatType.tosec:
if (!Remapping.TOSEC.ContainsKey(fileinfo[1].Value))
{
Console.WriteLine("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
_logger.Log("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
return false;
}
GroupCollection tosecInfo = Regex.Match(Remapping.TOSEC[fileinfo[1].Value], _remappedPattern).Groups;
@@ -180,7 +182,7 @@ namespace WoD
case DatType.trurip:
if (!Remapping.TruRip.ContainsKey(fileinfo[1].Value))
{
Console.WriteLine("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
_logger.Log("The filename " + fileinfo[1].Value + " could not be mapped! Please check the mappings and try again");
return false;
}
GroupCollection truripInfo = Regex.Match(Remapping.TruRip[fileinfo[1].Value], _remappedPattern).Groups;
@@ -216,7 +218,7 @@ namespace WoD
// If nothing is found, tell the user and exit
if (!sldr.HasRows)
{
Console.WriteLine("Error: No suitable system found! Please add the system and then try again.");
_logger.Log("Error: No suitable system found! Please add the system and then try again.");
return false;
}
@@ -240,7 +242,7 @@ namespace WoD
// If nothing is found, tell the user and exit
if (!sldr.HasRows)
{
Console.WriteLine("Error: No suitable source found! Please add the source and then try again.");
_logger.Log("Error: No suitable source found! Please add the source and then try again.");
return false;
}
@@ -495,14 +497,14 @@ INSERT INTO files (setid, name, type, lastupdated)
// If the insert of the checksums failed, that's bad
if (affected < 1)
{
Console.WriteLine("There was an error adding checksums for " + name + " to the database!");
_logger.Log("There was an error adding checksums for " + name + " to the database!");
return false;
}
}
// Otherwise, something happened which is bad
else
{
Console.WriteLine("There was an error adding " + name + " to the database!");
_logger.Log("There was an error adding " + name + " to the database!");
return false;
}
}

View File

@@ -9,15 +9,16 @@ namespace WoD
{
class DATabase
{
private static Logging logger;
private static string _dbName = "DATabase.sqlite";
private static string _connectionString = "Data Source=" + _dbName + ";Version = 3;";
private static string _version = "0.1.0.0";
private static string _version = "0.1.1.0";
private static string _header =
@"+-----------------------------------------------------------------------------+
| DATabase " + _version + @" |
| |
| Programmed by Matt Nadareski (darksabre76) |
| Tested by @tractivo |
| Programming: Matt Nadareski (darksabre76) |
| Testing: @tractivo |
+-----------------------------------------------------------------------------+
";
@@ -26,6 +27,8 @@ namespace WoD
// Perform initial setup and verification
DBTools.EnsureDatabase(_dbName, _connectionString);
Remapping.CreateRemappings();
logger = new Logging(false, "database.log");
logger.Start();
Console.Clear();
Console.Title = "DATabase " + _version;
@@ -33,12 +36,14 @@ namespace WoD
if (args.Length == 0)
{
ShowMainMenu();
logger.Close();
return;
}
// Determine which switches are enabled (with values if necessary)
bool help = false, import = false, generate = false, convert = false,
listsys = false, listsrc = false, norename = false, old = false;
listsys = false, listsrc = false, norename = false, old = false,
log = false;
string systems = "", sources = "", input = "";
foreach (string arg in args)
{
@@ -50,6 +55,7 @@ namespace WoD
listsrc = listsrc || (arg == "-lso" || arg == "--list-sources");
norename = norename || (arg == "-nr" || arg == "--no-rename");
old = old || (arg == "-old" || arg == "--romvault");
log = log || (arg == "-l" || arg == "--log");
systems = (arg.StartsWith("system=") && systems == "" ? arg.Split('=')[1] : systems);
sources = (arg.StartsWith("source=") && sources == "" ? arg.Split('=')[1] : sources);
@@ -62,9 +68,13 @@ namespace WoD
if (help || !(import ^ generate ^ listsys ^ listsrc))
{
Help();
logger.Close();
return;
}
// Update the logger with the new value
logger.ToFile = log;
// Now take care of each mode in succesion
// Import a file or folder
@@ -96,6 +106,7 @@ namespace WoD
{
InitConvert(input);
}
logger.Close();
return;
}
@@ -128,6 +139,7 @@ Make a selection:
4) Convert a DAT file from RV to XML
5) List all available sources
6) List all available systems
7) " + (logger.ToFile ? "Disable Logging" : "Enable Logging") + @"
X) Exit Program
");
Console.Write("Enter selection: ");
@@ -161,6 +173,9 @@ Make a selection:
Console.Write("\nPress any key to continue...");
Console.ReadKey();
break;
case "7":
logger.ToFile = !logger.ToFile;
break;
}
}
Console.Clear();
@@ -188,6 +203,7 @@ Options:
-lsy, --list-systems List all systems (id <= name)
-c, --convert Convert a RV DAT to XML
A filename or folder is required to run
-l, --log Enable logging of program output
Filenames and directories can't start with '-', 'system=', or 'source='
unless prefixed by 'input='
@@ -229,7 +245,7 @@ or 'b' to go back to the previous menu:");
if (filename != "" && File.Exists(filename))
{
Console.WriteLine("Beginning import of " + filename);
Import imp = new Import(filename, _connectionString);
Import imp = new Import(filename, _connectionString, logger);
imp.ImportData();
Console.WriteLine(filename + " imported!");
}
@@ -239,7 +255,7 @@ or 'b' to go back to the previous menu:");
foreach (string file in Directory.GetFiles(filename, "*", SearchOption.TopDirectoryOnly))
{
Console.WriteLine("Beginning import of " + file);
Import imp = new Import(file, _connectionString);
Import imp = new Import(file, _connectionString, logger);
imp.ImportData();
Console.WriteLine(file + " imported!");
}
@@ -305,7 +321,7 @@ Make a selection:
private static void InitGenerate(string systems, string sources, bool norename, bool old)
{
Generate gen = new Generate(systems, sources, _connectionString, norename, old);
Generate gen = new Generate(systems, sources, _connectionString, logger, norename, old);
gen.Export();
return;
}

View File

@@ -71,6 +71,7 @@
<Compile Include="Core\Import.cs" />
<Compile Include="Helper\DBTools.cs" />
<Compile Include="DATabase.cs" />
<Compile Include="Helper\Logging.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helper\Remapping.cs" />
<Compile Include="Helper\Style.cs" />

103
DATabase/Helper/Logging.cs Normal file
View File

@@ -0,0 +1,103 @@
using System;
using System.IO;
namespace WoD.Helper
{
public class Logging
{
// Private instance variables
private bool _tofile;
private string _filename;
private StreamWriter _log;
// Public wrappers
public bool ToFile
{
get { return _tofile; }
set
{
if (!value)
{
Close();
}
_tofile = value;
if (_tofile)
{
Start();
}
}
}
public Logging(bool tofile, string filename = "")
{
_tofile = tofile;
_filename = filename;
}
public bool Start()
{
if (!_tofile)
{
return true;
}
try
{
if (!File.Exists(_filename))
{
File.Create(_filename);
}
_log = new StreamWriter(File.OpenWrite(_filename));
_log.WriteLine("Logging started " + DateTime.Now);
}
catch
{
return false;
}
return true;
}
public bool Close()
{
if (!_tofile)
{
return true;
}
try
{
_log.WriteLine("Logging ended " + DateTime.Now);
_log.Close();
}
catch
{
return false;
}
return true;
}
public bool Log(string output)
{
// If we're writing to console, just write the string
if (!_tofile)
{
Console.WriteLine(output);
return true;
}
// If we're writing to file, use the existing stream
try
{
_log.WriteLine(output);
}
catch
{
Console.WriteLine("Could not write to log file!");
return false;
}
return true;
}
}
}