public void LogEvent(logger.LogLevel logLevel, DateTime time, String methodName, String threadName, String message) { if (sw == null) { return; } String strLevel = "[" + logger.GetNameByLogLevel(logLevel) + "]"; String line = time.ToString(logger.datePattern) + (threadName != null ? " " + threadName.PadRight(6, ' ') : "") + (methodName != null ? methodName : "") + " " + strLevel.PadRight(7) + " " + message; UInt32 color = logger.GetColorByLogLevel(logLevel); lock (sw) { bytes[0] = (byte)(color & 0x000000FF); bytes[1] = (byte)((color >> 8) & 0x000000FF); bytes[2] = (byte)((color >> 16) & 0x000000FF); for (int i = 0; i < bytes.Length; i++) { int b = bytes[i]; chars[i * 2] = hexDigits[b >> 4]; chars[i * 2 + 1] = hexDigits[b & 0xF]; } sw.Write("<font color=\"" + new string(chars) + "\">"); sw.Write(ConvertToHTML(line)); sw.WriteLine("</font><br>"); sw.Flush(); } }
public static String GetNameByLogLevel(logger.LogLevel level) { switch (level) { case LogLevel.RES: return("RES"); case LogLevel.AUDIO: return("AUDIO"); case LogLevel.PLAYER: return("PLAY"); case LogLevel.TODO: return("TODO"); case LogLevel.SQL: return("SQL"); case LogLevel.DATABASE: return("DB"); case LogLevel.TRANSACTION: return("TRANS"); case LogLevel.GUI: return("GUI"); case LogLevel.XML: return("XML"); case LogLevel.DEBUG: return("DEBUG"); case LogLevel.CONNECTION: return("CON"); case LogLevel.INFO: return("INFO"); case LogLevel.WARN: return("WARN"); case LogLevel.ERROR: return("ERROR"); case LogLevel.FATAL: return("FATAL"); case LogLevel.MEMORY: return("MEM"); default: return("UNKNOWN"); } }
public static UInt32 GetColorByLogLevel(logger.LogLevel level) { switch (level) { case logger.LogLevel.FATAL: return(NativeMethods.GetRGB(0xFF, 0x00, 0xFF)); case logger.LogLevel.ERROR: return(NativeMethods.GetRGB(0xFF, 0x00, 0x00)); case logger.LogLevel.WARN: return(NativeMethods.GetRGB(0x80, 0x00, 0x80)); case logger.LogLevel.GUI: return(NativeMethods.GetRGB(0x00, 0xFF, 0xFF)); case logger.LogLevel.XML: return(NativeMethods.GetRGB(0x40, 0x80, 0xF0)); case logger.LogLevel.INFO: return(NativeMethods.GetRGB(0xFF, 0xFF, 0x00)); case logger.LogLevel.TRANSACTION: return(NativeMethods.GetRGB(0x00, 0xFF, 0x00)); case logger.LogLevel.CONNECTION: return(NativeMethods.GetRGB(0x60, 0x30, 0xFF)); case logger.LogLevel.DEBUG: return(NativeMethods.GetRGB(0xFF, 0xFF, 0xFF)); case logger.LogLevel.DATABASE: return(NativeMethods.GetRGB(0x00, 0x80, 0x80)); case logger.LogLevel.SQL: return(NativeMethods.GetRGB(0x00, 0xFF, 0xFF)); case logger.LogLevel.RES: return(NativeMethods.GetRGB(0x00, 0x80, 0x80)); case logger.LogLevel.AUDIO: return(NativeMethods.GetRGB(0x40, 0xF0, 0x40)); case logger.LogLevel.PLAYER: return(NativeMethods.GetRGB(0x10, 0xF0, 0x10)); case logger.LogLevel.TODO: return(NativeMethods.GetRGB(0xFF, 0x40, 0x40)); case logger.LogLevel.MEMORY: return(NativeMethods.GetRGB(0xFF, 0x60, 0x60)); default: return(NativeMethods.GetRGB(0xFF, 0xFF, 0xFF)); } }
public void LogEvent(logger.LogLevel logLevel, DateTime time, String methodName, String threadName, String message) { if (sw == null) { return; } String strLevel = "[" + logger.GetNameByLogLevel(logLevel) + "]"; String line = time.ToString(logger.datePattern) + (threadName != null ? " " + threadName.PadRight(6, ' ') : "") + (methodName != null ? methodName : "") + " " + strLevel.PadRight(7) + " " + message; lock (sw) { sw.WriteLine(line); sw.Flush(); } }
public void LogEvent(logger.LogLevel logLevel, DateTime time, String methodName, String threadName, String message) { if (fs == null) { return; } lock (fs) { byteBuffer.Clear(); byteBuffer.PutByte(BinaryFileAppender.FRAME_TEXT); byteBuffer.PutInt((int)logLevel); byteBuffer.PutDateTime(time); byteBuffer.PutString(methodName); byteBuffer.PutString(threadName); byteBuffer.PutString(message); byteBuffer.WriteToFile(fs); fs.Flush(); } }
public void LogEvent(logger.LogLevel logLevel, DateTime time, String methodName, String threadName, String message) { byteBuffer.Clear(); byteBuffer.PutInt((int)logLevel); byteBuffer.PutDateTime(time); byteBuffer.PutString(methodName); byteBuffer.PutString(threadName); if (message.Length > MAX_MESSAGE_SIZE) { message = message.Substring(0, MAX_MESSAGE_SIZE); } byteBuffer.PutString(message); sizeBuf[0] = (byte)((byteBuffer.index) >> 8); sizeBuf[1] = (byte)(byteBuffer.index); pipeStream.Write(sizeBuf, 0, 2); pipeStream.Write(byteBuffer.data, 0, byteBuffer.index); pipeStream.Flush(); }
public void LogEvent(logger.LogLevel logLevel, DateTime time, String methodName, String threadName, String message) { NativeMethods.CONSOLE_SCREEN_BUFFER_INFO bufferInfo; IntPtr consoleHandle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE); NativeMethods.GetConsoleScreenBufferInfo(consoleHandle, out bufferInfo); // console colors NativeMethods.SetConsoleTextAttribute(consoleHandle, GetColorByLogLevel(logLevel)); String strLevel = "[" + logger.GetNameByLogLevel(logLevel) + "]"; String line = time.ToString("HH:mm:ss,fff") + (threadName != null ? " " + threadName.PadRight(6, ' ') : "") + (methodName != null ? methodName : "") + " " + strLevel.PadRight(7) + " " + message + "\n"; // write the output. UInt32 ignoreWrittenCount = 0; NativeMethods.WriteConsoleW(consoleHandle, line, (UInt32)line.Length, out ignoreWrittenCount, IntPtr.Zero); }
private ushort GetColorByLogLevel(logger.LogLevel level) { switch (level) { case logger.LogLevel.SQL: return((ushort)NativeMethods.ConsoleColors.Cyan | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.DATABASE: return((ushort)NativeMethods.ConsoleColors.Cyan); case logger.LogLevel.TRANSAKCJA: return((ushort)NativeMethods.ConsoleColors.Green | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.GUI: return((ushort)NativeMethods.ConsoleColors.Blue | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.DEBUG: return((ushort)NativeMethods.ConsoleColors.White | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.CONNECTION: return((ushort)NativeMethods.ConsoleColors.Blue | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.INFO: return((ushort)NativeMethods.ConsoleColors.Yellow | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.WARN: return((ushort)NativeMethods.ConsoleColors.Purple); case logger.LogLevel.ERROR: return((ushort)NativeMethods.ConsoleColors.Red | (ushort)NativeMethods.ConsoleColors.HighIntensity); case logger.LogLevel.FATAL: return((ushort)NativeMethods.ConsoleColors.Purple | (ushort)NativeMethods.ConsoleColors.HighIntensity); default: return((ushort)NativeMethods.ConsoleColors.White); } }
private static void NamedPipeThread() { //logger.debug("create pipe"); SafeFileHandle pipeHandle; String pipeName = "\\\\.\\pipe\\logconsole" + hostProcID.ToString(); pipeHandle = NativeMethods.CreateNamedPipe(pipeName, NativeMethods.PIPE_DUPLEX | NativeMethods.FILE_FLAG_OVERLAPPED, 0, MAX_INSTANCES, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero); if (pipeHandle.IsInvalid) { //logger.error("create pipe failed"); return; } //logger.debug("wait for client"); while (true) { // wait for client int success = NativeMethods.ConnectNamedPipe(pipeHandle, IntPtr.Zero); //failed to connect client pipe if (success != 1) { // hope that optimizer is not going to optimize this: if (NativeMethods.GetLastError() == NativeMethods.ERROR_PIPE_CONNECTED) { break; } //logger.error("connection failed: " + success); } else { break; } Thread.Sleep(15); } FileStream fStream = new FileStream(pipeHandle, FileAccess.ReadWrite, BUFFER_SIZE, true); //logger.debug("server connected"); Engine.IsReady = true; //logger.debug("wait for console"); while (true) { if (LogConsoleProgram.logConsoleWindow != null && LogConsoleProgram.logConsoleWindow.IsInitialized) { break; } Thread.Sleep(5); } //logger.debug("started"); byte[] buffer = new byte[BUFFER_SIZE]; ASCIIEncoding encoder = new ASCIIEncoding(); int offset = 0; bool readingNumBytes = true; int packetNumBytes = -1; while (!shutdown) { /* * Process hostProcess = null; * try * { * hostProcess = Process.GetProcessById(hostProcID); * if (hostProcess == null) * LogConsoleProgram.Shutdown(); * } * catch * { * LogConsoleProgram.Shutdown(); * } */ int bytesRead = fStream.Read(buffer, offset, BUFFER_SIZE - offset); offset += bytesRead; if (bytesRead == 0) { // disconnected LogConsoleProgram.Shutdown(); } while (true) { if (readingNumBytes) { if (offset < 2) { break; } packetNumBytes = buffer[1] | (buffer[0] << 8); Array.Copy(buffer, 2, buffer, 0, buffer.Length - 2); offset -= 2; readingNumBytes = false; } else { if (offset < packetNumBytes) { break; } LogEventsBuffer byteBuffer = new LogEventsBuffer(buffer, packetNumBytes); Array.Copy(buffer, packetNumBytes, buffer, 0, buffer.Length - packetNumBytes); offset -= packetNumBytes; readingNumBytes = true; logger.LogLevel logLevel = (logger.LogLevel)byteBuffer.GetInt(); DateTime time = byteBuffer.GetDateTime(); String methodName = byteBuffer.GetString(); String threadName = byteBuffer.GetString(); String message = byteBuffer.GetString(); //logger.debug("received " + message); LogConsoleProgram.logConsoleWindow.LogEvent(logLevel, time, methodName, threadName, message); } } //Thread.Sleep(15); } //logger.debug("server finished"); LogConsoleProgram.Shutdown(); }