Add copyright notices to all source files and put a license in LICENSE.
[versaplex.git] / wvdotnet / wvlog.cs
blobf63c669efdb020bf98bf7b1696a14c5452055836
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.IO;
8 using System.Threading;
9 using Wv.Extensions;
11 namespace Wv
13 public abstract class WvLogRcv
15 string open_header = null;
16 byte[] nl = "\n".ToUTF8();
18 protected abstract void w(WvBytes b);
20 void w(string s)
22 w(s.ToUTF8());
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
29 if (outbuf.used > 0
30 && open_header != null && open_header != header)
32 w(nl);
33 open_header = null;
36 // zero or more full lines (terminated by newlines)
37 int i;
38 while ((i = outbuf.strchr('\n')) > 0)
40 if (open_header == null)
41 w(header);
42 w(outbuf.get(i));
43 open_header = null;
46 // end-of-buffer partial line (ie. no newline terminator yet)
47 if (outbuf.used > 0)
49 if (open_header == null)
50 w(header);
51 w(outbuf.get(outbuf.used));
52 open_header = header;
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
69 WvStream outstr;
71 public WvLogStream(WvStream outstr)
73 this.outstr = outstr;
76 protected override void w(WvBytes b)
78 outstr.write(b);
82 public class WvLogFile : WvLogStream
84 public WvLogFile(string filename, string filemode)
85 : base(new WvFile(filename, filemode))
90 public class WvLog : WvStream
92 public enum L {
93 Critical = 0,
94 Error,
95 Warning,
96 Notice,
97 Info,
98 Debug, Debug1=Debug,
99 Debug2,
100 Debug3,
101 Debug4,
102 Debug5,
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();
112 string name;
113 L level;
115 string levelstr(L level)
117 switch (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";
129 default:
130 wv.assert(false, "Unknown loglevel??");
131 return "??";
135 public WvLog(string name, L level)
137 this.name = name;
138 this.level = level;
141 public WvLog(string name)
142 : this(name, L.Info)
145 public override int write(WvBytes b)
147 if (level > maxlevel)
148 return b.len; // pretend it's written
150 WvBuf outbuf = new WvBuf();
151 outbuf.put(b);
152 recv.writelines(wv.fmt("{0}<{1}>: ", name, levelstr(level)),
153 outbuf);
154 return b.len;
157 public void print(L level, object s)
159 L old = this.level;
160 try {
161 this.level = level;
162 print(s);
164 finally {
165 this.level = old;
169 public void print(L level, string fmt, params object[] args)
171 if (level > maxlevel)
172 return;
173 print(level, (object)String.Format(fmt, args));
176 public override void print(object o)
178 if (level > maxlevel)
179 return;
180 base.print(o);
183 public WvLog split(L level)
185 return new WvLog(name, level);