1 /* log.c - Diagnostic and error logging
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
6 * x3 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 3 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 srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 #include "helpfile.h" /* send_message, message_register, etc */
28 #define MAXLOGSEARCHLENGTH 10000
30 struct userNode
*chanserv
;
31 struct logDestination
;
33 struct logDest_vtable
{
34 const char *type_name
;
35 struct logDestination
* (*open
)(const char *args
);
36 void (*reopen
)(struct logDestination
*self
);
37 void (*close
)(struct logDestination
*self
);
38 void (*log_audit
)(struct logDestination
*self
, struct log_type
*type
, struct logEntry
*entry
);
39 void (*log_replay
)(struct logDestination
*self
, struct log_type
*type
, int is_write
, const char *line
);
40 void (*log_module
)(struct logDestination
*self
, struct log_type
*type
, enum log_severity sev
, const char *message
);
43 struct logDestination
{
44 struct logDest_vtable
*vtbl
;
49 DECLARE_LIST(logList
, struct logDestination
*);
53 struct logList logs
[LOG_NUM_SEVERITIES
];
54 struct logEntry
*log_oldest
;
55 struct logEntry
*log_newest
;
56 unsigned int log_count
;
58 unsigned int max_count
;
59 unsigned int default_set
: 1;
62 static const char *log_severity_names
[] = {
75 static struct dict
*log_dest_types
;
76 static struct dict
*log_dests
;
77 static struct dict
*log_types
;
78 static struct log_type
*log_default
;
79 static int log_inited
, log_debugged
;
81 DEFINE_LIST(logList
, struct logDestination
*);
82 static void log_format_audit(struct logEntry
*entry
);
83 static const struct message_entry msgtab
[] = {
84 { "MSG_INVALID_FACILITY", "$b%s$b is an invalid log facility." },
85 { "MSG_INVALID_SEVERITY", "$b%s$b is an invalid severity level." },
87 { "LAST_RESULTS", "$b%s$b] %s %s $b%s$b %s" },
88 { "LAST_ERROR", "%s:%s" },
89 { "LAST_COMMAND_LOG", "Channel Events for %s" },
90 { "LAST_LINE", "----------------------------------------" },
91 { "LAST_STOPPING_AT", "--------- Stopping at %d lines ---------" },
92 { "LAST_MAX_AGE", "-------- Data age limit reached --------" },
93 { "LAST_END_OF_LOG", "---------- Found %d Matches ------------" },
98 static struct logDestination
*
99 log_open(const char *name
)
101 struct logDest_vtable
*vtbl
;
102 struct logDestination
*ld
;
106 if ((ld
= dict_find(log_dests
, name
, NULL
))) {
110 if ((sep
= strchr(name
, ':'))) {
111 memcpy(type_name
, name
, sep
-name
);
112 type_name
[sep
-name
] = 0;
114 strcpy(type_name
, name
);
116 if (!(vtbl
= dict_find(log_dest_types
, type_name
, NULL
))) {
117 log_module(MAIN_LOG
, LOG_ERROR
, "Invalid log type for log '%s'.", name
);
120 if (!(ld
= vtbl
->open(sep
? sep
+1 : 0)))
122 ld
->name
= strdup(name
);
123 dict_insert(log_dests
, ld
->name
, ld
);
129 logList_open(struct logList
*ll
, struct record_data
*rd
)
131 struct logDestination
*ld
;
138 if ((ld
= log_open(rd
->d
.qstring
)))
139 logList_append(ll
, ld
);
141 case RECDB_STRING_LIST
:
142 for (ii
=0; ii
<rd
->d
.slist
->used
; ++ii
) {
143 if ((ld
= log_open(rd
->d
.slist
->list
[ii
])))
144 logList_append(ll
, ld
);
153 logList_join(struct logList
*target
, const struct logList
*source
)
155 unsigned int ii
, jj
, kk
;
160 target
->used
+= source
->used
;
161 target
->size
+= source
->used
;
162 target
->list
= realloc(target
->list
, target
->size
* sizeof(target
->list
[0]));
163 for (ii
= 0; ii
< source
->used
; ++ii
, ++jj
) {
165 for (dup
= 0, kk
= 0; kk
< jj
; kk
++) {
166 if (target
->list
[kk
] == source
->list
[ii
]) {
176 target
->list
[jj
] = source
->list
[ii
];
177 target
->list
[jj
]->refcnt
++;
182 logList_close(struct logList
*ll
)
185 for (ii
=0; ii
<ll
->used
; ++ii
) {
186 if (!--ll
->list
[ii
]->refcnt
) {
187 struct logDestination
*ld
= ll
->list
[ii
];
199 enum log_severity ls
;
201 for (it
= dict_first(log_types
); it
; it
= iter_next(it
)) {
203 for (ls
= 0; ls
< LOG_NUM_SEVERITIES
; ls
++) {
204 logList_close(<
->logs
[ls
]);
206 lt
->logs
[ls
].size
= 0;
207 lt
->logs
[ls
].used
= 0;
208 lt
->logs
[ls
].list
= 0;
214 log_type_free_oldest(struct log_type
*lt
)
216 struct logEntry
*next
;
220 next
= lt
->log_oldest
->next
;
221 free(lt
->log_oldest
->default_desc
);
222 free(lt
->log_oldest
);
223 lt
->log_oldest
= next
;
228 log_type_free(void *ptr
)
230 struct log_type
*lt
= ptr
;
232 while (lt
->log_oldest
)
233 log_type_free_oldest(lt
);
242 dict_delete(log_types
);
243 dict_delete(log_dests
);
244 dict_delete(log_dest_types
);
247 static enum log_severity
248 find_severity(const char *text
)
250 enum log_severity ls
;
251 for (ls
= 0; ls
< LOG_NUM_SEVERITIES
; ++ls
)
252 if (!ircncasecmp(text
, log_severity_names
[ls
], strlen(log_severity_names
[ls
])))
254 return LOG_NUM_SEVERITIES
;
257 /* Log keys are based on syslog.conf syntax:
258 * KEY := LOGSET '.' SEVSET
259 * LOGSET := LOGLIT | LOGLIT ',' LOGSET
260 * LOGLIT := a registered log type
261 * SEVSET := '*' | SEVLIT | '<' SEVLIT | '<=' SEVLIT | '>' SEVLIT | '>=' SEVLIT | SEVLIT ',' SEVSET
262 * SEVLIT := one of log_severity_names
263 * A KEY contains the Cartesian product of the logs in its LOGSET
264 * and the severities in its SEVSET.
268 log_parse_logset(char *buffer
, struct string_list
*slist
)
272 char *cont
= strchr(buffer
, ',');
275 string_list_append(slist
, strdup(buffer
));
281 log_parse_sevset(char *buffer
, char targets
[LOG_NUM_SEVERITIES
])
283 memset(targets
, 0, LOG_NUM_SEVERITIES
);
286 enum log_severity bound
;
289 cont
= strchr(buffer
, ',');
292 if (buffer
[0] == '*' && buffer
[1] == 0) {
293 for (bound
= 0; bound
< LOG_NUM_SEVERITIES
; bound
++) {
294 /* make people explicitly specify replay targets */
295 if (bound
!= LOG_REPLAY
)
298 } else if (buffer
[0] == '<') {
299 if (buffer
[1] == '=')
300 bound
= find_severity(buffer
+2) + 1;
302 bound
= find_severity(buffer
+1);
303 for (first
= 1; bound
> 0; bound
--) {
304 /* make people explicitly specify replay targets */
305 if (bound
!= LOG_REPLAY
|| first
) {
310 } else if (buffer
[0] == '>') {
311 if (buffer
[1] == '=')
312 bound
= find_severity(buffer
+2);
314 bound
= find_severity(buffer
+1) + 1;
315 for (first
= 1; bound
< LOG_NUM_SEVERITIES
; bound
++) {
316 /* make people explicitly specify replay targets */
317 if (bound
!= LOG_REPLAY
|| first
) {
323 if (buffer
[0] == '=')
325 bound
= find_severity(buffer
);
333 log_parse_cross(const char *buffer
, struct string_list
*types
, char sevset
[LOG_NUM_SEVERITIES
])
337 dup
= strdup(buffer
);
338 sep
= strchr(dup
, '.');
340 log_parse_logset(dup
, types
);
341 log_parse_sevset(sep
, sevset
);
346 log_parse_options(struct log_type
*type
, struct dict
*conf
)
349 opt
= database_get_data(conf
, "max_age", RECDB_QSTRING
);
351 type
->max_age
= ParseInterval(opt
);
352 opt
= database_get_data(conf
, "max_count", RECDB_QSTRING
);
354 type
->max_count
= strtoul(opt
, NULL
, 10);
360 struct record_data
*rd
, *rd2
;
363 struct log_type
*type
;
364 enum log_severity sev
;
368 dict_delete(log_dests
);
370 log_dests
= dict_new();
371 dict_set_free_keys(log_dests
, free
);
373 rd
= conf_get_node("logs");
374 if (rd
&& (rd
->type
== RECDB_OBJECT
)) {
375 for (it
= dict_first(rd
->d
.object
); it
; it
= iter_next(it
)) {
376 if ((sep
= strchr(iter_key(it
), '.'))) {
377 struct logList logList
;
378 char sevset
[LOG_NUM_SEVERITIES
];
379 struct string_list
*slist
;
381 /* It looks like a <type>.<severity> record. Try to parse it. */
382 slist
= alloc_string_list(4);
383 log_parse_cross(iter_key(it
), slist
, sevset
);
385 logList_open(&logList
, iter_data(it
));
386 for (ii
= 0; ii
< slist
->used
; ++ii
) {
387 type
= log_register_type(slist
->list
[ii
], NULL
);
388 for (sev
= 0; sev
< LOG_NUM_SEVERITIES
; ++sev
) {
391 logList_join(&type
->logs
[sev
], &logList
);
394 logList_close(&logList
);
395 free_string_list(slist
);
396 } else if ((rd2
= iter_data(it
))
397 && (rd2
->type
== RECDB_OBJECT
)
398 && (type
= log_register_type(iter_key(it
), NULL
))) {
399 log_parse_options(type
, rd2
->d
.object
);
401 log_module(MAIN_LOG
, LOG_ERROR
, "Unknown logs subkey '%s'.", iter_key(it
));
412 enum log_severity sev
;
413 struct logDestination
*log_stdout
;
414 struct logList target
;
416 log_stdout
= log_open("std:out");
417 logList_init(&target
);
418 logList_append(&target
, log_stdout
);
420 for (sev
= 0; sev
< LOG_NUM_SEVERITIES
; ++sev
)
421 logList_join(&log_default
->logs
[sev
], &target
);
423 logList_close(&target
);
431 for (it
= dict_first(log_dests
); it
; it
= iter_next(it
)) {
432 struct logDestination
*ld
= iter_data(it
);
433 ld
->vtbl
->reopen(ld
);
438 log_register_type(const char *name
, const char *default_log
)
440 struct log_type
*type
;
441 struct logDestination
*dest
;
442 enum log_severity sev
;
444 if (!(type
= dict_find(log_types
, name
, NULL
))) {
445 type
= calloc(1, sizeof(*type
));
446 type
->name
= strdup(name
);
448 type
->max_count
= 1024;
449 dict_insert(log_types
, type
->name
, type
);
451 if (default_log
&& !type
->default_set
) {
452 /* If any severity level was unspecified in the config, use the default. */
454 for (sev
= 0; sev
< LOG_NUM_SEVERITIES
; ++sev
) {
455 if (sev
== LOG_REPLAY
)
456 continue; /* never default LOG_REPLAY */
457 if (!type
->logs
[sev
].size
) {
458 logList_init(&type
->logs
[sev
]);
460 if (!(dest
= log_open(default_log
)))
464 logList_append(&type
->logs
[sev
], dest
);
468 type
->default_set
= 1;
473 /* logging functions */
476 log_audit(struct log_type
*type
, enum log_severity sev
, struct userNode
*user
, struct userNode
*bot
, const char *channel_name
, unsigned int flags
, const char *command
)
478 struct logEntry
*entry
;
479 unsigned int size
, ii
;
482 /* First make sure severity is appropriate */
483 if ((sev
!= LOG_COMMAND
) && (sev
!= LOG_OVERRIDE
) && (sev
!= LOG_STAFF
)) {
484 log_module(MAIN_LOG
, LOG_ERROR
, "Illegal audit severity %d", sev
);
487 /* Allocate and fill in the log entry */
488 size
= sizeof(*entry
) + strlen(user
->nick
) + strlen(command
) + 2;
489 if (user
->handle_info
)
490 size
+= strlen(user
->handle_info
->handle
) + 1;
492 size
+= strlen(channel_name
) + 1;
493 if (flags
& AUDIT_HOSTMASK
)
494 size
+= strlen(user
->ident
) + strlen(user
->hostname
) + 2;
495 entry
= calloc(1, size
);
496 str_next
= (char*)(entry
+ 1);
501 size
= strlen(channel_name
) + 1;
502 entry
->channel_name
= memcpy(str_next
, channel_name
, size
);
506 size
= strlen(user
->nick
) + 1;
507 entry
->user_nick
= memcpy(str_next
, user
->nick
, size
);
510 if (user
->handle_info
) {
511 size
= strlen(user
->handle_info
->handle
) + 1;
512 entry
->user_account
= memcpy(str_next
, user
->handle_info
->handle
, size
);
515 if (flags
& AUDIT_HOSTMASK
) {
516 size
= sprintf(str_next
, "%s@%s", user
->ident
, user
->hostname
) + 1;
517 entry
->user_hostmask
= str_next
;
520 entry
->user_hostmask
= 0;
523 size
= strlen(command
) + 1;
524 entry
->command
= memcpy(str_next
, command
, size
);
528 /* fill in the default text for the event */
529 log_format_audit(entry
);
531 /* insert into the linked list */
533 entry
->prev
= type
->log_newest
;
534 if (type
->log_newest
)
535 type
->log_newest
->next
= entry
;
537 type
->log_oldest
= entry
;
538 type
->log_newest
= entry
;
541 /* remove old elements from the linked list */
542 while (type
->log_count
> type
->max_count
)
543 log_type_free_oldest(type
);
544 while (type
->log_oldest
&& (type
->log_oldest
->time
+ (time_t)type
->max_age
< now
))
545 log_type_free_oldest(type
);
546 if (type
->log_oldest
)
547 type
->log_oldest
->prev
= 0;
549 type
->log_newest
= 0;
551 /* call the destination logs */
552 for (ii
=0; ii
<type
->logs
[sev
].used
; ++ii
) {
553 struct logDestination
*ld
= type
->logs
[sev
].list
[ii
];
554 ld
->vtbl
->log_audit(ld
, type
, entry
);
556 for (ii
=0; ii
<log_default
->logs
[sev
].used
; ++ii
) {
557 struct logDestination
*ld
= log_default
->logs
[sev
].list
[ii
];
558 ld
->vtbl
->log_audit(ld
, type
, entry
);
563 log_replay(struct log_type
*type
, int is_write
, const char *line
)
567 for (ii
=0; ii
<type
->logs
[LOG_REPLAY
].used
; ++ii
) {
568 struct logDestination
*ld
= type
->logs
[LOG_REPLAY
].list
[ii
];
569 ld
->vtbl
->log_replay(ld
, type
, is_write
, line
);
571 for (ii
=0; ii
<log_default
->logs
[LOG_REPLAY
].used
; ++ii
) {
572 struct logDestination
*ld
= log_default
->logs
[LOG_REPLAY
].list
[ii
];
573 ld
->vtbl
->log_replay(ld
, type
, is_write
, line
);
578 log_module(struct log_type
*type
, enum log_severity sev
, const char *format
, ...)
584 if (sev
> LOG_FATAL
) {
585 log_module(MAIN_LOG
, LOG_ERROR
, "Illegal log_module severity %d", sev
);
588 va_start(args
, format
);
589 vsnprintf(msgbuf
, sizeof(msgbuf
), format
, args
);
592 for (ii
=0; ii
<type
->logs
[sev
].used
; ++ii
) {
593 struct logDestination
*ld
= type
->logs
[sev
].list
[ii
];
594 ld
->vtbl
->log_module(ld
, type
, sev
, msgbuf
);
596 for (ii
=0; ii
<log_default
->logs
[sev
].used
; ++ii
) {
597 struct logDestination
*ld
= log_default
->logs
[sev
].list
[ii
];
598 ld
->vtbl
->log_module(ld
, type
, sev
, msgbuf
);
601 /* Special behavior before we start full operation */
602 fprintf(stderr
, "%s: %s\n", log_severity_names
[sev
], msgbuf
);
604 if (sev
== LOG_FATAL
) {
605 //assert(0 && "fatal message logged");
610 /* audit log searching */
613 log_discrim_create(struct userNode
*service
, struct userNode
*user
, unsigned int argc
, char *argv
[])
616 struct logSearch
*discrim
;
618 /* Assume all criteria require arguments. */
621 send_message(user
, service
, "MSG_MISSING_PARAMS", argv
[0]);
625 discrim
= malloc(sizeof(struct logSearch
));
626 memset(discrim
, 0, sizeof(*discrim
));
628 discrim
->max_time
= INT_MAX
;
629 discrim
->severities
= ~0;
631 for (ii
=1; ii
<argc
-1; ii
++) {
632 if (!irccasecmp(argv
[ii
], "bot")) {
633 struct userNode
*bot
= GetUserH(argv
[++ii
]);
635 send_message(user
, service
, "MSG_NICK_UNKNOWN", argv
[ii
]);
637 } else if (!IsLocal(bot
)) {
638 send_message(user
, service
, "MSG_NOT_A_SERVICE", argv
[ii
]);
641 discrim
->masks
.bot
= bot
;
642 } else if (!irccasecmp(argv
[ii
], "channel")) {
643 discrim
->masks
.channel_name
= argv
[++ii
];
644 } else if (!irccasecmp(argv
[ii
], "nick")) {
645 discrim
->masks
.user_nick
= argv
[++ii
];
646 } else if (!irccasecmp(argv
[ii
], "account")) {
647 discrim
->masks
.user_account
= argv
[++ii
];
648 } else if (!irccasecmp(argv
[ii
], "hostmask")) {
649 discrim
->masks
.user_hostmask
= argv
[++ii
];
650 } else if (!irccasecmp(argv
[ii
], "command")) {
651 discrim
->masks
.command
= argv
[++ii
];
652 } else if (!irccasecmp(argv
[ii
], "age")) {
653 const char *cmp
= argv
[++ii
];
656 discrim
->min_time
= now
- ParseInterval(cmp
+2);
658 discrim
->min_time
= now
- (ParseInterval(cmp
+1) - 1);
659 } else if (cmp
[0] == '>') {
661 discrim
->max_time
= now
- ParseInterval(cmp
+2);
663 discrim
->max_time
= now
- (ParseInterval(cmp
+1) - 1);
665 discrim
->min_time
= now
- ParseInterval(cmp
+2);
667 } else if (!irccasecmp(argv
[ii
], "limit")) {
668 discrim
->limit
= strtoul(argv
[++ii
], NULL
, 10);
669 } else if (!irccasecmp(argv
[ii
], "level")) {
670 char *severity
= argv
[++ii
];
671 discrim
->severities
= 0;
673 enum log_severity sev
= find_severity(severity
);
674 if (sev
== LOG_NUM_SEVERITIES
) {
675 send_message(user
, service
, "MSG_INVALID_SEVERITY", severity
);
678 discrim
->severities
|= 1 << sev
;
679 severity
= strchr(severity
, ',');
684 } else if (!irccasecmp(argv
[ii
], "type")) {
685 if (!(discrim
->type
= dict_find(log_types
, argv
[++ii
], NULL
))) {
686 send_message(user
, service
, "MSG_INVALID_FACILITY", argv
[ii
]);
690 send_message(user
, service
, "MSG_INVALID_CRITERIA", argv
[ii
]);
702 entry_match(struct logSearch
*discrim
, struct logEntry
*entry
)
704 if ((entry
->time
< discrim
->min_time
)
705 || (entry
->time
> discrim
->max_time
)
706 || !(discrim
->severities
& (1 << entry
->slvl
))
707 || (discrim
->masks
.bot
&& (discrim
->masks
.bot
!= entry
->bot
))
708 /* don't do glob matching, so that !events #a*b does not match #acb */
709 || (discrim
->masks
.channel_name
710 && (!entry
->channel_name
711 || irccasecmp(entry
->channel_name
, discrim
->masks
.channel_name
)))
712 || (discrim
->masks
.user_nick
713 && !match_ircglob(entry
->user_nick
, discrim
->masks
.user_nick
))
714 || (discrim
->masks
.user_account
715 && (!entry
->user_account
716 || !match_ircglob(entry
->user_account
, discrim
->masks
.user_account
)))
717 || (discrim
->masks
.user_hostmask
718 && entry
->user_hostmask
719 && !match_ircglob(entry
->user_hostmask
, discrim
->masks
.user_hostmask
))
720 || (discrim
->masks
.command
721 && !match_ircglob(entry
->command
, discrim
->masks
.command
))) {
728 log_report_entry(struct logEntry
*match
, void *extra
)
730 struct logReport
*rpt
= extra
;
731 send_message_type(4, rpt
->user
, rpt
->reporter
, "%s", match
->default_desc
);
735 log_entry_search(struct logSearch
*discrim
, entry_search_func esf
, void *data
)
737 unsigned int matched
= 0;
740 static volatile struct logEntry
*last
;
741 struct logEntry
*entry
;
743 for (entry
= discrim
->type
->log_oldest
, last
= NULL
;
745 last
= entry
, entry
= entry
->next
) {
747 if (entry_match(discrim
, entry
)) {
749 if (++matched
>= discrim
->limit
)
756 for (it
= dict_first(log_types
); it
; it
= iter_next(it
)) {
757 discrim
->type
= iter_data(it
);
758 matched
+= log_entry_search(discrim
, esf
, data
);
765 /* generic helper functions */
768 log_format_timestamp(time_t when
, struct string_buffer
*sbuf
)
771 localtime_r(&when
, &local
);
772 if (sbuf
->size
< 24) {
775 sbuf
->list
= calloc(1, 24);
777 sbuf
->used
= sprintf(sbuf
->list
, "[%02d:%02d:%02d %02d/%02d/%04d]", local
.tm_hour
, local
.tm_min
, local
.tm_sec
, local
.tm_mon
+1, local
.tm_mday
, local
.tm_year
+1900);
781 log_format_audit(struct logEntry
*entry
)
783 struct string_buffer sbuf
;
784 memset(&sbuf
, 0, sizeof(sbuf
));
785 log_format_timestamp(entry
->time
, &sbuf
);
786 string_buffer_append_string(&sbuf
, " (");
787 string_buffer_append_string(&sbuf
, entry
->bot
->nick
);
788 if (entry
->channel_name
) {
789 string_buffer_append(&sbuf
, ':');
790 string_buffer_append_string(&sbuf
, entry
->channel_name
);
792 string_buffer_append_string(&sbuf
, ") [");
793 string_buffer_append_string(&sbuf
, entry
->user_nick
);
794 if (entry
->user_hostmask
) {
795 string_buffer_append(&sbuf
, '!');
796 string_buffer_append_string(&sbuf
, entry
->user_hostmask
);
798 if (entry
->user_account
) {
799 string_buffer_append(&sbuf
, ':');
800 string_buffer_append_string(&sbuf
, entry
->user_account
);
802 string_buffer_append_string(&sbuf
, "]: ");
803 string_buffer_append_string(&sbuf
, entry
->command
);
804 entry
->default_desc
= strdup(sbuf
.list
);
808 /* shared stub log operations act as a noop */
811 ldNop_reopen(UNUSED_ARG(struct logDestination
*self_
)) {
812 /* no operation necessary */
816 ldNop_replay(UNUSED_ARG(struct logDestination
*self_
), UNUSED_ARG(struct log_type
*type
), UNUSED_ARG(int is_write
), UNUSED_ARG(const char *line
)) {
817 /* no operation necessary */
822 struct logDest_file
{
823 struct logDestination base
;
827 static struct logDest_vtable ldFile_vtbl
;
829 static struct logDestination
*
830 ldFile_open(const char *args
) {
831 struct logDest_file
*ld
;
832 ld
= calloc(1, sizeof(*ld
));
833 ld
->base
.vtbl
= &ldFile_vtbl
;
834 ld
->fname
= strdup(args
);
835 ld
->output
= fopen(ld
->fname
, "a");
840 ldFile_reopen(struct logDestination
*self_
) {
841 struct logDest_file
*self
= (struct logDest_file
*)self_
;
842 fclose(self
->output
);
843 self
->output
= fopen(self
->fname
, "a");
847 ldFile_close(struct logDestination
*self_
) {
848 struct logDest_file
*self
= (struct logDest_file
*)self_
;
849 fclose(self
->output
);
855 ldFile_audit(struct logDestination
*self_
, UNUSED_ARG(struct log_type
*type
), struct logEntry
*entry
) {
856 struct logDest_file
*self
= (struct logDest_file
*)self_
;
857 fputs(entry
->default_desc
, self
->output
);
858 fputc('\n', self
->output
);
859 fflush(self
->output
);
863 ldFile_replay(struct logDestination
*self_
, UNUSED_ARG(struct log_type
*type
), int is_write
, const char *line
) {
864 struct logDest_file
*self
= (struct logDest_file
*)self_
;
865 struct string_buffer sbuf
;
866 memset(&sbuf
, 0, sizeof(sbuf
));
867 log_format_timestamp(now
, &sbuf
);
868 string_buffer_append_string(&sbuf
, is_write
? "W: " : " ");
869 string_buffer_append_string(&sbuf
, line
);
870 fputs(sbuf
.list
, self
->output
);
871 fputc('\n', self
->output
);
873 fflush(self
->output
);
877 ldFile_module(struct logDestination
*self_
, struct log_type
*type
, enum log_severity sev
, const char *message
) {
878 struct logDest_file
*self
= (struct logDest_file
*)self_
;
879 struct string_buffer sbuf
;
880 memset(&sbuf
, 0, sizeof(sbuf
));
881 log_format_timestamp(now
, &sbuf
);
882 fprintf(self
->output
, "%s (%s:%s) %s\n", sbuf
.list
, type
->name
, log_severity_names
[sev
], message
);
884 fflush(self
->output
);
887 static struct logDest_vtable ldFile_vtbl
= {
899 static struct logDest_vtable ldStd_vtbl
;
901 static struct logDestination
*
902 ldStd_open(const char *args
) {
903 struct logDest_file
*ld
;
904 ld
= calloc(1, sizeof(*ld
));
905 ld
->base
.vtbl
= &ldStd_vtbl
;
906 ld
->fname
= strdup(args
);
908 /* Print to stderr if given "err" and default to stdout otherwise. */
910 ld
->output
= fdopen(atoi(args
), "a");
911 else if (!strcasecmp(args
, "err"))
920 ldStd_close(struct logDestination
*self_
) {
921 struct logDest_file
*self
= (struct logDest_file
*)self_
;
927 ldStd_replay(struct logDestination
*self_
, UNUSED_ARG(struct log_type
*type
), int is_write
, const char *line
) {
928 struct logDest_file
*self
= (struct logDest_file
*)self_
;
929 fprintf(self
->output
, "%s%s\n", is_write
? "W: " : " ", line
);
933 ldStd_module(struct logDestination
*self_
, UNUSED_ARG(struct log_type
*type
), enum log_severity sev
, const char *message
) {
934 struct logDest_file
*self
= (struct logDest_file
*)self_
;
935 fprintf(self
->output
, "%s: %s\n", log_severity_names
[sev
], message
);
938 static struct logDest_vtable ldStd_vtbl
= {
951 struct logDestination base
;
954 static struct logDest_vtable ldIrc_vtbl
;
956 static struct logDestination
*
957 ldIrc_open(const char *args
) {
958 struct logDest_irc
*ld
;
959 ld
= calloc(1, sizeof(*ld
));
960 ld
->base
.vtbl
= &ldIrc_vtbl
;
961 ld
->target
= strdup(args
);
966 ldIrc_close(struct logDestination
*self_
) {
967 struct logDest_irc
*self
= (struct logDest_irc
*)self_
;
973 * ldIrc_audit and ldIrc_module send log messages targetted to an IRC channel, to the channel
976 ldIrc_audit(struct logDestination
*self_
, UNUSED_ARG(struct log_type
*type
), struct logEntry
*entry
) {
977 struct logDest_irc
*self
= (struct logDest_irc
*)self_
;
979 if (entry
->channel_name
) {
980 send_target_message(5, self
->target
, entry
->bot
, "(%s", strchr(strchr(entry
->default_desc
, ' '), ':')+1);
982 send_target_message(5, self
->target
, entry
->bot
, "%s", strchr(entry
->default_desc
, ')')+2);
987 ldIrc_module(struct logDestination
*self_
, struct log_type
*type
, enum log_severity sev
, const char *message
) {
988 struct logDest_irc
*self
= (struct logDest_irc
*)self_
;
989 extern struct userNode
*opserv
;
991 send_target_message(5, self
->target
, opserv
, "%s %s: %s\n", type
->name
, log_severity_names
[sev
], message
);
994 static struct logDest_vtable ldIrc_vtbl
= {
1000 ldNop_replay
, /* totally ignore this - it would be a recipe for disaster */
1007 log_types
= dict_new();
1008 dict_set_free_keys(log_types
, free
);
1009 dict_set_free_data(log_types
, log_type_free
);
1010 log_dest_types
= dict_new();
1011 /* register log types */
1012 dict_insert(log_dest_types
, ldFile_vtbl
.type_name
, &ldFile_vtbl
);
1013 dict_insert(log_dest_types
, ldStd_vtbl
.type_name
, &ldStd_vtbl
);
1014 dict_insert(log_dest_types
, ldIrc_vtbl
.type_name
, &ldIrc_vtbl
);
1015 conf_register_reload(log_conf_read
);
1016 log_default
= log_register_type("*", NULL
);
1017 reg_exit_func(cleanup_logs
);
1018 message_register_table(msgtab
);
1022 void SyncLog(char *fmt
,...)
1025 char buff
[MAXLEN
*4];
1029 va_start(args
, fmt
);
1030 vsnprintf(buff
, MAXLEN
, fmt
, args
);
1031 buff
[MAXLEN
- 1] = '\0';
1034 for (tmp
= buff
; *tmp
; tmp
++)
1036 if ((*tmp
== '\n') || (*tmp
== '\r'))
1038 else if (*tmp
== '\001')
1042 if((LogFile
= fopen("sync.log", "a")))
1044 fprintf(LogFile
, "%s: %s\n", time2str(time(NULL
)), buff
);
1050 int parselog(char *LogLine
, struct userNode
*user
, struct chanNode
*cptr
, char *chan
, char *nuh
, char *command
, char *rest
)
1052 struct svccmd
*svccmd
;
1054 struct service
*service
;
1056 char serv
[NICKLEN
+1];
1058 char myservc
[MAXLEN
];
1071 datestr
= (char *) mysep(&LogLine
, "]");
1072 mywho
= (char *) mysep(&LogLine
, " ");
1073 mynuh
= (char *) mysep(&LogLine
, " ");
1074 mycommand
= (char *) mysep(&LogLine
, " ");
1075 myrest
= (char *) mysep(&LogLine
, "\0");
1076 myserva
= (char *) mysep(&mywho
, ":");
1077 mychana
= (char *) mysep(&mywho
, ":");
1078 myserv
= (char *) mysep(&myserva
, "(");
1079 mychan
= (char *) mysep(&mychana
, ")");
1099 if(*chan
&& strcasecmp(mychan
, chan
))
1104 info
= conf_get_data("services/opserv/nick", RECDB_QSTRING
);
1109 strcpy(myservc
, myserv
);
1112 if (!strcmp(myserv
, info
)) {
1116 if ((service
= service_find(myserv
))) {
1117 if (!(cmd
= dict_find(service
->commands
, mycommand
, NULL
)))
1120 if (!(svccmd
= svccmd_resolve_name(cmd
, mycommand
)))
1123 pos
= snprintf(buf
, sizeof(buf
), "%s.%s", svccmd
->command
->parent
->name
, svccmd
->command
->name
);
1125 if (svccmd
->alias
.used
) {
1127 unsplit_string((char**)svccmd
->alias
.list
+1, svccmd
->alias
.used
-1, buf
+pos
);
1132 if (!(strcmp(buf
+0, "OpServ.OP")))
1134 if (!(strcmp(buf
+0, "OpServ.DEOP")))
1136 if (!(strcmp(buf
+0, "OpServ.VOICE")))
1138 if (!(strcmp(buf
+0, "OpServ.DEVOICE")))
1140 if (!(strcmp(buf
+0, "OpServ.KICK")))
1142 if (!(strcmp(buf
+0, "OpServ.KICKBAN")))
1145 if (!(strcmp(buf
+0, "OpServ.OPALL")))
1147 if (!(strcmp(buf
+0, "OpServ.DEOPALL")))
1149 if (!(strcmp(buf
+0, "OpServ.VOICEALL")))
1151 if (!(strcmp(buf
+0, "OpServ.DEVOICEALL")))
1153 if (!(strcmp(buf
+0, "OpServ.KICKALL")))
1155 if (!(strcmp(buf
+0, "OpServ.KICKBANALL")))
1159 if (!(strcmp(buf
+0, "OpServ.INVITE")))
1161 if (!(strcmp(buf
+0, "OpServ.INVITEME")))
1163 if (!(strcmp(buf
+0, "OpServ.CLEARBANS")))
1165 if (!(strcmp(buf
+0, "OpServ.CLEARMODES")))
1169 send_message(user
, chanserv
, "LAST_RESULTS", datestr
, myserv
, mynuh
, mycommand
, myrest
);
1173 sprintf(serv
, "%s", "");
1174 send_message(user
, chanserv
, "LAST_RESULTS", datestr
, serv
, mynuh
, mycommand
, myrest
);
1181 int ShowLog(struct userNode
*user
, struct chanNode
*cptr
, char *chan
, char *nuh
, char *command
, char *rest
, int maxlines
)
1184 int i
, s
, Last
= 0, filelen
, first
;
1185 int line
= 0, searchline
= 0;
1188 char Buff
[Block
+1] = "", PrevBuff
[Block
+1] = "";
1189 char LogLine
[(Block
+1)*2] = ""; /* To hold our exported results. */
1191 if(!(TheFile
= fopen(AccountingLog
, "r")))
1193 send_message(user
, chanserv
, "LAST_ERROR", AccountingLog
, strerror(errno
));
1196 s
= fseek(TheFile
, 0, SEEK_END
); /* Start at the end. */
1197 filelen
= ftell(TheFile
); /* Find out the length. */
1198 FilePosition
= 0; /* (from the bottom) */
1199 send_message(user
, chanserv
, "LAST_COMMAND_LOG", cptr
->name
);
1200 send_message(user
, chanserv
, "LAST_LINE");
1203 FilePosition
+= Block
;
1204 if(FilePosition
> filelen
)
1206 FilePosition
= filelen
;
1209 if((s
= fseek(TheFile
, filelen
-FilePosition
, SEEK_SET
)) < 0)
1211 send_message(user
, chanserv
, "LAST_ERROR", AccountingLog
, strerror(errno
));
1215 s
= fread(Buff
, 1, Block
, TheFile
);
1219 send_message(user
, chanserv
, "LAST_ERROR", AccountingLog
, strerror(errno
));
1224 for(i
= s
-1; i
>= 0; i
--)
1229 strcpy(LogLine
, &Buff
[i
+1]);
1231 strcat(LogLine
, PrevBuff
);
1234 if(parselog(LogLine
, user
, cptr
, chan
, nuh
, command
, rest
))
1236 if(line
>= maxlines
)
1238 send_message(user
, chanserv
, "LAST_STOPPING_AT", maxlines
);
1241 if( searchline
>= MAXLOGSEARCHLENGTH
)
1243 send_message(user
, chanserv
, "LAST_MAX_AGE");
1250 strcpy(PrevBuff
, Buff
); /* Save the remaining bit. */
1252 send_message(user
, chanserv
, "LAST_END_OF_LOG", line
);