mirror of
https://bitbucket.org/Ioncannon/project-meteor-server.git
synced 2025-05-20 08:26:59 -04:00
Cleaned up LogFile and ByteArrayToHex utils.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace FFXIVClassic_Lobby_Server.common
|
||||
{
|
||||
@@ -7,66 +8,64 @@ namespace FFXIVClassic_Lobby_Server.common
|
||||
{
|
||||
public enum LogType
|
||||
{
|
||||
Error = ConsoleColor.Red,
|
||||
Debug = ConsoleColor.Yellow,
|
||||
Info = ConsoleColor.Cyan,
|
||||
Status = ConsoleColor.Green,
|
||||
Sql = ConsoleColor.Magenta,
|
||||
Conn = ConsoleColor.Green,
|
||||
Default = ConsoleColor.Gray
|
||||
Info = ConsoleColor.White,
|
||||
Debug = ConsoleColor.Cyan,
|
||||
Error = ConsoleColor.Red
|
||||
}
|
||||
|
||||
public static void error(String message)
|
||||
public static void Status(String message)
|
||||
{
|
||||
log(message, LogType.Error);
|
||||
LogFile(message, LogType.Status);
|
||||
}
|
||||
|
||||
public static void debug(String message)
|
||||
public static void Sql(String message)
|
||||
{
|
||||
#if DEBUG
|
||||
log(message, LogType.Debug);
|
||||
#endif
|
||||
LogFile(message, LogType.Sql);
|
||||
}
|
||||
|
||||
public static void info(String message)
|
||||
public static void Info(String message)
|
||||
{
|
||||
log(message, LogType.Info);
|
||||
LogFile(message, LogType.Info);
|
||||
}
|
||||
|
||||
public static void database(String message)
|
||||
public static void Debug(String message)
|
||||
{
|
||||
log(message, LogType.Sql);
|
||||
#if DEBUG
|
||||
LogFile(message, LogType.Debug);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void conn(String message)
|
||||
public static void Error(String message)
|
||||
{
|
||||
log(message, LogType.Conn);
|
||||
LogFile(message, LogType.Error);
|
||||
}
|
||||
|
||||
public static void log(String message, LogType type)
|
||||
private static void LogFile(String message, LogType type)
|
||||
{
|
||||
var timestamp = String.Format("[{0}] ", DateTime.Now.ToString("dd/MMM HH:mm:ss"));
|
||||
var typestr = String.Format("[{0}] ", type.ToString().ToUpper());
|
||||
string timestamp = String.Format("[{0}]", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"));
|
||||
string messageType = String.Format("[{0}] ", type.ToString().ToUpper());
|
||||
|
||||
Console.Write(timestamp);
|
||||
Console.WriteLine(timestamp);
|
||||
Console.ForegroundColor = (ConsoleColor)type;
|
||||
Console.Write(typestr);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Console.WriteLine(message);
|
||||
Console.Write(messageType);
|
||||
Console.ResetColor();
|
||||
Console.Write(message);
|
||||
|
||||
message = message.Insert(0, typestr);
|
||||
message = message.Insert(0, timestamp);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
Directory.CreateDirectory(ConfigConstants.OPTIONS_LOGPATH);
|
||||
sb.AppendLine(String.Format("{0}{1}{2}", timestamp, messageType, message));
|
||||
|
||||
try
|
||||
if (!Directory.Exists(ConfigConstants.OPTIONS_LOGPATH))
|
||||
{
|
||||
File.AppendAllText(ConfigConstants.OPTIONS_LOGPATH + ConfigConstants.OPTIONS_LOGFILE, message + Environment.NewLine);
|
||||
Directory.CreateDirectory(ConfigConstants.OPTIONS_LOGPATH);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
using (FileStream fs = new FileStream(Path.Combine(ConfigConstants.OPTIONS_LOGPATH, ConfigConstants.OPTIONS_LOGFILE), FileMode.Append, FileAccess.Write))
|
||||
using (StreamWriter sw = new StreamWriter(fs))
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.Write(e.Message);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
sw.WriteLine(sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,66 +18,69 @@ namespace FFXIVClassic_Lobby_Server.common
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ByteArrayToHex(byte[] bytes)
|
||||
public static string ByteArrayToHex(byte[] bytes, int offset = 0, int bytesPerLine = 16)
|
||||
{
|
||||
if (bytes == null) return "<null>";
|
||||
int bytesLength = bytes.Length;
|
||||
var bytesPerLine = 16;
|
||||
char[] HexChars = "0123456789ABCDEF".ToCharArray();
|
||||
|
||||
int firstHexColumn =
|
||||
8 // 8 characters for the address
|
||||
+ 3; // 3 spaces
|
||||
|
||||
int firstCharColumn = firstHexColumn
|
||||
+ bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
|
||||
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
|
||||
+ 2; // 2 spaces
|
||||
|
||||
int lineLength = firstCharColumn
|
||||
+ bytesPerLine // - characters to show the ascii value
|
||||
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
|
||||
|
||||
char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
|
||||
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
|
||||
StringBuilder result = new StringBuilder(expectedLines * lineLength);
|
||||
|
||||
for (int i = 0; i < bytesLength; i += bytesPerLine)
|
||||
if (bytes == null)
|
||||
{
|
||||
line[0] = HexChars[(i >> 28) & 0xF];
|
||||
line[1] = HexChars[(i >> 24) & 0xF];
|
||||
line[2] = HexChars[(i >> 20) & 0xF];
|
||||
line[3] = HexChars[(i >> 16) & 0xF];
|
||||
line[4] = HexChars[(i >> 12) & 0xF];
|
||||
line[5] = HexChars[(i >> 8) & 0xF];
|
||||
line[6] = HexChars[(i >> 4) & 0xF];
|
||||
line[7] = HexChars[(i >> 0) & 0xF];
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
char[] hexChars = "0123456789ABCDEF".ToCharArray();
|
||||
|
||||
int hexColumn = firstHexColumn;
|
||||
int charColumn = firstCharColumn;
|
||||
// 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
|
||||
// 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
|
||||
int offsetBlock = 8 + 3;
|
||||
int byteBlock = offsetBlock + (bytesPerLine * 3) + ((bytesPerLine - 1) / 8) + 2;
|
||||
int lineLength = byteBlock + bytesPerLine + Environment.NewLine.Length;
|
||||
|
||||
char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
|
||||
int numLines = (bytes.Length + bytesPerLine - 1) / bytesPerLine;
|
||||
|
||||
for (int j = 0; j < bytesPerLine; j++)
|
||||
{
|
||||
if (j > 0 && (j & 7) == 0) hexColumn++;
|
||||
if (i + j >= bytesLength)
|
||||
StringBuilder sb = new StringBuilder(numLines * lineLength);
|
||||
|
||||
for (int i = offset; i < bytes.Length; i += bytesPerLine)
|
||||
{
|
||||
line[0] = hexChars[(i >> 28) & 0xF];
|
||||
line[1] = hexChars[(i >> 24) & 0xF];
|
||||
line[2] = hexChars[(i >> 20) & 0xF];
|
||||
line[3] = hexChars[(i >> 16) & 0xF];
|
||||
line[4] = hexChars[(i >> 12) & 0xF];
|
||||
line[5] = hexChars[(i >> 8) & 0xF];
|
||||
line[6] = hexChars[(i >> 4) & 0xF];
|
||||
line[7] = hexChars[(i >> 0) & 0xF];
|
||||
|
||||
int hexColumn = offsetBlock;
|
||||
int charColumn = byteBlock;
|
||||
|
||||
for (int j = 0; j < bytesPerLine; j++)
|
||||
{
|
||||
if (j > 0 && (j & 7) == 0)
|
||||
{
|
||||
line[hexColumn] = ' ';
|
||||
line[hexColumn + 1] = ' ';
|
||||
line[charColumn] = ' ';
|
||||
hexColumn++;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte b = bytes[i + j];
|
||||
line[hexColumn] = HexChars[(b >> 4) & 0xF];
|
||||
line[hexColumn + 1] = HexChars[b & 0xF];
|
||||
line[charColumn] = (b < 32 ? '.' : (char)b);
|
||||
|
||||
if (i + j >= bytes.Length)
|
||||
{
|
||||
line[hexColumn] = ' ';
|
||||
line[hexColumn + 1] = ' ';
|
||||
line[charColumn] = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
byte by = bytes[i + j];
|
||||
line[hexColumn] = hexChars[(by >> 4) & 0xF];
|
||||
line[hexColumn + 1] = hexChars[by & 0xF];
|
||||
line[charColumn] = (by < 32 ? '.' : (char)by);
|
||||
}
|
||||
hexColumn += 3;
|
||||
charColumn++;
|
||||
}
|
||||
result.Append(line);
|
||||
}
|
||||
return Environment.NewLine + result.ToString();
|
||||
|
||||
hexColumn += 3;
|
||||
charColumn++;
|
||||
}
|
||||
|
||||
sb.Append(line);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static UInt32 UnixTimeStampUTC()
|
||||
|
Reference in New Issue
Block a user