Added Logging Levels

This commit is contained in:
C9Glax 2022-05-05 16:00:56 +02:00
parent 958913d3d2
commit f30cef46c9
3 changed files with 22 additions and 14 deletions

View File

@ -4,31 +4,39 @@
{ {
private LogType logType; private LogType logType;
private string logfilepath; private string logfilepath;
public Logger(LogType type) private loglevel level;
public Logger(LogType type, loglevel level)
{ {
this.logType = type; this.logType = type;
this.logfilepath = ""; this.logfilepath = "";
this.level = level;
} }
public Logger(LogType type, string path) public Logger(LogType type, loglevel level, string path)
{ {
this.logType = type; this.logType = type;
this.logfilepath = path; this.logfilepath = path;
this.level = level;
} }
public void log(string message, params object[] ?replace) public void log(loglevel type, string message, params object[] ?replace)
{ {
switch (this.logType) if(type >= this.level)
{ {
case LogType.Console: string header = string.Format("{0} {1} {2}:", DateTime.Now.ToLocalTime().ToShortDateString(), DateTime.Now.ToLocalTime().ToLongTimeString(), type.ToString());
Console.WriteLine(string.Format(message, replace)); switch (this.logType)
break; {
case LogType.Logfile: case LogType.Console:
File.WriteAllText(this.logfilepath, string.Format(message, replace)); Console.WriteLine(string.Format(header + message, replace));
break; break;
case LogType.Logfile:
File.WriteAllText(this.logfilepath, string.Format(header + message, replace));
break;
}
} }
} }
} }
public enum LogType { Console, Logfile } public enum LogType { Console, Logfile }
public enum loglevel : ushort { DEBUG = 0, INFO = 1, ERROR = 2 };
} }

View File

@ -8,7 +8,7 @@ namespace astar
private Logger logger; private Logger logger;
public Astar() public Astar()
{ {
this.logger = new Logger(LogType.Console); this.logger = new Logger(LogType.Console, loglevel.DEBUG);
Importer.Import(logger); Importer.Import(logger);
} }
} }

View File

@ -34,7 +34,7 @@ namespace astar
if(currentNodeType == nodeType.WAY) if(currentNodeType == nodeType.WAY)
{ {
ImportWay(currentWay); ImportWay(currentWay);
logger.log("Way nodes: {0}", currentWay.nodes.Count); logger.log(loglevel.INFO, "Way nodes: {0}", currentWay.nodes.Count);
} }
currentNodeType = nodeType.WAY; currentNodeType = nodeType.WAY;
currentNode = nullNode; currentNode = nullNode;
@ -44,7 +44,7 @@ namespace astar
UInt64 id = Convert.ToUInt64(reader.GetAttribute("ref")); UInt64 id = Convert.ToUInt64(reader.GetAttribute("ref"));
if (!nodes.TryGetValue(id, out currentNode)) if (!nodes.TryGetValue(id, out currentNode))
{ {
logger.log("ERR: Node with id {0} not imported.", id); logger.log(loglevel.DEBUG, "Node with id {0} not imported.", id);
} }
else else
{ {
@ -83,7 +83,7 @@ namespace astar
} }
} }
logger.log("Loaded. Nodes: {0}", nodes.Count); logger.log(loglevel.INFO, "Loaded. Nodes: {0}", nodes.Count);
} }
internal static void ImportWay(Way way) internal static void ImportWay(Way way)