Skip to content

Commit

Permalink
Make HTMLLogger allocation-free.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Sep 13, 2015
1 parent cf21052 commit b5a1e37
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions source/vibe/core/log.d
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ struct LogLine {
Fiber fiber;
uint fiberID;
SysTime time;
string text;
string text; /// Legacy field used in `Logger.log`
}

/// Abstract base class for all loggers
Expand Down Expand Up @@ -340,7 +340,7 @@ final class HTMLLogger : Logger {

@property void minLogLevel(LogLevel value) pure nothrow @safe { this.minLevel = value; }

override void log(ref LogLine msg)
override void beginLine(ref LogLine msg)
@trusted // FILE isn't @safe (as of DMD 2.065)
{
if( !m_logFile.isOpen ) return;
Expand All @@ -361,18 +361,25 @@ final class HTMLLogger : Logger {
if (msg.thread)
m_logFile.writef(`<div class="threadName">%s</div>`, msg.thread.name);
m_logFile.write(`<div class="message">`);
{
auto dst = m_logFile.lockingTextWriter();
auto txt = msg.text;
while (!txt.empty && (txt.front == ' ' || txt.front == '\t')) {
foreach (i; 0 .. txt.front == ' ' ? 1 : 4)
dst.put("&nbsp;");
txt.popFront();
}
filterHTMLEscape(dst, txt);
}

override void put(scope const(char)[] text)
{
auto dst = () @trusted { return m_logFile.lockingTextWriter(); } (); // LockingTextWriter not @safe for DMD 2.066
while (!text.empty && (text.front == ' ' || text.front == '\t')) {
foreach (i; 0 .. text.front == ' ' ? 1 : 4)
() @trusted { dst.put("&nbsp;"); } (); // LockingTextWriter not @safe for DMD 2.066
text.popFront();
}
m_logFile.write(`</div>`);
m_logFile.writeln(`</div>`);
() @trusted { filterHTMLEscape(dst, text); } (); // LockingTextWriter not @safe for DMD 2.066
}

override void endLine()
{
() @trusted { // not @safe for DMD 2.066
m_logFile.write(`</div>`);
m_logFile.writeln(`</div>`);
} ();
m_logFile.flush();
}

Expand Down

0 comments on commit b5a1e37

Please sign in to comment.