Logging overhaul!

This change brings about a few changes:
1) A new LogLevel called "User" that is used for non-verbose, non-error output
2) Only User and Error are output to console now, not all of them
3) All programs have log to file enabled by default and all flags for enabling logging have been removed
4) Some former Verbose statements have been converted over to User because of the shift in usage.
This commit is contained in:
Matt Nadareski
2016-05-10 15:41:33 -07:00
parent 37253b8c91
commit 1800ac6750
12 changed files with 80 additions and 85 deletions

View File

@@ -97,11 +97,11 @@ namespace SabreTools
return;
}
Logger logger = new Logger(false, "datfromdir.log");
Logger logger = new Logger(true, "datfromdir.log");
logger.Start();
// First things first, take care of all of the arguments that this could have
bool noMD5 = false, noSHA1 = false, forceunpack = false, archivesAsFiles = false, old = false, log = false, superDat = false, bare = false;
bool noMD5 = false, noSHA1 = false, forceunpack = false, archivesAsFiles = false, old = false, superDat = false, bare = false;
string name = "", desc = "", cat = "", version = "", author = "";
List<string> inputs = new List<string>();
foreach (string arg in args)
@@ -138,10 +138,6 @@ namespace SabreTools
case "--old":
old = true;
break;
case "-l":
case "--log":
log = true;
break;
case "-sd":
case "--superdat":
superDat = true;
@@ -183,9 +179,6 @@ namespace SabreTools
return;
}
// Set the new state for Logger
logger.ToFile = log;
// Output the title
Build.Start("DATFromDir");
@@ -365,7 +358,7 @@ namespace SabreTools
_logger.Log(Path.GetFileName(item) + " treated like an archive");
foreach (string entry in Directory.EnumerateFiles(_tempDir, "*", SearchOption.AllDirectories))
{
_logger.Log("\tFound file: " + entry);
_logger.Log("Found file: " + entry);
string fileCRC = String.Empty;
string fileMD5 = String.Empty;
string fileSHA1 = String.Empty;
@@ -410,7 +403,7 @@ namespace SabreTools
SHA1 = fileSHA1,
});
_logger.Log("File added" + Environment.NewLine);
_logger.User("File added: " + entry + Environment.NewLine);
}
}
// Otherwise, just get the info on the file itself
@@ -474,7 +467,7 @@ namespace SabreTools
SHA1 = fileSHA1,
});
_logger.Log("File added" + Environment.NewLine);
_logger.User("File added: " + actualitem + Environment.NewLine);
}
catch (IOException ex)
{

View File

@@ -27,7 +27,7 @@ namespace SabreTools
public static void Main(string[] args)
{
// Perform initial setup and verification
logger = new Logger(false, "database.log");
logger = new Logger(true, "database.log");
logger.Start();
DBTools.EnsureDatabase(_dbName, _connectionString);
Remapping.CreateRemappings();
@@ -44,7 +44,6 @@ namespace SabreTools
// If there's no arguments, show the menu
if (args.Length == 0)
{
logger.ToFile = true;
ShowMainMenu();
logger.Close();
return;
@@ -67,7 +66,6 @@ namespace SabreTools
generate = false,
genall = false,
import = false,
log = false,
listsrc = false,
listsys = false,
merge = false,
@@ -163,10 +161,6 @@ namespace SabreTools
case "--import":
import = true;
break;
case "-l":
case "--log":
log = true;
break;
case "-lso":
case "--list-sources":
listsrc = true;
@@ -321,9 +315,6 @@ namespace SabreTools
return;
}
// Update the logger with the new value
logger.ToFile = log;
// Now take care of each mode in succesion
// Import a file or folder
@@ -738,7 +729,7 @@ or 'b' to go back to the previous menu:
private static void ConvertMissMenu()
{
string selection = "", input = "", prefix = "", postfix = "", addext = "", repext = "";
bool usegame = true, quotes = false, gamename = false;
bool usegame = true, quotes = false, gamename = false, romba = false;
while (selection.ToLowerInvariant() != "b")
{
Console.Clear();
@@ -749,13 +740,14 @@ Make a selection:
1) File to convert" + (input != "" ? ":\n\t" + input : "") + @"
2) " + (usegame ? "Output roms instead of games" : "Output games instead of roms") + @"
3) Prefix to add to each line" + (prefix != "" ? ":\n\t" + prefix : "") + @"
4) Postfix to add to each line" + (postfix != "" ? ":\n\t" + postfix : "") + @"
5) " + (quotes ? "Don't add quotes around each item" : "Add quotes around each item") + @"
6) Replace all extensions with another" + (repext != "" ? ":\t" + repext : "") + @"
7) Add extensions to each item" + (addext != "" ? ":\n\t" + addext : "") + @"
" + (!usegame ? " 8) " + (gamename ? "Don't add game name before every item" : "Add game name before every item") + "\n" : "") +
@" 9) Begin conversion
3) " + (romba ? "Disable Romba-style output naming" : "Enable Romba-style output naming (overrides previous)") + @"
4) Prefix to add to each line" + (prefix != "" ? ":\n\t" + prefix : "") + @"
5) Postfix to add to each line" + (postfix != "" ? ":\n\t" + postfix : "") + @"
6) " + (quotes ? "Don't add quotes around each item" : "Add quotes around each item") + @"
7) Replace all extensions with another" + (repext != "" ? ":\t" + repext : "") + @"
8) Add extensions to each item" + (addext != "" ? ":\n\t" + addext : "") + @"
" + (!usegame ? " 9) " + (gamename ? "Don't add game name before every item" : "Add game name before every item") + "\n" : "") +
@" 10) Begin conversion
B) Go back to the previous menu
");
Console.Write("Enter selection: ");
@@ -771,32 +763,35 @@ Make a selection:
usegame = !usegame;
break;
case "3":
romba = !romba;
break;
case "4":
Console.Clear();
Console.Write("Please enter the prefix: ");
prefix = Console.ReadLine();
break;
case "4":
case "5":
Console.Clear();
Console.Write("Please enter the postfix: ");
postfix = Console.ReadLine();
break;
case "5":
case "6":
quotes = !quotes;
break;
case "6":
case "7":
Console.Clear();
Console.Write("Please enter the replacement extension: ");
repext = Console.ReadLine();
break;
case "7":
case "8":
Console.Clear();
Console.Write("Please enter the additional extension: ");
addext = Console.ReadLine();
break;
case "8":
case "9":
gamename = !gamename;
break;
case "9":
case "10":
Console.Clear();
InitConvertMiss(input, usegame, prefix, postfix, quotes, repext, addext, gamename);
Console.Write("\nPress any key to continue...");
@@ -1100,20 +1095,20 @@ Make a selection:
// Check to see if the second argument is a file that exists
if (filename != "" && File.Exists(filename))
{
logger.Log("Beginning import of " + filename);
logger.User("Beginning import of " + filename);
Import imp = new Import(filename, _connectionString, logger);
bool success = imp.ImportData();
logger.Log(filename + (success ? "" : " not") + " imported!");
logger.User(filename + (success ? "" : " not") + " imported!");
}
// Check to see if the second argument is a directory that exists
else if (filename != "" && Directory.Exists(filename))
{
foreach (string file in Directory.GetFiles(filename, "*", SearchOption.AllDirectories))
{
logger.Log("Beginning import of " + file);
logger.User("Beginning import of " + file);
Import imp = new Import(file, _connectionString, logger);
bool success = imp.ImportData();
logger.Log(file + (success ? "" : " not") + " imported!");
logger.User(file + (success ? "" : " not") + " imported!");
}
}
else
@@ -1239,7 +1234,7 @@ Make a selection:
InitGenerate("", "", outdir, norename, old);
// Zip up all of the files that were generated
logger.Log("Creating zip archive");
logger.User("Creating zip archive");
ZipArchive zip = ZipFile.Open(actualdir + "dats-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip", ZipArchiveMode.Create);
foreach (String filename in Directory.EnumerateFiles(outdir))
{
@@ -1252,7 +1247,7 @@ Make a selection:
}
}
zip.Dispose();
logger.Log("Zip archive created!");
logger.User("Zip archive created!");
// Remove all of the DATs from the folder
Directory.Delete(outdir, true);
@@ -1268,7 +1263,7 @@ Make a selection:
{
if (File.Exists(filename))
{
logger.Log("Converting " + filename);
logger.User("Converting " + filename);
XmlDocument doc = new XmlDocument();
try
{
@@ -1279,7 +1274,7 @@ Make a selection:
sw.Write(conv);
sw.Close();
fs.Close();
logger.Log("Converted file: " + Path.GetFileNameWithoutExtension(filename) + ".new.dat");
logger.User("Converted file: " + Path.GetFileNameWithoutExtension(filename) + ".new.dat");
}
catch (XmlException)
{
@@ -1301,7 +1296,7 @@ Make a selection:
{
if (File.Exists(filename))
{
logger.Log("Converting " + filename);
logger.User("Converting " + filename);
XElement conv = Converters.ClrMameProToXML(File.ReadAllLines(filename));
FileStream fs = File.OpenWrite(Path.GetFileNameWithoutExtension(filename) + ".new.xml");
StreamWriter sw = new StreamWriter(fs);
@@ -1310,7 +1305,7 @@ Make a selection:
sw.Write(conv);
sw.Close();
fs.Close();
logger.Log("Converted file: " + Path.GetFileNameWithoutExtension(filename) + ".new.xml");
logger.User("Converted file: " + Path.GetFileNameWithoutExtension(filename) + ".new.xml");
}
else
{
@@ -1344,9 +1339,9 @@ Make a selection:
string name = Path.GetFileNameWithoutExtension(input) + "-miss.txt";
// Read in the roms from the DAT and then write them to the file
logger.Log("Converting " + input);
logger.User("Converting " + input);
Output.WriteToText(name, Path.GetDirectoryName(input), RomManipulation.Parse(input, 0, 0, logger), logger, usegame, prefix, postfix, addext, repext, quotes, gamename);
logger.Log(input + " converted to: " + name);
logger.User(input + " converted to: " + name);
return;
}
else

View File

@@ -103,7 +103,7 @@ namespace SabreTools
Dictionary<string, List<RomData>> dict = new Dictionary<string, List<RomData>>();
foreach (string input in _inputs)
{
_logger.Log("Adding DAT: " + input);
_logger.User("Adding DAT: " + input);
dict = RomManipulation.ParseDict(input, 0, 0, dict, _logger);
}

View File

@@ -100,7 +100,7 @@ namespace SabreTools
name += " (merged)";
// For good measure, get all sources
Dictionary<int, string> sources = new Dictionary<int, string>();
Dictionary<int, string> sources = new Dictionary<int, string>();C:\Users\Matt\GitHub\DATabase\DATabaseTwo\Generate.cs
sources.Add(0, "Default");
string squery = "SELECT id, name FROM source";
@@ -171,7 +171,7 @@ namespace SabreTools
}
// Now process all of the roms
_logger.Log("Cleaning rom data");
_logger.User("Cleaning rom data");
List<string> keys = roms.Keys.ToList();
foreach (string key in keys)
{

View File

@@ -58,7 +58,7 @@ namespace SabreTools
/// <returns>True if the data could be inserted or updated correctly, false otherwise</returns>
public bool ImportData()
{
_logger.Log("Beginning import/update process");
_logger.User("Beginning import/update process");
using (SqliteConnection dbc = new SqliteConnection(_connectionString))
{
dbc.Open();
@@ -88,7 +88,7 @@ namespace SabreTools
string system = _datroot + Path.DirectorySeparatorChar + sldr.GetString(1) + " - " + sldr.GetString(2);
system = system.Trim();
_logger.Log("System: " + system.Remove(0, 5));
_logger.User("System: " + system.Remove(0, 5));
// Audit all DATs in the folder
foreach (string file in Directory.GetFiles(system, "*", SearchOption.AllDirectories))
@@ -246,7 +246,7 @@ VALUES (" + hashid + ", 'source', '" + sourceid + @"'),
}
}
_logger.Log("Import/update process complete!");
_logger.User("Import/update process complete!");
return true;
}

View File

@@ -27,7 +27,7 @@ namespace SabreTools
{
// Perform initial setup and verification
Console.Clear();
logger = new Logger(false, "database.log");
logger = new Logger(true, "headerer.log");
logger.Start();
DBTools.EnsureDatabase(_dbName, _connectionString);
Remapping.CreateHeaderSkips();
@@ -194,7 +194,7 @@ namespace SabreTools
}
}
logger.Log("File has header: " + (type != HeaderType.None));
logger.User("File has header: " + (type != HeaderType.None));
if (type != HeaderType.None)
{
@@ -212,13 +212,13 @@ namespace SabreTools
hbin = hbin.Skip(hs).ToArray();
// Write out the new file
logger.Log("Creating unheadered file: " + file + ".new");
logger.User("Creating unheadered file: " + file + ".new");
BinaryWriter bw = new BinaryWriter(File.OpenWrite(file + ".new"));
FileInfo fi = new FileInfo(file);
bw.Write(hbin);
bw.Write(br.ReadBytes((int)fi.Length - hs));
bw.Close();
logger.Log("Unheadered file created!");
logger.User("Unheadered file created!");
// Now add the information to the database if it's not already there
SHA1 sha1 = SHA1.Create();
@@ -287,7 +287,7 @@ namespace SabreTools
logger.Log("Found match with rom type " + sldr.GetString(1));
header = sldr.GetString(0);
logger.Log("Creating reheadered file: " + file + ".new" + sub);
logger.User("Creating reheadered file: " + file + ".new" + sub);
BinaryWriter bw = new BinaryWriter(File.OpenWrite(file + ".new" + sub));
// Source: http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa
@@ -297,7 +297,7 @@ namespace SabreTools
}
bw.Write(File.ReadAllBytes(file));
bw.Close();
logger.Log("Reheadered file created!");
logger.User("Reheadered file created!");
}
}
else

View File

@@ -38,7 +38,7 @@ namespace SabreTools
public static void Main(string[] args)
{
// Perform initial setup and verification
Logger logger = new Logger(false, "database.log");
Logger logger = new Logger(true, "offlinemerge.log");
logger.Start();
Console.Clear();
@@ -143,13 +143,13 @@ namespace SabreTools
if (_currentAllMerged != "" && _currentMissingMerged != "" && _currentNewMerged != "")
{
// First get the combination Dictionary of currentAllMerged and currentNewMerged
_logger.Log("Adding Current and New Merged DATs to the dictionary");
_logger.User("Adding Current and New Merged DATs to the dictionary");
Dictionary<string, List<RomData>> completeDats = new Dictionary<string, List<RomData>>();
completeDats = RomManipulation.ParseDict(_currentAllMerged, 0, 0, completeDats, _logger);
completeDats = RomManipulation.ParseDict(_currentNewMerged, 0, 0, completeDats, _logger);
// Now get Net New output dictionary [(currentNewMerged)-(currentAllMerged)]
_logger.Log("Creating and populating Net New dictionary");
_logger.User("Creating and populating Net New dictionary");
Dictionary<string, List<RomData>> netNew = new Dictionary<string, List<RomData>>();
foreach (string key in completeDats.Keys)
{
@@ -173,7 +173,7 @@ namespace SabreTools
}
// Now create the Unneeded dictionary [(currentAllMerged)-(currentNewMerged)]
_logger.Log("Creating and populating Uneeded dictionary");
_logger.User("Creating and populating Uneeded dictionary");
Dictionary<string, List<RomData>> unneeded = new Dictionary<string, List<RomData>>();
foreach (string key in completeDats.Keys)
{
@@ -197,7 +197,7 @@ namespace SabreTools
}
// Now create the New Missing dictionary [(Net New)+(currentMissingMerged-(Unneeded))]
_logger.Log("Creating and populating New Missing dictionary");
_logger.User("Creating and populating New Missing dictionary");
Dictionary<string, List<RomData>> midMissing = new Dictionary<string, List<RomData>>();
midMissing = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midMissing, _logger);
foreach (string key in unneeded.Keys)
@@ -245,7 +245,7 @@ namespace SabreTools
}
// Now create the Have dictionary [(currentNewMerged)-(c)]
_logger.Log("Creating and populating Have dictionary");
_logger.User("Creating and populating Have dictionary");
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
foreach (string key in newMissing.Keys)
{
@@ -308,7 +308,7 @@ namespace SabreTools
// If we are supposed to replace everything in the output with default values, do so
if (_fake)
{
_logger.Log("Replacing all hashes in Net New with 0-byte values");
_logger.User("Replacing all hashes in Net New with 0-byte values");
List<string> keys = netNew.Keys.ToList();
foreach (string key in keys)
{
@@ -326,7 +326,7 @@ namespace SabreTools
netNew[key] = temp;
}
_logger.Log("Replacing all hashes in Unneeded with 0-byte values");
_logger.User("Replacing all hashes in Unneeded with 0-byte values");
keys = unneeded.Keys.ToList();
foreach (string key in keys)
{
@@ -344,7 +344,7 @@ namespace SabreTools
unneeded[key] = temp;
}
_logger.Log("Replacing all hashes in New Missing with 0-byte values");
_logger.User("Replacing all hashes in New Missing with 0-byte values");
keys = newMissing.Keys.ToList();
foreach (string key in keys)
{
@@ -362,7 +362,7 @@ namespace SabreTools
newMissing[key] = temp;
}
_logger.Log("Replacing all hashes in Have with 0-byte values");
_logger.User("Replacing all hashes in Have with 0-byte values");
keys = have.Keys.ToList();
foreach (string key in keys)
{
@@ -394,7 +394,7 @@ namespace SabreTools
else if (_currentAllMerged != "" && _currentMissingMerged != "")
{
// Now create the Have dictionary [(currentAllMerged)-(currentMissingMerged)]
_logger.Log("Creating and populating Have dictionary");
_logger.User("Creating and populating Have dictionary");
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
midHave = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midHave, _logger);
midHave = RomManipulation.ParseDict(_currentAllMerged, 0, 0, midHave, _logger);
@@ -423,7 +423,7 @@ namespace SabreTools
// If we are supposed to replace everything in the output with default values, do so
if (_fake)
{
_logger.Log("Replacing all hashes in Have with 0-byte values");
_logger.User("Replacing all hashes in Have with 0-byte values");
List<string> keys = have.Keys.ToList();
foreach (string key in keys)
{
@@ -451,7 +451,7 @@ namespace SabreTools
else if (_currentNewMerged != "" && _currentMissingMerged != "")
{
// Now create the Have dictionary [(currentNewMerged)-(currentMissingMerged)]
_logger.Log("Creating and populating Have dictionary");
_logger.User("Creating and populating Have dictionary");
Dictionary<string, List<RomData>> midHave = new Dictionary<string, List<RomData>>();
midHave = RomManipulation.ParseDict(_currentMissingMerged, 0, 0, midHave, _logger);
midHave = RomManipulation.ParseDict(_currentNewMerged, 0, 0, midHave, _logger);
@@ -480,7 +480,7 @@ namespace SabreTools
// If we are supposed to replace everything in the output with default values, do so
if (_fake)
{
_logger.Log("Replacing all hashes in Have with 0-byte values");
_logger.User("Replacing all hashes in Have with 0-byte values");
List<string> keys = have.Keys.ToList();
foreach (string key in keys)
{

View File

@@ -114,7 +114,6 @@ Options:
-nr, --no-rename Don't auto-rename games
-o, --old Output DAT in CMP format instead of XML
-i, --import Start tool in import mode
-l, --log Enable logging of program output
-lso, --list-sources List all sources (id <= name)
-lsy, --list-systems List all systems (id <= name)
-m, --merge Merge one or more DATs
@@ -168,7 +167,6 @@ Options:
-c=, --cat= Set the category of the DAT
-v=, --version= Set the version of the DAT
-au=, --author= Set the author of the DAT
-l, --log Enable log to file
-sd, --superdat Enable SuperDAT creation");
break;
case "OfflineMerge":

View File

@@ -46,6 +46,7 @@ namespace SabreTools.Helper
public enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}

View File

@@ -106,8 +106,11 @@ namespace SabreTools.Helper
/// <returns>True if the output could be written, false otherwise</returns>
public bool Log(string output, LogLevel loglevel = LogLevel.VERBOSE)
{
// Everything writes to console
Console.WriteLine(loglevel.ToString() + " " + output);
// USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
{
Console.WriteLine((loglevel == LogLevel.ERROR ? loglevel.ToString() + " " : "") + output);
}
// If we're writing to file, use the existing stream
if (_tofile)
@@ -127,6 +130,11 @@ namespace SabreTools.Helper
return true;
}
public bool User(string output)
{
return Log(output, LogLevel.USER);
}
/// <summary>
/// Write the given string as a warning to the log output
/// </summary>

View File

@@ -41,7 +41,7 @@ namespace SabreTools.Helper
}
// (currently uses current time, change to "last updated time")
logger.Log("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
logger.User("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
try
{
@@ -125,7 +125,7 @@ namespace SabreTools.Helper
}
sw.Write((old ? ")" : "\t</machine>\n</datafile>"));
logger.Log("File written!" + Environment.NewLine);
logger.User("File written!" + Environment.NewLine);
sw.Close();
fs.Close();
}
@@ -202,7 +202,7 @@ namespace SabreTools.Helper
}
// (currently uses current time, change to "last updated time")
logger.Log("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
logger.User("Opening file for writing: " + outDir + description + (old ? ".dat" : ".xml"));
try
{
@@ -281,7 +281,7 @@ namespace SabreTools.Helper
}
sw.Write((old ? ")" : "\t</machine>\n</datafile>"));
logger.Log("File written!" + Environment.NewLine);
logger.User("File written!" + Environment.NewLine);
sw.Close();
fs.Close();
}
@@ -332,7 +332,7 @@ namespace SabreTools.Helper
addext = (addext == "" || addext.StartsWith(".") ? addext : "." + addext);
repext = (repext == "" || repext.StartsWith(".") ? repext : "." + repext);
logger.Log("Opening file for writing: " + outdir + textfile);
logger.User("Opening file for writing: " + outdir + textfile);
try
{
@@ -372,7 +372,7 @@ namespace SabreTools.Helper
}
}
logger.Log("File written!" + Environment.NewLine);
logger.User("File written!" + Environment.NewLine);
sw.Close();
fs.Close();
}

View File

@@ -45,7 +45,7 @@ namespace SabreTools
}
}
logger.Log("The total file size is: " + GetBytesReadable(size));
logger.User("The total file size is: " + GetBytesReadable(size));
logger.Close();
}