Okay, get rid of WvAutoCast.iter() altogether; use GetEnumerator().
[versaplex.git] / versaplexd / vxschemaerrors.cs
blob88c2b9a07acdd4cb4d44342de6aec99267024d55
1 using System;
2 using System.Text;
3 using System.Collections.Generic;
4 using Wv;
6 internal class VxSchemaError
8 // The key of the element that had the error
9 public string key;
10 // The error message
11 public string msg;
12 // The SQL error number, or -1 if not applicable.
13 public int errnum;
14 public WvLog.L level;
16 // Default to a level of Error;
17 public VxSchemaError(string newkey, string newmsg, int newerrnum)
19 key = newkey;
20 msg = newmsg;
21 errnum = newerrnum;
22 level = WvLog.L.Error;
25 public VxSchemaError(string newkey, string newmsg, int newerrnum,
26 WvLog.L newlevel)
28 key = newkey;
29 msg = newmsg;
30 errnum = newerrnum;
31 level = newlevel;
34 public VxSchemaError(VxSchemaError other)
36 key = other.key;
37 msg = other.msg;
38 errnum = other.errnum;
39 level = other.level;
42 public VxSchemaError(MessageReader reader)
44 key = reader.ReadString();
45 msg = reader.ReadString();
46 errnum = reader.ReadInt32();
47 int intlevel = reader.ReadInt32();
48 // Note: C# lets you cast an invalid value to an enum without an
49 // exception, we have to check this ourselves. Default to
50 // Critical (i.e. 0) if someone sends us something unexpected.
51 if (Enum.IsDefined(typeof(WvLog.L), intlevel))
52 level = (WvLog.L)intlevel;
53 else
54 level = WvLog.L.Critical;
57 public void WriteError(MessageWriter writer)
59 writer.Write(key);
60 writer.Write(msg);
61 writer.Write(errnum);
62 writer.Write((int)level);
65 public override string ToString()
67 return String.Format("{0} {1}: {2} ({3})", key, level, msg, errnum);
70 public static string GetDbusSignature()
72 return "ssii";
76 internal class VxSchemaErrors : Dictionary<string, List<VxSchemaError>>
78 public VxSchemaErrors()
82 public VxSchemaErrors(MessageReader reader)
84 reader.ReadArrayFunc(8, (r) => {
85 VxSchemaError err = new VxSchemaError(r);
86 this.Add(err.key, err);
87 });
90 public void Add(string key, VxSchemaError val)
92 if (!this.ContainsKey(key))
94 var list = new List<VxSchemaError>();
95 list.Add(val);
96 this.Add(key, list);
98 else
99 this[key].Add(val);
102 public void Add(VxSchemaErrors other)
104 foreach (var kvp in other)
106 var list = new List<VxSchemaError>();
107 foreach (var elem in kvp.Value)
108 list.Add(new VxSchemaError(elem));
109 this.Add(kvp.Key, list);
113 private void _WriteErrors(MessageWriter writer)
115 foreach (var kvp in this)
116 foreach (VxSchemaError err in kvp.Value)
118 writer.WritePad(8);
119 err.WriteError(writer);
123 // Static so we can properly write an empty array for a null object.
124 public static void WriteErrors(MessageWriter writer, VxSchemaErrors errs)
126 writer.WriteDelegatePrependSize(delegate(MessageWriter w)
128 if (errs != null)
129 errs._WriteErrors(w);
130 }, 8);
133 public static string GetDbusSignature()
135 return String.Format("a({0})", VxSchemaError.GetDbusSignature());
138 public override string ToString()
140 StringBuilder sb = new StringBuilder();
141 foreach (var kvp in this)
142 foreach (var err in kvp.Value)
143 sb.Append(err.ToString() + "\n");
144 return sb.ToString();