2 * log - logging functions
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 public class Waypoint : Object
34 if (gps.gps.fix_status() != libgps.STATUS_NO_FIX)
36 lat = gps.gps.info().fix.latitude;
37 lon = gps.gps.info().fix.longitude;
38 ts = (time_t)gps.gps.info().fix.time;
40 // Use 1000 as missing values
47 public void writeInside(FileStream outfd)
50 outfd.printf(" <time>%s</time>\n", t.format("%Y-%m-%dT%H:%M:%SZ"));
55 public class LogEntry : Waypoint
64 public void write(FileStream outfd)
66 outfd.printf(" <wpt lat=\"%f\" lon=\"%f\">\n", lat, lon);
68 outfd.printf(" <name>%s</name>\n", Markup.escape_text(msg));
69 outfd.puts(" </wpt>\n");
73 public class TrackEntry : Waypoint
80 public void write(FileStream outfd)
82 outfd.printf(" <trkpt lat=\"%f\" lon=\"%f\">\n", lat, lon);
84 outfd.puts(" </trkpt>\n");
89 public class Log : Object
94 public List<LogEntry> entries;
95 public List<TrackEntry> track;
97 public Log(uint id, string tag, string title)
106 public void add(string msg)
108 var entry = new LogEntry();
110 entries.append(entry);
113 public void add_trackpoint()
115 track.append(new TrackEntry());
120 if (entries == null) return;
122 // Directory where we save the log
123 string dir = config.homedir + "/log-" + tag;
124 DirUtils.create(dir, 0777);
126 // First try with a plain name
127 var t = Time.local(entries.data.ts);
128 string basename = dir + "/" + t.format("%Y%m%d-%H%M%S");
130 string pathname = basename + ".gpx";
132 // Find a pathname that does not exist already
133 for (int i = 1; FileUtils.test(pathname, FileTest.EXISTS); ++i)
134 pathname = "%s-%d.gpx".printf(basename, i);
137 var outfd = FileStream.open(pathname, "w");
140 zavai.log.error("opening " + pathname + ": " + strerror(errno));
148 protected void writeTrack(FileStream outfd)
150 outfd.puts(" <trk>\n");
151 outfd.puts(" <trkseg>\n");
152 for (weak List<TrackEntry> i = track; i != null; i = i.next)
154 outfd.puts(" </trkseg>\n");
155 outfd.puts(" </trk>\n");
158 protected void writeEntries(FileStream outfd)
160 outfd.puts(" <wpt>\n");
161 for (weak List<LogEntry> i = entries; i != null; i = i.next)
163 outfd.puts(" </wpt>\n");
166 protected void write(FileStream outfd)
168 outfd.puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
169 outfd.puts("<gpx version=\"1.0\"\n");
170 outfd.printf(" creator=\"zavai %s\"\n", zavai.config.version);
171 outfd.puts(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
172 outfd.puts(" xmlns=\"http://www.topografix.com/GPX/1/0\"\n");
173 outfd.puts(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
174 outfd.puts(" <metadata>\n");
175 outfd.printf(" <name>%s</name>\n", Markup.escape_text(title));
176 outfd.puts(" </metadata>\n");
177 if (track != null) writeTrack(outfd);
178 if (entries != null) writeEntries(outfd);
179 outfd.puts(" </gpx>\n");
183 public class Logger : Resource, Object
185 protected List<Log> logs;
193 zavai.registry.register(this);
196 protected void start_trace()
198 gps.gps.pos_changed += on_pos_changed;
201 protected void end_trace()
203 gps.gps.pos_changed -= on_pos_changed;
206 protected void on_pos_changed()
208 for (weak List<Log> i = logs; i != null; i = i.next)
209 i.data.add_trackpoint();
212 protected uint gen_seq()
214 // Increase avoiding 0 on rollover
217 if (++seq == 0) ++seq;
219 for (weak List<Log> i = logs; i != null; i = i.next)
221 if (i.data.id == seq)
232 protected weak Log? find(uint id)
234 for (weak List<Log> i = logs; i != null; i = i.next)
240 protected Log? pop(uint id)
242 for (weak List<Log> i = logs; i != null; i = i.next)
252 public uint start(string tag, string title)
254 bool was_empty = (logs == null);
256 logs.append(new Log(id, tag, title));
257 if (was_empty) start_trace();
261 public void add(uint id, string msg)
264 if (log == null) return;
268 public void end(uint id)
272 if (log == null) end_trace();
275 public void instant(string tag, string msg)
277 var log = new Log(0, tag, msg);
282 public void shutdown()
286 var log = pop(logs.data.id);
292 public void error(string s)
294 stderr.printf("%s\n", s);
296 public void warning(string s)
298 stderr.printf("%s\n", s);
300 public void info(string s)
302 stderr.printf("%s\n", s);
304 public void debug(string s)
306 stderr.printf("%s\n", s);