3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
8 using System
.Threading
;
13 public abstract class WvLogRcv
15 string open_header
= null;
16 byte[] nl
= "\n".ToUTF8();
18 protected abstract void w(WvBytes b
);
25 // note: outbuf is always completely empty when we return
26 public void writelines(string header
, WvBuf outbuf
)
28 // force ending of previous partial line, if necessary
30 && open_header
!= null && open_header
!= header
)
36 // zero or more full lines (terminated by newlines)
38 while ((i
= outbuf
.strchr('\n')) > 0)
40 if (open_header
== null)
46 // end-of-buffer partial line (ie. no newline terminator yet)
49 if (open_header
== null)
51 w(outbuf
.get(outbuf
.used
));
57 public class WvLogConsole
: WvLogRcv
59 Stream outstr
= Console
.OpenStandardError();
61 protected override void w(WvBytes b
)
63 outstr
.Write(b
.bytes
, b
.start
, b
.len
);
67 public class WvLogStream
: WvLogRcv
71 public WvLogStream(WvStream outstr
)
76 protected override void w(WvBytes b
)
82 public class WvLogFile
: WvLogStream
84 public WvLogFile(string filename
, string filemode
)
85 : base(new WvFile(filename
, filemode
))
90 public class WvLog
: WvStream
105 static L _maxlevel
= L
.Info
;
106 public static L maxlevel
{
107 get { return _maxlevel; }
108 set { _maxlevel = value; }
110 public static WvLogRcv recv
= new WvLogConsole();
115 string levelstr(L level
)
119 case L
.Critical
: return "!";
120 case L
.Error
: return "E";
121 case L
.Warning
: return "W";
122 case L
.Notice
: return "N";
123 case L
.Info
: return "I";
124 case L
.Debug1
: return "1";
125 case L
.Debug2
: return "2";
126 case L
.Debug3
: return "3";
127 case L
.Debug4
: return "4";
128 case L
.Debug5
: return "5";
130 wv
.assert(false, "Unknown loglevel??");
135 public WvLog(string name
, L level
)
141 public WvLog(string name
)
145 public override int write(WvBytes b
)
147 if (level
> maxlevel
)
148 return b
.len
; // pretend it's written
150 WvBuf outbuf
= new WvBuf();
152 recv
.writelines(wv
.fmt("{0}<{1}>: ", name
, levelstr(level
)),
157 public void print(L level
, object s
)
169 public void print(L level
, string fmt
, params object[] args
)
171 if (level
> maxlevel
)
173 print(level
, (object)String
.Format(fmt
, args
));
176 public override void print(object o
)
178 if (level
> maxlevel
)
183 public WvLog
split(L level
)
185 return new WvLog(name
, level
);