| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp { |
| 4 |
|
| 5 |
public class XmppLog { |
| 6 |
protected const string ANSI_COLOR_END = "\x1b[0m"; |
| 7 |
protected const string ANSI_COLOR_WHITE = "\x1b[37;1m"; |
| 8 |
|
| 9 |
class NodeLogDesc { |
| 10 |
public string? name; |
| 11 |
private string? ns_uri; |
| 12 |
private string? val; |
| 13 |
private Map<string, string?> attrs = new HashMap<string, string?>(); |
| 14 |
private NodeLogDesc? inner; |
| 15 |
|
| 16 |
public NodeLogDesc(string desc) { |
| 17 |
string d = desc; |
| 18 |
|
| 19 |
if (d.contains("[")) { |
| 20 |
int start = d.index_of("["); |
| 21 |
int end = d.index_of("]"); |
| 22 |
string attrs = d.substring(start + 1, end - start - 1); |
| 23 |
d = d.substring(0, start) + d.substring(end + 1); |
| 24 |
foreach (string attr in attrs.split(",")) { |
| 25 |
if (attr.contains("=")) { |
| 26 |
string key = attr.substring(0, attr.index_of("=")); |
| 27 |
string val = attr.substring(attr.index_of("=") + 1); |
| 28 |
this.attrs[key] = val; |
| 29 |
} else { |
| 30 |
this.attrs[attr] = null; |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
if (d.contains(":") && d.index_of("{") == 0 && d.index_of("}") != -1) { |
| 35 |
int end = d.index_of("}"); |
| 36 |
this.ns_uri = d.substring(1, end - 1); |
| 37 |
d = d.substring(end + 2); |
| 38 |
} |
| 39 |
if (d.contains(".")) { |
| 40 |
inner = new NodeLogDesc(d.substring(d.index_of(".") + 1)); |
| 41 |
d = d.substring(0, d.index_of(".")); |
| 42 |
} else if (d.contains("=")) { |
| 43 |
this.val = d.substring(d.index_of("=")); |
| 44 |
d = d.substring(0, d.index_of("=")); |
| 45 |
} |
| 46 |
|
| 47 |
if (d != "") this.name = d; |
| 48 |
} |
| 49 |
|
| 50 |
public bool matches(StanzaNode node) { |
| 51 |
if (name != null && node.name != name) return false; |
| 52 |
if (ns_uri != null && node.ns_uri != ns_uri) return false; |
| 53 |
if (val != null && node.val != val) return false; |
| 54 |
foreach (var pair in attrs.entries) { |
| 55 |
if (pair.value == null && node.get_attribute(pair.key) == null) return false; |
| 56 |
else if (pair.value != null && pair.value != node.get_attribute(pair.key)) return false; |
| 57 |
} |
| 58 |
if (inner == null) return true; |
| 59 |
foreach (StanzaNode snode in node.get_all_subnodes()) { |
| 60 |
if (((!)inner).matches(snode)) return true; |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
private bool use_ansi; |
| 67 |
private bool hide_ns = true; |
| 68 |
private string ident; |
| 69 |
private string desc; |
| 70 |
private Gee.List<NodeLogDesc> descs = new ArrayList<NodeLogDesc>(); |
| 71 |
|
| 72 |
public XmppLog(string? ident = null, string? desc = null) { |
| 73 |
this.ident = ident ?? ""; |
| 74 |
this.desc = desc ?? ""; |
| 75 |
this.use_ansi = is_atty(stderr.fileno()); |
| 76 |
while (this.desc.contains(";")) { |
| 77 |
string opt = this.desc.substring(0, this.desc.index_of(";")); |
| 78 |
this.desc = this.desc.substring(opt.length + 1); |
| 79 |
switch (opt) { |
| 80 |
case "ansi": use_ansi = true; break; |
| 81 |
case "no-ansi": use_ansi = false; break; |
| 82 |
case "hide-ns": hide_ns = true; break; |
| 83 |
case "show-ns": hide_ns = false; break; |
| 84 |
} |
| 85 |
} |
| 86 |
if (desc != "") { |
| 87 |
foreach (string d in this.desc.split("|")) { |
| 88 |
descs.add(new NodeLogDesc(d)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public virtual bool should_log_node(StanzaNode node) { |
| 94 |
if (ident == "" || desc == "") return false; |
| 95 |
if (desc == "all") return true; |
| 96 |
foreach (var desc in descs) { |
| 97 |
if (desc.matches(node)) return true; |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
public virtual bool should_log_str(string str) { |
| 103 |
if (ident == "" || desc == "") return false; |
| 104 |
if (desc == "all") return true; |
| 105 |
foreach (var desc in descs) { |
| 106 |
if (desc.name == "#text") return true; |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
public void node(string what, StanzaNode node, XmppStream stream) { |
| 112 |
if (should_log_node(node)) { |
| 113 |
stderr.printf("%sXMPP %s [%s stream:%p thread:%p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, Thread.self<Thread>(), new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", use_ansi ? node.to_ansi_string(hide_ns) : node.to_string()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public void str(string what, string str, XmppStream stream) { |
| 118 |
if (should_log_str(str)) { |
| 119 |
stderr.printf("%sXMPP %s [%s stream:%p thread:%p %s]%s\n%s\n", use_ansi ? ANSI_COLOR_WHITE : "", what, ident, stream, Thread.self<Thread>(), new DateTime.now_local().to_string(), use_ansi ? ANSI_COLOR_END : "", str); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
[CCode (cname = "isatty")] |
| 124 |
private static extern bool is_atty(int fd); |
| 125 |
} |
| 126 |
|
| 127 |
} |