Skip to content

Instantly share code, notes, and snippets.

@sbarisic
Created August 29, 2018 10:54
Show Gist options
  • Save sbarisic/a147155294d1878370ee1d0852f05d41 to your computer and use it in GitHub Desktop.
Save sbarisic/a147155294d1878370ee1d0852f05d41 to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
namespace HTMLog {
public class LogFile : IDisposable {
readonly string Prologue = @"<!DOCTYPE html><html><head><title>HTMLog</title>
<link rel=""stylesheet"" href=""//cdn.jsdelivr.net/npm/hack-font@3/build/web/hack.css"">
<style>
:root { --dark-bg: rgb(30, 30, 30); --light-bg: rgb(35, 35, 35); }
body {
font-family: Hack, monospace; background-color: var(--dark-bg); color: white;
margin-top: 0px; margin-left: 0px; margin-bottom: 0px; margin-right: 0px;
line-height: 1em; height: 2em; width: 100%; white-space: pre;
background: linear-gradient(var(--dark-bg), var(--dark-bg) 1em, var(--light-bg) 0, var(--light-bg) 2em);
}
p { display: inline; }
div { top: 0%; left: 0%; bottom: 100%; right: 100%; }
a:link { color: rgb(20, 150, 150); }
a:visited { color: rgb(30, 80, 90); }
a:hover { color: rgb(30, 190, 220); }
a:active { color: rgb(30, 190, 255); }
" + "</style></head><body>\n";
readonly string Epilogue = "</body></html>\n";
StreamWriter OutWriter;
Stream OutStream;
bool OwnsOutStream;
int LeftPos;
public byte R, G, B;
public LogFile(Stream OutStream) {
OwnsOutStream = false;
Init(OutStream);
}
public LogFile(string FileName) {
OwnsOutStream = true;
FileStream FS = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
Init(FS);
}
public void Dispose() {
OutWriter.Dispose();
if (OwnsOutStream)
OutStream?.Dispose();
}
void Init(Stream OutStream) {
R = G = B = 255;
this.OutStream = OutStream;
OutWriter = new StreamWriter(OutStream, Encoding.UTF8, 1024, true);
if (OutStream.Length <= Prologue.Length) {
OutStream.Seek(0, SeekOrigin.Begin);
WriteRaw(Prologue);
} else
OutStream.Seek(-Epilogue.Length, SeekOrigin.End);
}
void Flush() {
OutWriter.Flush();
OutStream.Flush();
}
void WriteRaw(string Msg) {
OutWriter.Write(Msg);
OutWriter.Write(Epilogue);
Flush();
OutStream.Seek(-Epilogue.Length, SeekOrigin.End);
}
void WriteRaw(string Fmt, params object[] Args) {
WriteRaw(string.Format(CultureInfo.InvariantCulture, Fmt, Args));
}
string MessageTag(string Txt) {
return string.Format("<p style=\"color: #{1:x2}{2:x2}{3:x2};\">{0}</p>", Escape(Txt), R, G, B);
}
string LinkTag(string Msg, string LinkTo) {
return string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", LinkTo, Escape(Msg));
}
string Escape(string Str) {
return System.Security.SecurityElement.Escape(Str);
}
public void Write(string Msg) {
LeftPos += Msg.Length;
WriteRaw(MessageTag(Msg));
}
public void Write(byte R, byte G, byte B, string Msg) {
this.R = R;
this.G = G;
this.B = B;
Write(Msg);
}
public void Write(string Fmt, params object[] Args) {
Write(string.Format(Fmt, Args));
}
public void WriteLine(byte R, byte G, byte B, string Msg) {
this.R = R;
this.G = G;
this.B = B;
WriteLine(Msg);
}
public void WriteLine(string Msg = null) {
if (Msg != null)
WriteRaw(MessageTag(Msg));
WriteRaw("\n");
//WriteRaw("</br>\n");
LeftPos = 0;
}
public void WriteLink(string Msg, string LinkTo) {
LeftPos += Msg.Length;
WriteRaw(LinkTag(Msg, LinkTo));
}
public void Tab(int Pos, char Chr = ' ') {
if (LeftPos >= Pos)
return;
Write(new string(Chr, Pos - LeftPos));
}
public void SetColor(byte R, byte G, byte B) {
this.R = R;
this.G = G;
this.B = B;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment