Remove rarely-used event_log/LOG_EVENT support
[asterisk/asterisk.git] / main / logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Asterisk Logger
22  * 
23  * Logging routines
24  *
25  * \author Mark Spencer <markster@digium.com>
26  */
27
28 /*
29  * define _ASTERISK_LOGGER_H to prevent the inclusion of logger.h;
30  * it redefines LOG_* which we need to define syslog_level_map.
31  * later, we force the inclusion of logger.h again.
32  */
33 #define _ASTERISK_LOGGER_H
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 /*
39  * WARNING: additional #include directives should NOT be placed here, they 
40  * should be placed AFTER '#undef _ASTERISK_LOGGER_H' below
41  */
42 #include "asterisk/_private.h"
43 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
44 #include <signal.h>
45 #include <time.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #ifdef HAVE_BKTR
49 #include <execinfo.h>
50 #define MAX_BACKTRACE_FRAMES 20
51 #endif
52
53 #define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
54                         from <syslog.h> which is included by logger.h */
55 #include <syslog.h>
56
57 static int syslog_level_map[] = {
58         LOG_DEBUG,
59         LOG_INFO,    /* arbitrary equivalent of LOG_EVENT */
60         LOG_NOTICE,
61         LOG_WARNING,
62         LOG_ERR,
63         LOG_DEBUG,
64         LOG_DEBUG
65 };
66
67 #define SYSLOG_NLEVELS sizeof(syslog_level_map) / sizeof(int)
68
69 #undef _ASTERISK_LOGGER_H       /* now include logger.h */
70 #include "asterisk/logger.h"
71 #include "asterisk/lock.h"
72 #include "asterisk/channel.h"
73 #include "asterisk/config.h"
74 #include "asterisk/term.h"
75 #include "asterisk/cli.h"
76 #include "asterisk/utils.h"
77 #include "asterisk/manager.h"
78 #include "asterisk/threadstorage.h"
79 #include "asterisk/strings.h"
80 #include "asterisk/pbx.h"
81 #include "asterisk/app.h"
82
83 #if defined(__linux__) && !defined(__NR_gettid)
84 #include <asm/unistd.h>
85 #endif
86
87 #if defined(__linux__) && defined(__NR_gettid)
88 #define GETTID() syscall(__NR_gettid)
89 #else
90 #define GETTID() getpid()
91 #endif
92
93 static char dateformat[256] = "%b %e %T";               /* Original Asterisk Format */
94
95 static char queue_log_name[256] = QUEUELOG;
96 static char exec_after_rotate[256] = "";
97
98 static int filesize_reload_needed;
99 static int global_logmask = -1;
100
101 enum rotatestrategy {
102         SEQUENTIAL = 1 << 0,     /* Original method - create a new file, in order */
103         ROTATE = 1 << 1,         /* Rotate all files, such that the oldest file has the highest suffix */
104         TIMESTAMP = 1 << 2,      /* Append the epoch timestamp onto the end of the archived file */
105 } rotatestrategy = SEQUENTIAL;
106
107 static struct {
108         unsigned int queue_log:1;
109 } logfiles = { 1 };
110
111 static char hostname[MAXHOSTNAMELEN];
112
113 enum logtypes {
114         LOGTYPE_SYSLOG,
115         LOGTYPE_FILE,
116         LOGTYPE_CONSOLE,
117 };
118
119 struct logchannel {
120         int logmask;                    /* What to log to this channel */
121         int disabled;                   /* If this channel is disabled or not */
122         int facility;                   /* syslog facility */
123         enum logtypes type;             /* Type of log channel */
124         FILE *fileptr;                  /* logfile logging file pointer */
125         char filename[256];             /* Filename */
126         AST_LIST_ENTRY(logchannel) list;
127 };
128
129 static AST_RWLIST_HEAD_STATIC(logchannels, logchannel);
130
131 enum logmsgtypes {
132         LOGMSG_NORMAL = 0,
133         LOGMSG_VERBOSE,
134 };
135
136 struct logmsg {
137         enum logmsgtypes type;
138         char date[256];
139         int level;
140         char file[80];
141         int line;
142         char function[80];
143         long process_id;
144         AST_LIST_ENTRY(logmsg) list;
145         char str[0];
146 };
147
148 static AST_LIST_HEAD_STATIC(logmsgs, logmsg);
149 static pthread_t logthread = AST_PTHREADT_NULL;
150 static ast_cond_t logcond;
151 static int close_logger_thread;
152
153 static FILE *qlog;
154
155 /*! \brief Logging channels used in the Asterisk logging system */
156 static char *levels[] = {
157         "DEBUG",
158         "---EVENT---",          /* no longer used */
159         "NOTICE",
160         "WARNING",
161         "ERROR",
162         "VERBOSE",
163         "DTMF"
164 };
165
166 /*! \brief Colors used in the console for logging */
167 static int colors[] = {
168         COLOR_BRGREEN,
169         COLOR_BRBLUE,
170         COLOR_YELLOW,
171         COLOR_BRRED,
172         COLOR_RED,
173         COLOR_GREEN,
174         COLOR_BRGREEN
175 };
176
177 AST_THREADSTORAGE(verbose_buf);
178 #define VERBOSE_BUF_INIT_SIZE   256
179
180 AST_THREADSTORAGE(log_buf);
181 #define LOG_BUF_INIT_SIZE       256
182
183 static int make_components(const char *s, int lineno)
184 {
185         char *w;
186         int res = 0;
187         char *stringp = ast_strdupa(s);
188
189         while ((w = strsep(&stringp, ","))) {
190                 w = ast_skip_blanks(w);
191                 if (!strcasecmp(w, "error")) 
192                         res |= (1 << __LOG_ERROR);
193                 else if (!strcasecmp(w, "warning"))
194                         res |= (1 << __LOG_WARNING);
195                 else if (!strcasecmp(w, "notice"))
196                         res |= (1 << __LOG_NOTICE);
197                 else if (!strcasecmp(w, "debug"))
198                         res |= (1 << __LOG_DEBUG);
199                 else if (!strcasecmp(w, "verbose"))
200                         res |= (1 << __LOG_VERBOSE);
201                 else if (!strcasecmp(w, "dtmf"))
202                         res |= (1 << __LOG_DTMF);
203                 else {
204                         fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
205                 }
206         }
207
208         return res;
209 }
210
211 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno)
212 {
213         struct logchannel *chan;
214         char *facility;
215 #ifndef SOLARIS
216         CODE *cptr;
217 #endif
218
219         if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan))))
220                 return NULL;
221
222         if (!strcasecmp(channel, "console")) {
223                 chan->type = LOGTYPE_CONSOLE;
224         } else if (!strncasecmp(channel, "syslog", 6)) {
225                 /*
226                 * syntax is:
227                 *  syslog.facility => level,level,level
228                 */
229                 facility = strchr(channel, '.');
230                 if (!facility++ || !facility) {
231                         facility = "local0";
232                 }
233
234 #ifndef SOLARIS
235                 /*
236                 * Walk through the list of facilitynames (defined in sys/syslog.h)
237                 * to see if we can find the one we have been given
238                 */
239                 chan->facility = -1;
240                 cptr = facilitynames;
241                 while (cptr->c_name) {
242                         if (!strcasecmp(facility, cptr->c_name)) {
243                                 chan->facility = cptr->c_val;
244                                 break;
245                         }
246                         cptr++;
247                 }
248 #else
249                 chan->facility = -1;
250                 if (!strcasecmp(facility, "kern")) 
251                         chan->facility = LOG_KERN;
252                 else if (!strcasecmp(facility, "USER")) 
253                         chan->facility = LOG_USER;
254                 else if (!strcasecmp(facility, "MAIL")) 
255                         chan->facility = LOG_MAIL;
256                 else if (!strcasecmp(facility, "DAEMON")) 
257                         chan->facility = LOG_DAEMON;
258                 else if (!strcasecmp(facility, "AUTH")) 
259                         chan->facility = LOG_AUTH;
260                 else if (!strcasecmp(facility, "SYSLOG")) 
261                         chan->facility = LOG_SYSLOG;
262                 else if (!strcasecmp(facility, "LPR")) 
263                         chan->facility = LOG_LPR;
264                 else if (!strcasecmp(facility, "NEWS")) 
265                         chan->facility = LOG_NEWS;
266                 else if (!strcasecmp(facility, "UUCP")) 
267                         chan->facility = LOG_UUCP;
268                 else if (!strcasecmp(facility, "CRON")) 
269                         chan->facility = LOG_CRON;
270                 else if (!strcasecmp(facility, "LOCAL0")) 
271                         chan->facility = LOG_LOCAL0;
272                 else if (!strcasecmp(facility, "LOCAL1")) 
273                         chan->facility = LOG_LOCAL1;
274                 else if (!strcasecmp(facility, "LOCAL2")) 
275                         chan->facility = LOG_LOCAL2;
276                 else if (!strcasecmp(facility, "LOCAL3")) 
277                         chan->facility = LOG_LOCAL3;
278                 else if (!strcasecmp(facility, "LOCAL4")) 
279                         chan->facility = LOG_LOCAL4;
280                 else if (!strcasecmp(facility, "LOCAL5")) 
281                         chan->facility = LOG_LOCAL5;
282                 else if (!strcasecmp(facility, "LOCAL6")) 
283                         chan->facility = LOG_LOCAL6;
284                 else if (!strcasecmp(facility, "LOCAL7")) 
285                         chan->facility = LOG_LOCAL7;
286 #endif /* Solaris */
287
288                 if (0 > chan->facility) {
289                         fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
290                         ast_free(chan);
291                         return NULL;
292                 }
293
294                 chan->type = LOGTYPE_SYSLOG;
295                 snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
296                 openlog("asterisk", LOG_PID, chan->facility);
297         } else {
298                 if (channel[0] == '/') {
299                         if (!ast_strlen_zero(hostname)) { 
300                                 snprintf(chan->filename, sizeof(chan->filename), "%s.%s", channel, hostname);
301                         } else {
302                                 ast_copy_string(chan->filename, channel, sizeof(chan->filename));
303                         }
304                 }                 
305                 
306                 if (!ast_strlen_zero(hostname)) {
307                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s", ast_config_AST_LOG_DIR, channel, hostname);
308                 } else {
309                         snprintf(chan->filename, sizeof(chan->filename), "%s/%s", ast_config_AST_LOG_DIR, channel);
310                 }
311                 chan->fileptr = fopen(chan->filename, "a");
312                 if (!chan->fileptr) {
313                         /* Can't log here, since we're called with a lock */
314                         fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
315                 } 
316                 chan->type = LOGTYPE_FILE;
317         }
318         chan->logmask = make_components(components, lineno);
319         return chan;
320 }
321
322 static void init_logger_chain(int locked)
323 {
324         struct logchannel *chan;
325         struct ast_config *cfg;
326         struct ast_variable *var;
327         const char *s;
328         struct ast_flags config_flags = { 0 };
329
330         if (!(cfg = ast_config_load2("logger.conf", "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID)
331                 return;
332
333         /* delete our list of log channels */
334         if (!locked)
335                 AST_RWLIST_WRLOCK(&logchannels);
336         while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list)))
337                 ast_free(chan);
338         if (!locked)
339                 AST_RWLIST_UNLOCK(&logchannels);
340         
341         global_logmask = 0;
342         errno = 0;
343         /* close syslog */
344         closelog();
345         
346         /* If no config file, we're fine, set default options. */
347         if (!cfg) {
348                 if (errno)
349                         fprintf(stderr, "Unable to open logger.conf: %s; default settings will be used.\n", strerror(errno));
350                 else
351                         fprintf(stderr, "Errors detected in logger.conf: see above; default settings will be used.\n");
352                 if (!(chan = ast_calloc(1, sizeof(*chan))))
353                         return;
354                 chan->type = LOGTYPE_CONSOLE;
355                 chan->logmask = 28; /*warning,notice,error */
356                 if (!locked)
357                         AST_RWLIST_WRLOCK(&logchannels);
358                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
359                 if (!locked)
360                         AST_RWLIST_UNLOCK(&logchannels);
361                 global_logmask |= chan->logmask;
362                 return;
363         }
364         
365         if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
366                 if (ast_true(s)) {
367                         if (gethostname(hostname, sizeof(hostname) - 1)) {
368                                 ast_copy_string(hostname, "unknown", sizeof(hostname));
369                                 fprintf(stderr, "What box has no hostname???\n");
370                         }
371                 } else
372                         hostname[0] = '\0';
373         } else
374                 hostname[0] = '\0';
375         if ((s = ast_variable_retrieve(cfg, "general", "dateformat")))
376                 ast_copy_string(dateformat, s, sizeof(dateformat));
377         else
378                 ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
379         if ((s = ast_variable_retrieve(cfg, "general", "queue_log")))
380                 logfiles.queue_log = ast_true(s);
381         if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name")))
382                 ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
383         if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate")))
384                 ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
385         if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
386                 if (strcasecmp(s, "timestamp") == 0)
387                         rotatestrategy = TIMESTAMP;
388                 else if (strcasecmp(s, "rotate") == 0)
389                         rotatestrategy = ROTATE;
390                 else if (strcasecmp(s, "sequential") == 0)
391                         rotatestrategy = SEQUENTIAL;
392                 else
393                         fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
394         } else {
395                 if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
396                         rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
397                         fprintf(stderr, "rotatetimestamp option has been deprecated.  Please use rotatestrategy instead.\n");
398                 }
399         }
400
401         if (!locked)
402                 AST_RWLIST_WRLOCK(&logchannels);
403         var = ast_variable_browse(cfg, "logfiles");
404         for (; var; var = var->next) {
405                 if (!(chan = make_logchannel(var->name, var->value, var->lineno)))
406                         continue;
407                 AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
408                 global_logmask |= chan->logmask;
409         }
410         if (!locked)
411                 AST_RWLIST_UNLOCK(&logchannels);
412
413         ast_config_destroy(cfg);
414 }
415
416 void ast_child_verbose(int level, const char *fmt, ...)
417 {
418         char *msg = NULL, *emsg = NULL, *sptr, *eptr;
419         va_list ap, aq;
420         int size;
421
422         /* Don't bother, if the level isn't that high */
423         if (option_verbose < level) {
424                 return;
425         }
426
427         va_start(ap, fmt);
428         va_copy(aq, ap);
429         if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
430                 va_end(ap);
431                 va_end(aq);
432                 return;
433         }
434         va_end(ap);
435
436         if (!(msg = ast_malloc(size + 1))) {
437                 va_end(aq);
438                 return;
439         }
440
441         vsnprintf(msg, size + 1, fmt, aq);
442         va_end(aq);
443
444         if (!(emsg = ast_malloc(size * 2 + 1))) {
445                 ast_free(msg);
446                 return;
447         }
448
449         for (sptr = msg, eptr = emsg; ; sptr++) {
450                 if (*sptr == '"') {
451                         *eptr++ = '\\';
452                 }
453                 *eptr++ = *sptr;
454                 if (*sptr == '\0') {
455                         break;
456                 }
457         }
458         ast_free(msg);
459
460         fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
461         fflush(stdout);
462         ast_free(emsg);
463 }
464
465 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
466 {
467         va_list ap;
468         char qlog_msg[8192];
469         int qlog_len;
470         char time_str[16];
471
472         if (ast_check_realtime("queue_log")) {
473                 va_start(ap, fmt);
474                 vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
475                 va_end(ap);
476                 snprintf(time_str, sizeof(time_str), "%ld", (long)time(NULL));
477                 ast_store_realtime("queue_log", "time", time_str, 
478                                                 "callid", callid, 
479                                                 "queuename", queuename, 
480                                                 "agent", agent, 
481                                                 "event", event,
482                                                 "data", qlog_msg,
483                                                 SENTINEL);
484         } else {
485                 if (qlog) {
486                         va_start(ap, fmt);
487                         qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
488                         vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
489                         va_end(ap);
490                 }
491                 AST_RWLIST_RDLOCK(&logchannels);
492                 if (qlog) {
493                         fprintf(qlog, "%s\n", qlog_msg);
494                         fflush(qlog);
495                 }
496                 AST_RWLIST_UNLOCK(&logchannels);
497         }
498 }
499
500 static int rotate_file(const char *filename)
501 {
502         char old[PATH_MAX];
503         char new[PATH_MAX];
504         int x, y, which, found, res = 0, fd;
505         char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
506
507         switch (rotatestrategy) {
508         case SEQUENTIAL:
509                 for (x = 0; ; x++) {
510                         snprintf(new, sizeof(new), "%s.%d", filename, x);
511                         fd = open(new, O_RDONLY);
512                         if (fd > -1)
513                                 close(fd);
514                         else
515                                 break;
516                 }
517                 if (rename(filename, new)) {
518                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
519                         res = -1;
520                 }
521                 break;
522         case TIMESTAMP:
523                 snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
524                 if (rename(filename, new)) {
525                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
526                         res = -1;
527                 }
528                 break;
529         case ROTATE:
530                 /* Find the next empty slot, including a possible suffix */
531                 for (x = 0; ; x++) {
532                         found = 0;
533                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
534                                 snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
535                                 fd = open(new, O_RDONLY);
536                                 if (fd > -1) {
537                                         close(fd);
538                                         found = 1;
539                                         break;
540                                 }
541                         }
542                         if (!found) {
543                                 break;
544                         }
545                 }
546
547                 /* Found an empty slot */
548                 for (y = x; y > 0; y--) {
549                         for (which = 0; which < ARRAY_LEN(suffixes); which++) {
550                                 snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
551                                 fd = open(old, O_RDONLY);
552                                 if (fd > -1) {
553                                         /* Found the right suffix */
554                                         close(fd);
555                                         snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
556                                         if (rename(old, new)) {
557                                                 fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
558                                                 res = -1;
559                                         }
560                                         break;
561                                 }
562                         }
563                 }
564
565                 /* Finally, rename the current file */
566                 snprintf(new, sizeof(new), "%s.0", filename);
567                 if (rename(filename, new)) {
568                         fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
569                         res = -1;
570                 }
571         }
572
573         if (!ast_strlen_zero(exec_after_rotate)) {
574                 struct ast_channel *c = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Logger/rotate");
575                 char buf[512];
576                 pbx_builtin_setvar_helper(c, "filename", filename);
577                 pbx_substitute_variables_helper(c, exec_after_rotate, buf, sizeof(buf));
578                 if (ast_safe_system(buf) != -1) {
579                         ast_log(LOG_WARNING, "error executing '%s'\n", buf);
580                 }
581                 c = ast_channel_release(c);
582         }
583         return res;
584 }
585
586 static int reload_logger(int rotate)
587 {
588         char old[PATH_MAX] = "";
589         int queue_rotate = rotate;
590         struct logchannel *f;
591         int res = 0;
592         struct stat st;
593
594         AST_RWLIST_WRLOCK(&logchannels);
595
596         if (qlog) {
597                 if (rotate < 0) {
598                         /* Check filesize - this one typically doesn't need an auto-rotate */
599                         snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
600                         if (stat(old, &st) != 0 || st.st_size > 0x40000000) { /* Arbitrarily, 1 GB */
601                                 fclose(qlog);
602                                 qlog = NULL;
603                         } else
604                                 queue_rotate = 0;
605                 } else {
606                         fclose(qlog);
607                         qlog = NULL;
608                 }
609         } else 
610                 queue_rotate = 0;
611
612         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
613
614         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
615                 if (f->disabled) {
616                         f->disabled = 0;        /* Re-enable logging at reload */
617                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
618                 }
619                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
620                         fclose(f->fileptr);     /* Close file */
621                         f->fileptr = NULL;
622                         if (rotate)
623                                 rotate_file(f->filename);
624                 }
625         }
626
627         filesize_reload_needed = 0;
628
629         init_logger_chain(1 /* locked */);
630
631         if (logfiles.queue_log) {
632                 snprintf(old, sizeof(old), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
633                 if (queue_rotate)
634                         rotate_file(old);
635
636                 qlog = fopen(old, "a");
637                 if (qlog) {
638                         AST_RWLIST_UNLOCK(&logchannels);
639                         ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
640                         AST_RWLIST_WRLOCK(&logchannels);
641                         ast_verb(1, "Asterisk Queue Logger restarted\n");
642                 } else {
643                         ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
644                         res = -1;
645                 }
646         }
647
648         AST_RWLIST_UNLOCK(&logchannels);
649
650         return res;
651 }
652
653 /*! \brief Reload the logger module without rotating log files (also used from loader.c during
654         a full Asterisk reload) */
655 int logger_reload(void)
656 {
657         if(reload_logger(0))
658                 return RESULT_FAILURE;
659         return RESULT_SUCCESS;
660 }
661
662 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
663 {
664         switch (cmd) {
665         case CLI_INIT:
666                 e->command = "logger reload";
667                 e->usage = 
668                         "Usage: logger reload\n"
669                         "       Reloads the logger subsystem state.  Use after restarting syslogd(8) if you are using syslog logging.\n";
670                 return NULL;
671         case CLI_GENERATE:
672                 return NULL;
673         }
674         if (reload_logger(0)) {
675                 ast_cli(a->fd, "Failed to reload the logger\n");
676                 return CLI_FAILURE;
677         }
678         return CLI_SUCCESS;
679 }
680
681 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
682 {
683         switch (cmd) {
684         case CLI_INIT:
685                 e->command = "logger rotate";
686                 e->usage = 
687                         "Usage: logger rotate\n"
688                         "       Rotates and Reopens the log files.\n";
689                 return NULL;
690         case CLI_GENERATE:
691                 return NULL;    
692         }
693         if (reload_logger(1)) {
694                 ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
695                 return CLI_FAILURE;
696         } 
697         return CLI_SUCCESS;
698 }
699
700 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
701 {
702         int x;
703         int state;
704         int level = -1;
705
706         switch (cmd) {
707         case CLI_INIT:
708                 e->command = "logger set level";
709                 e->usage = 
710                         "Usage: logger set level\n"
711                         "       Set a specific log level to enabled/disabled for this console.\n";
712                 return NULL;
713         case CLI_GENERATE:
714                 return NULL;
715         }
716
717         if (a->argc < 5)
718                 return CLI_SHOWUSAGE;
719
720         for (x = 0; x <= NUMLOGLEVELS; x++) {
721                 if (!strcasecmp(a->argv[3], levels[x])) {
722                         level = x;
723                         break;
724                 }
725         }
726
727         state = ast_true(a->argv[4]) ? 1 : 0;
728
729         if (level != -1) {
730                 ast_console_toggle_loglevel(a->fd, level, state);
731                 ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
732         } else
733                 return CLI_SHOWUSAGE;
734
735         return CLI_SUCCESS;
736 }
737
738 /*! \brief CLI command to show logging system configuration */
739 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
740 {
741 #define FORMATL "%-35.35s %-8.8s %-9.9s "
742         struct logchannel *chan;
743         switch (cmd) {
744         case CLI_INIT:
745                 e->command = "logger show channels";
746                 e->usage = 
747                         "Usage: logger show channels\n"
748                         "       List configured logger channels.\n";
749                 return NULL;
750         case CLI_GENERATE:
751                 return NULL;    
752         }
753         ast_cli(a->fd, FORMATL, "Channel", "Type", "Status");
754         ast_cli(a->fd, "Configuration\n");
755         ast_cli(a->fd, FORMATL, "-------", "----", "------");
756         ast_cli(a->fd, "-------------\n");
757         AST_RWLIST_RDLOCK(&logchannels);
758         AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
759                 ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
760                         chan->disabled ? "Disabled" : "Enabled");
761                 ast_cli(a->fd, " - ");
762                 if (chan->logmask & (1 << __LOG_DEBUG)) 
763                         ast_cli(a->fd, "Debug ");
764                 if (chan->logmask & (1 << __LOG_DTMF)) 
765                         ast_cli(a->fd, "DTMF ");
766                 if (chan->logmask & (1 << __LOG_VERBOSE)) 
767                         ast_cli(a->fd, "Verbose ");
768                 if (chan->logmask & (1 << __LOG_WARNING)) 
769                         ast_cli(a->fd, "Warning ");
770                 if (chan->logmask & (1 << __LOG_NOTICE)) 
771                         ast_cli(a->fd, "Notice ");
772                 if (chan->logmask & (1 << __LOG_ERROR)) 
773                         ast_cli(a->fd, "Error ");
774                 ast_cli(a->fd, "\n");
775         }
776         AST_RWLIST_UNLOCK(&logchannels);
777         ast_cli(a->fd, "\n");
778                 
779         return CLI_SUCCESS;
780 }
781
782 struct verb {
783         void (*verboser)(const char *string);
784         AST_LIST_ENTRY(verb) list;
785 };
786
787 static AST_RWLIST_HEAD_STATIC(verbosers, verb);
788
789 static struct ast_cli_entry cli_logger[] = {
790         AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
791         AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
792         AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
793         AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console")
794 };
795
796 static int handle_SIGXFSZ(int sig) 
797 {
798         /* Indicate need to reload */
799         filesize_reload_needed = 1;
800         return 0;
801 }
802
803 static void ast_log_vsyslog(int level, const char *file, int line, const char *function, char *str, long pid)
804 {
805         char buf[BUFSIZ];
806
807         if (level >= SYSLOG_NLEVELS) {
808                 /* we are locked here, so cannot ast_log() */
809                 fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
810                 return;
811         }
812
813         if (level == __LOG_VERBOSE) {
814                 snprintf(buf, sizeof(buf), "VERBOSE[%ld]: %s", pid, str);
815                 level = __LOG_DEBUG;
816         } else if (level == __LOG_DTMF) {
817                 snprintf(buf, sizeof(buf), "DTMF[%ld]: %s", pid, str);
818                 level = __LOG_DEBUG;
819         } else {
820                 snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: %s",
821                          levels[level], pid, file, line, function, str);
822         }
823
824         term_strip(buf, buf, strlen(buf) + 1);
825         syslog(syslog_level_map[level], "%s", buf);
826 }
827
828 /*! \brief Print a normal log message to the channels */
829 static void logger_print_normal(struct logmsg *logmsg)
830 {
831         struct logchannel *chan = NULL;
832         char buf[BUFSIZ];
833
834         AST_RWLIST_RDLOCK(&logchannels);
835
836         if (!AST_RWLIST_EMPTY(&logchannels)) {
837                 AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
838                         /* If the channel is disabled, then move on to the next one */
839                         if (chan->disabled)
840                                 continue;
841                         /* Check syslog channels */
842                         if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << logmsg->level))) {
843                                 ast_log_vsyslog(logmsg->level, logmsg->file, logmsg->line, logmsg->function, logmsg->str, logmsg->process_id);
844                         /* Console channels */
845                         } else if (chan->type == LOGTYPE_CONSOLE && (chan->logmask & (1 << logmsg->level))) {
846                                 char linestr[128];
847                                 char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
848
849                                 /* If the level is verbose, then skip it */
850                                 if (logmsg->level == __LOG_VERBOSE)
851                                         continue;
852
853                                 /* Turn the numerical line number into a string */
854                                 snprintf(linestr, sizeof(linestr), "%d", logmsg->line);
855                                 /* Build string to print out */
856                                 snprintf(buf, sizeof(buf), "[%s] %s[%ld]: %s:%s %s: %s",
857                                          logmsg->date,
858                                          term_color(tmp1, levels[logmsg->level], colors[logmsg->level], 0, sizeof(tmp1)),
859                                          logmsg->process_id,
860                                          term_color(tmp2, logmsg->file, COLOR_BRWHITE, 0, sizeof(tmp2)),
861                                          term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
862                                          term_color(tmp4, logmsg->function, COLOR_BRWHITE, 0, sizeof(tmp4)),
863                                          logmsg->str);
864                                 /* Print out */
865                                 ast_console_puts_mutable(buf, logmsg->level);
866                         /* File channels */
867                         } else if (chan->type == LOGTYPE_FILE && (chan->logmask & (1 << logmsg->level))) {
868                                 int res = 0;
869
870                                 /* If no file pointer exists, skip it */
871                                 if (!chan->fileptr)
872                                         continue;
873                                 
874                                 /* Print out to the file */
875                                 res = fprintf(chan->fileptr, "[%s] %s[%ld] %s: %s",
876                                               logmsg->date, levels[logmsg->level], logmsg->process_id, logmsg->file, logmsg->str);
877                                 if (res <= 0 && !ast_strlen_zero(logmsg->str)) {
878                                         fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
879                                         if (errno == ENOMEM || errno == ENOSPC)
880                                                 fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
881                                         else
882                                                 fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
883                                         manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
884                                         chan->disabled = 1;
885                                 } else if (res > 0) {
886                                         fflush(chan->fileptr);
887                                 }
888                         }
889                 }
890         } else if (logmsg->level != __LOG_VERBOSE) {
891                 fputs(logmsg->str, stdout);
892         }
893
894         AST_RWLIST_UNLOCK(&logchannels);
895
896         /* If we need to reload because of the file size, then do so */
897         if (filesize_reload_needed) {
898                 reload_logger(-1);
899                 ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
900         }
901
902         return;
903 }
904
905 /*! \brief Print a verbose message to the verbosers */
906 static void logger_print_verbose(struct logmsg *logmsg)
907 {
908         struct verb *v = NULL;
909
910         /* Iterate through the list of verbosers and pass them the log message string */
911         AST_RWLIST_RDLOCK(&verbosers);
912         AST_RWLIST_TRAVERSE(&verbosers, v, list)
913                 v->verboser(logmsg->str);
914         AST_RWLIST_UNLOCK(&verbosers);
915
916         return;
917 }
918
919 /*! \brief Actual logging thread */
920 static void *logger_thread(void *data)
921 {
922         struct logmsg *next = NULL, *msg = NULL;
923
924         for (;;) {
925                 /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
926                 AST_LIST_LOCK(&logmsgs);
927                 if (AST_LIST_EMPTY(&logmsgs)) {
928                         if (close_logger_thread) {
929                                 break;
930                         } else {
931                                 ast_cond_wait(&logcond, &logmsgs.lock);
932                         }
933                 }
934                 next = AST_LIST_FIRST(&logmsgs);
935                 AST_LIST_HEAD_INIT_NOLOCK(&logmsgs);
936                 AST_LIST_UNLOCK(&logmsgs);
937
938                 /* Otherwise go through and process each message in the order added */
939                 while ((msg = next)) {
940                         /* Get the next entry now so that we can free our current structure later */
941                         next = AST_LIST_NEXT(msg, list);
942
943                         /* Depending on the type, send it to the proper function */
944                         if (msg->type == LOGMSG_NORMAL)
945                                 logger_print_normal(msg);
946                         else if (msg->type == LOGMSG_VERBOSE)
947                                 logger_print_verbose(msg);
948
949                         /* Free the data since we are done */
950                         ast_free(msg);
951                 }
952
953                 /* If we should stop, then stop */
954                 if (close_logger_thread)
955                         break;
956         }
957
958         return NULL;
959 }
960
961 int init_logger(void)
962 {
963         char tmp[256];
964         int res = 0;
965
966         /* auto rotate if sig SIGXFSZ comes a-knockin */
967         (void) signal(SIGXFSZ, (void *) handle_SIGXFSZ);
968
969         /* start logger thread */
970         ast_cond_init(&logcond, NULL);
971         if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
972                 ast_cond_destroy(&logcond);
973                 return -1;
974         }
975
976         /* register the logger cli commands */
977         ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
978
979         ast_mkdir(ast_config_AST_LOG_DIR, 0777);
980   
981         /* create log channels */
982         init_logger_chain(0 /* locked */);
983
984         if (logfiles.queue_log) {
985                 snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
986                 qlog = fopen(tmp, "a");
987                 ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
988         }
989         return res;
990 }
991
992 void close_logger(void)
993 {
994         struct logchannel *f = NULL;
995
996         /* Stop logger thread */
997         AST_LIST_LOCK(&logmsgs);
998         close_logger_thread = 1;
999         ast_cond_signal(&logcond);
1000         AST_LIST_UNLOCK(&logmsgs);
1001
1002         if (logthread != AST_PTHREADT_NULL)
1003                 pthread_join(logthread, NULL);
1004
1005         AST_RWLIST_WRLOCK(&logchannels);
1006
1007         if (qlog) {
1008                 fclose(qlog);
1009                 qlog = NULL;
1010         }
1011
1012         AST_RWLIST_TRAVERSE(&logchannels, f, list) {
1013                 if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1014                         fclose(f->fileptr);
1015                         f->fileptr = NULL;
1016                 }
1017         }
1018
1019         closelog(); /* syslog */
1020
1021         AST_RWLIST_UNLOCK(&logchannels);
1022
1023         return;
1024 }
1025
1026 /*!
1027  * \brief send log messages to syslog and/or the console
1028  */
1029 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
1030 {
1031         struct logmsg *logmsg = NULL;
1032         struct ast_str *buf = NULL;
1033         struct ast_tm tm;
1034         struct timeval now = ast_tvnow();
1035         int res = 0;
1036         va_list ap;
1037
1038         if (!(buf = ast_str_thread_get(&log_buf, LOG_BUF_INIT_SIZE)))
1039                 return;
1040
1041         if (AST_RWLIST_EMPTY(&logchannels)) {
1042                 /*
1043                  * we don't have the logger chain configured yet,
1044                  * so just log to stdout
1045                  */
1046                 if (level != __LOG_VERBOSE) {
1047                         int result;
1048                         va_start(ap, fmt);
1049                         result = ast_str_set_va(&buf, BUFSIZ, fmt, ap); /* XXX BUFSIZ ? */
1050                         va_end(ap);
1051                         if (result != AST_DYNSTR_BUILD_FAILED) {
1052                                 term_filter_escapes(ast_str_buffer(buf));
1053                                 fputs(ast_str_buffer(buf), stdout);
1054                         }
1055                 }
1056                 return;
1057         }
1058         
1059         /* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
1060            are non-zero; LOG_DEBUG messages can still be displayed if option_debug
1061            is zero, if option_verbose is non-zero (this allows for 'level zero'
1062            LOG_DEBUG messages to be displayed, if the logmask on any channel
1063            allows it)
1064         */
1065         if (!option_verbose && !option_debug && (level == __LOG_DEBUG))
1066                 return;
1067
1068         /* Ignore anything that never gets logged anywhere */
1069         if (!(global_logmask & (1 << level)))
1070                 return;
1071         
1072         /* Build string */
1073         va_start(ap, fmt);
1074         res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
1075         va_end(ap);
1076
1077         /* If the build failed, then abort and free this structure */
1078         if (res == AST_DYNSTR_BUILD_FAILED)
1079                 return;
1080
1081         /* Create a new logging message */
1082         if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
1083                 return;
1084
1085         /* Copy string over */
1086         strcpy(logmsg->str, ast_str_buffer(buf));
1087
1088         /* Set type to be normal */
1089         logmsg->type = LOGMSG_NORMAL;
1090
1091         /* Create our date/time */
1092         ast_localtime(&now, &tm, NULL);
1093         ast_strftime(logmsg->date, sizeof(logmsg->date), dateformat, &tm);
1094
1095         /* Copy over data */
1096         logmsg->level = level;
1097         logmsg->line = line;
1098         ast_copy_string(logmsg->file, file, sizeof(logmsg->file));
1099         ast_copy_string(logmsg->function, function, sizeof(logmsg->function));
1100         logmsg->process_id = (long) GETTID();
1101
1102         /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
1103         if (logthread != AST_PTHREADT_NULL) {
1104                 AST_LIST_LOCK(&logmsgs);
1105                 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1106                 ast_cond_signal(&logcond);
1107                 AST_LIST_UNLOCK(&logmsgs);
1108         } else {
1109                 logger_print_normal(logmsg);
1110                 ast_free(logmsg);
1111         }
1112
1113         return;
1114 }
1115
1116 #ifdef HAVE_BKTR
1117
1118 struct ast_bt *ast_bt_create(void) 
1119 {
1120         struct ast_bt *bt = ast_calloc(1, sizeof(*bt));
1121         if (!bt) {
1122                 ast_log(LOG_ERROR, "Unable to allocate memory for backtrace structure!\n");
1123                 return NULL;
1124         }
1125
1126         bt->alloced = 1;
1127
1128         ast_bt_get_addresses(bt);
1129
1130         return bt;
1131 }
1132
1133 int ast_bt_get_addresses(struct ast_bt *bt)
1134 {
1135         bt->num_frames = backtrace(bt->addresses, AST_MAX_BT_FRAMES);
1136
1137         return 0;
1138 }
1139
1140 void *ast_bt_destroy(struct ast_bt *bt)
1141 {
1142         if (bt->alloced) {
1143                 ast_free(bt);
1144         }
1145
1146         return NULL;
1147 }
1148
1149 #endif /* HAVE_BKTR */
1150
1151 void ast_backtrace(void)
1152 {
1153 #ifdef HAVE_BKTR
1154         struct ast_bt *bt;
1155         int i = 0;
1156         char **strings;
1157
1158         if (!(bt = ast_bt_create())) {
1159                 ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
1160                 return;
1161         }
1162
1163         if ((strings = backtrace_symbols(bt->addresses, bt->num_frames))) {
1164                 ast_debug(1, "Got %d backtrace record%c\n", bt->num_frames, bt->num_frames != 1 ? 's' : ' ');
1165                 for (i = 0; i < bt->num_frames; i++) {
1166                         ast_log(LOG_DEBUG, "#%d: [%p] %s\n", i, bt->addresses[i], strings[i]);
1167                 }
1168                 free(strings);
1169         } else {
1170                 ast_debug(1, "Could not allocate memory for backtrace\n");
1171         }
1172         ast_bt_destroy(bt);
1173 #else
1174         ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
1175 #endif
1176 }
1177
1178 void __ast_verbose_ap(const char *file, int line, const char *func, const char *fmt, va_list ap)
1179 {
1180         struct logmsg *logmsg = NULL;
1181         struct ast_str *buf = NULL;
1182         int res = 0;
1183
1184         if (!(buf = ast_str_thread_get(&verbose_buf, VERBOSE_BUF_INIT_SIZE)))
1185                 return;
1186
1187         if (ast_opt_timestamp) {
1188                 struct timeval now;
1189                 struct ast_tm tm;
1190                 char date[40];
1191                 char *datefmt;
1192
1193                 now = ast_tvnow();
1194                 ast_localtime(&now, &tm, NULL);
1195                 ast_strftime(date, sizeof(date), dateformat, &tm);
1196                 datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
1197                 sprintf(datefmt, "%c[%s] %s", 127, date, fmt);
1198                 fmt = datefmt;
1199         } else {
1200                 char *tmp = alloca(strlen(fmt) + 2);
1201                 sprintf(tmp, "%c%s", 127, fmt);
1202                 fmt = tmp;
1203         }
1204
1205         /* Build string */
1206         res = ast_str_set_va(&buf, 0, fmt, ap);
1207
1208         /* If the build failed then we can drop this allocated message */
1209         if (res == AST_DYNSTR_BUILD_FAILED)
1210                 return;
1211
1212         if (!(logmsg = ast_calloc(1, sizeof(*logmsg) + res + 1)))
1213                 return;
1214
1215         strcpy(logmsg->str, ast_str_buffer(buf));
1216
1217         ast_log(__LOG_VERBOSE, file, line, func, "%s", logmsg->str + 1);
1218
1219         /* Set type */
1220         logmsg->type = LOGMSG_VERBOSE;
1221         
1222         /* Add to the list and poke the thread if possible */
1223         if (logthread != AST_PTHREADT_NULL) {
1224                 AST_LIST_LOCK(&logmsgs);
1225                 AST_LIST_INSERT_TAIL(&logmsgs, logmsg, list);
1226                 ast_cond_signal(&logcond);
1227                 AST_LIST_UNLOCK(&logmsgs);
1228         } else {
1229                 logger_print_verbose(logmsg);
1230                 ast_free(logmsg);
1231         }
1232 }
1233
1234 void __ast_verbose(const char *file, int line, const char *func, const char *fmt, ...)
1235 {
1236         va_list ap;
1237         va_start(ap, fmt);
1238         __ast_verbose_ap(file, line, func, fmt, ap);
1239         va_end(ap);
1240 }
1241
1242 /* No new code should use this directly, but we have the ABI for backwards compat */
1243 #undef ast_verbose
1244 void __attribute__((format(printf, 1,2))) ast_verbose(const char *fmt, ...);
1245 void ast_verbose(const char *fmt, ...)
1246 {
1247         va_list ap;
1248         va_start(ap, fmt);
1249         __ast_verbose_ap("", 0, "", fmt, ap);
1250         va_end(ap);
1251 }
1252
1253 int ast_register_verbose(void (*v)(const char *string)) 
1254 {
1255         struct verb *verb;
1256
1257         if (!(verb = ast_malloc(sizeof(*verb))))
1258                 return -1;
1259
1260         verb->verboser = v;
1261
1262         AST_RWLIST_WRLOCK(&verbosers);
1263         AST_RWLIST_INSERT_HEAD(&verbosers, verb, list);
1264         AST_RWLIST_UNLOCK(&verbosers);
1265         
1266         return 0;
1267 }
1268
1269 int ast_unregister_verbose(void (*v)(const char *string))
1270 {
1271         struct verb *cur;
1272
1273         AST_RWLIST_WRLOCK(&verbosers);
1274         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&verbosers, cur, list) {
1275                 if (cur->verboser == v) {
1276                         AST_RWLIST_REMOVE_CURRENT(list);
1277                         ast_free(cur);
1278                         break;
1279                 }
1280         }
1281         AST_RWLIST_TRAVERSE_SAFE_END;
1282         AST_RWLIST_UNLOCK(&verbosers);
1283         
1284         return cur ? 0 : -1;
1285 }