2 * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 %module "Amanda::Logfile"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24 %include "amglue/dumpspecs.swg"
25 %import "Amanda/Cmdline.swg"
27 %include "Amanda/Logfile.pod"
33 #include "diskfile.h" /* for the gross hack, below */
37 open_logfile get_logline close_logfile
42 amglue_add_enum_tag_fns(logtype_t);
43 amglue_add_constant(L_BOGUS, logtype_t);
44 amglue_add_constant(L_FATAL, logtype_t);
45 amglue_add_constant(L_ERROR, logtype_t);
46 amglue_add_constant(L_WARNING, logtype_t);
47 amglue_add_constant(L_INFO, logtype_t);
48 amglue_add_constant(L_SUMMARY, logtype_t);
49 amglue_add_constant(L_START, logtype_t);
50 amglue_add_constant(L_FINISH, logtype_t);
51 amglue_add_constant(L_DISK, logtype_t);
52 amglue_add_constant(L_DONE, logtype_t);
53 amglue_add_constant(L_PART, logtype_t);
54 amglue_add_constant(L_PARTPARTIAL, logtype_t);
55 amglue_add_constant(L_SUCCESS, logtype_t);
56 amglue_add_constant(L_PARTIAL, logtype_t);
57 amglue_add_constant(L_FAIL, logtype_t);
58 amglue_add_constant(L_STRANGE, logtype_t);
59 amglue_add_constant(L_CHUNK, logtype_t);
60 amglue_add_constant(L_CHUNKSUCCESS, logtype_t);
61 amglue_add_constant(L_STATS, logtype_t);
62 amglue_add_constant(L_MARKER, logtype_t);
63 amglue_add_constant(L_CONT, logtype_t);
64 amglue_copy_to_tag(logtype_t, constants);
66 amglue_add_enum_tag_fns(program_t);
67 amglue_add_constant(P_UNKNOWN, program_t);
68 amglue_add_constant(P_PLANNER, program_t);
69 amglue_add_constant(P_DRIVER, program_t);
70 amglue_add_constant(P_REPORTER, program_t);
71 amglue_add_constant(P_DUMPER, program_t);
72 amglue_add_constant(P_CHUNKER, program_t);
73 amglue_add_constant(P_TAPER, program_t);
74 amglue_add_constant(P_AMFLUSH, program_t);
75 amglue_add_constant(P_AMDUMP, program_t);
76 amglue_add_constant(P_AMIDXTAPED, program_t);
77 amglue_add_constant(P_AMFETCHDUMP, program_t);
78 amglue_add_constant(P_AMCHECKDUMP, program_t);
79 amglue_copy_to_tag(program_t, constants);
81 /* TODO: support for writing logfiles is omitted for the moment. */
84 /* open_ and close_logfile are both simple wrappers around fopen/fclose. */
85 typedef FILE loghandle;
87 static loghandle *open_logfile(char *filename) {
88 return fopen(filename, "r");
93 static void close_logfile(loghandle *logfile) {
94 if (logfile) fclose(logfile);
98 /* We fake the return type of get_logline, and use a typemap to
99 * slurp curstr, curprog, and curlog into a return value. */
101 typedef int LOGLINE_RETURN;
103 %typemap(out) LOGLINE_RETURN {
106 $result = sv_2mortal(newSViv(curlog));
108 $result = sv_2mortal(newSViv(curprog));
110 $result = sv_2mortal(newSVpv(curstr, 0));
113 /* otherwise (end of logfile) return an empty list */
115 LOGLINE_RETURN get_logline(FILE *logfile);
117 %rename(log_add) log_add_;
119 static void log_add_(logtype_t typ, char *message)
121 log_add(typ, "%s", message);
129 find_result_t *selfp = self;
130 free_find_result(&selfp);
152 /* This typemap is used in a few functions. It converts a linked list of find_result_t's
153 * into an array of same, de-linking the list in the process. This gives ownership of the
154 * objects to perl, which is consistent with the C interface to this module.
156 %typemap(out) find_result_t * {
160 /* measure the list and make room on the perl stack */
161 for (len=0, iter=$1; iter; iter=iter->next) len++;
167 /* Let SWIG take ownership of the object */
168 $result = SWIG_NewPointerObj(iter, $descriptor(find_result_t *), SWIG_OWNER | SWIG_SHADOW);
171 /* null out the 'next' field */
178 /* Similarly, on input we link an array full of find_result_t's. The list is then
179 * unlinked on return. Note that the array is supplied as an arrayref (since it's
180 * usually the first argument).
182 %typemap(in) find_result_t * {
185 find_result_t *head = NULL, *tail = NULL;
187 if (!SvROK($input) || SvTYPE(SvRV($input)) != SVt_PVAV) {
188 SWIG_exception(SWIG_TypeError, "expected an arrayref of find_result_t's");
191 av = (AV *)SvRV($input);
192 len = av_len(av) + 1;
194 for (i = 0; i < len; i++) {
195 SV **val = av_fetch(av, i, 0);
198 if (!val || SWIG_ConvertPtr(*val, (void **)&r, $descriptor(find_result_t *), 0) == -1) {
199 SWIG_exception(SWIG_TypeError, "array member is not a find_result_t");
212 /* point to the head of that list */
216 %typemap(freearg) find_result_t * {
217 find_result_t *iter = $1, *next;
219 /* undo all the links we added earlier */
227 %typemap(out) char ** {
231 /* measure the length of the array and make sure perl has enough room */
232 for (len=0, iter=$1; *iter; iter++) len++;
235 /* now copy it to the perl stack */
236 for (i=0, iter=$1; *iter; iter++, i++) {
237 $result = sv_2mortal(newSVpv(*iter, 0));
243 find_log search_logfile dumps_match
244 match_host match_disk match_datestamp match_level
247 char **find_log(void);
249 %rename(search_logfile) search_logfile_wrap;
251 static find_result_t *search_logfile_wrap(char *label, char *datestamp,
252 char *logfile, int add_missing_disks) {
253 find_result_t *rv = NULL;
255 /* We use a static variable to collect any unrecognized disks */
256 static disklist_t unrecognized_disks = { NULL, NULL };
258 search_logfile(&rv, label, datestamp, logfile,
259 add_missing_disks? &unrecognized_disks : NULL);
265 %rename(search_holding_disk) search_holding_disk_wrap;
267 static find_result_t *search_holding_disk_wrap(void) {
268 find_result_t *rv = NULL;
269 static disklist_t unrecognized_disks = { NULL, NULL };
270 search_holding_disk(&rv, &unrecognized_disks);
275 find_result_t *dumps_match(find_result_t *output_find, char *hostname,
276 char *diskname, char *datestamp, char *level, int ok);
278 find_result_t *dumps_match_dumpspecs(find_result_t *output_find,
279 amglue_dumpspec_list *dumpspecs,
282 /* these are actually available for clients as well, but they do not deserve
283 * their own perl module, so they're stuck here */
284 gboolean match_host(char *pat, char *value);
285 gboolean match_disk(char *pat, char *value);
286 gboolean match_datestamp(char *pat, char *value);
287 gboolean match_level(char *pat, char *value);
290 amanda_log_handler_t *amanda_log_trace_log;
293 $amanda_log_trace_log
298 find_all_logs find_latest_log
305 my $logdir = shift @_ || config_dir_relative(getconf($CNF_LOGDIR));
307 opendir my $logdh, $logdir or die("can't read $logdir");
308 my @logfiles = sort grep { m{^log\.\d+\.\d+$} } readdir $logdh;
315 my $logdir = shift @_;
316 my @logs = find_all_logs($logdir || ());